I'm more or less new to Laravel 4. I've never used routes before but normally what I'm used to is url/controller/action and then the backend routing for me. I've read the documentation for routes and controllers a few times as well as read through some tutorials and so, I'm trying to figure out how to get this to work without writing a route for every controller and action.
I tried something like
Route::get('{controller}/{action}', function($controller, $action = 'index'){
return $controller."@".$action;
});
Now then, I know this is wrong since it doesn't work, but what am I missing? On most tutorials and stuff I'm seeing an route for more or less every controller and action like:
Route::get('/controller/action' , 'ControllerName@Action');
Which seems silly and like a waste of time to me.
Is there anyway to achieve what I want?
I come from .Net world and routing is typically done:
Which looks like:
In Laravel I accomplish this routing like so:
The controller would look roughly like so:
If you are looking for a more automated routing, this would be the Laravel 4 way:
Route:
Controller (in this case UsersController.php):
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:
Then when creating urls in your application, use a helper to generate a link to a named route:
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.