Search code examples
phplaravelrouteslaravel-routinglaravel-3

My Routes are Returning a 404, How can I Fix Them?


I've just started learning the Laravel framework and I'm having an issue with routing.

The only route that's working is the default home route that's attached to Laravel out of the box.

I'm using WAMP on Windows and it uses PHP 5.4.3, and Apache 2.2.22, and I also have mod_rewrite enabled, and have removed the 'index.php' from the application.php config file to leave an empty string.

I've created a new controller called User:

class User_Controller extends Base_Controller {
    public $restful = true;

    public function get_index() 
    {
        return View::make('user.index');
    }
}

I've created a view file in application/views/user/ called index.php with some basic HTML code, and in routes.php I've added the following:

Route::get('/', function () {
    return View::make('home.index');
});

Route::get('user', function () {
    return View::make('user.index');
});

The first route works fine when visiting the root (http://localhost/mysite/public) in my web browser, but when I try to go to my second route with http://localhost/mysite/public/user I get a 404 Not Found error. Why would this be happening?


Solution

  • Have you tried adding this to your routes file instead Route::get('user', "user@index")?

    The piece of text before the @, user in this case, will direct the page to the user controller and the piece of text after the @, index, will direct the script to the user function public function get_index().

    I see you're using $restful, in which case you could set your Route to Route::any('user', 'user@index'). This will handle both POST and GET, instead of writing them both out separately.