anon12822's avatar

Implicit Controllers?

I'm just wondering here what the general opinion is with implicit controllers, compared to explict or RESTful controllers?

0 likes
3 replies
bobbybouwmann's avatar
Level 88

Implicit Controllers

Well the idea about implicit controllers is that you can create any function and map that to an action, so you don't have to define them in your routes.php. I think this makes it unbelievable unreadable since you need to take a look to all controllers to know what's going on! Jeffrey also got a good video on it: https://laracasts.com/lessons/say-no-to-implicit-routing

Explicit Controllers

Explicit routing is pointing every action(GET, POST) to a function of a controller. This is the best way to define your routes in my opinion. Using it like this you create the documentation and the sitemap of your application in one file ;)

Route::get('users', 'UsersController@index');
Route::get('users/ajax', 'UsersController@ajax');

RESTful Routing

RESTful routing is something everyone should do. This lays between implicit and explicit routing. You can define a complete resource but that gives you less documentation as well. What happens when you don't have a show method for that resource or not an index method for that resource. Then Route::resource('users', 'UsersController') wouldn't work anymore since you expect all the basics methods, but they might not exist! Laravel has a solution to this to use something like this

Route::resource('users', 'UsersController', ['except' => ['show', 'index']]);

This method works fine for simple resources, but if you need to add extra methods you end up using explicit routes anyway,

My Conclusion

Never use Route::controller, routes.php is your documentation of your application. With Route::controller you hide what it does. This does not apply to Route::resource because it is defined what it can do and what not. I would always go for explicit routing but use RESTful routing in the back of my head! My routes.php file look like this

Route::get('users',     ['as' => 'users', 'uses' => 'UsersController@index']);
Route::get('user/create', ['as' => 'user.create', 'uses' => 'UsersController@create']);
Route::post('users', ['as' => 'user.store', 'uses' => 'UsersController@store']);
Route::get('user/{id}', ['as' => 'user.show', 'uses' => 'UsersController@show']);
Route::get('user/{id}/edit', ['as' => 'user.edit', 'uses' => 'UsersController@edit']);
Route::patch('user/{id}', ['as' => 'user.update', 'uses' => 'UsersController@update']);
Route::delete('user/{id}', ['as' => 'user.delete', 'uses' => 'UsersController@delete']);

It's a combination of explicit routing but with the power of RESTful routing! I also could have used Route::resource('users', 'UsersController') but I think this is clearer ;)

I hope this answers your question ;)

4 likes
ChristArt's avatar

This may work will for entry level websites, but with larger sites the routes file(s) start to look like the migration folder. I would rather use Implicit routing when grouping of logic allows.

Please or to participate in this conversation.