Laravel 4 : Route to localhost/controller/action

If you are looking for a more automated routing, this would be the Laravel 4 way:

Route:

Route::controller('users', 'UsersController');

Controller (in this case UsersController.php):

public function getIndex()
{
    // routed from GET request to /users
}

public function getProfile()
{
    // routed from GET request to /users/profile
}

public function postProfile()
{
    // routed from POST request to /users/profile
}

public function getPosts($id)
{
    // routed from GET request to: /users/posts/42
}

As The Shift Exchange mentioned, there are some benefits to doing it the verbose way. In addition to the excellent article he linked, you can create a name for each route, for example:

Route::get("users", array(
    "as"=>"dashboard",
    "uses"=>"UsersController@getIndex"
));

Then when creating urls in your application, use a helper to generate a link to a named route:

$url = URL::route('dashboard');

Links are then future proofed from changes to controllers/actions.

You can also generate links directly to actions which would still work with automatic routing.

$url = URL::action('UsersController@getIndex');

Leave a Comment