Home » Php » Route::controller() alternative in Laravel 5.3+

Route::controller() alternative in Laravel 5.3+

Posted by: admin November 29, 2017 Leave a comment

Questions:

I just upgraded from Laravel 5.2 to 5.3. I am using Laravel-DataTables package for several tables in my application.

After upgrade when I run artisan serve I’m receiving:

[BadMethodCallException]
Method controller does not exist.

I’ve tracked the issue down to this piece of code in my routes.php (now web.php)

Route::controller('datatables', 'ProfileController', [
    'anyOrders'  => 'datatables.dataOrders',
    'anyProperties' => 'datatables.dataProperties',
]);

This is the suggested way to route the queries for DataTables Documentation.

Was the Route::controller() deprecated, and what is the alternative to for these routes?

Answers:

The explicit routes will be:

Route::get('/datatables/orders', array('middleware' => 'auth', 'uses' => '[email protected]'))->name('datatables.dataOrders');
Route::get('/datatables/properties', array('middleware' => 'auth', 'uses' => '[email protected]'))->name('datatables.dataProperties');

Questions:
Answers:

I had the same issue as you, and none of the alternatives (explicit declaration or publishing) was good enough. There were also some alternatives which required changing too much code.

This is why I wrote a class called AdvancedRoute, which serves as a drop in replacement.

It can be used by simply replacing Route::controller with AdvancedRoute::controller like this:

AdvancedRoute::controller('users','UserController');

Full information how to install and use find at the GitHub repo at:

https://github.com/lesichkovm/laravel-advanced-route

Hope you find this useful.

Questions:
Answers:

https://laravel.com/docs/5.3/upgrade#upgrade-5.3.0

The following features are deprecated in 5.2 and will be removed in the 5.3 release in June 2016:

  • Implicit controller routes using Route::controller have been deprecated. Please use explicit route registration in your routes file. This will likely be extracted into a package.
Questions:
Answers:

You can use resource().

Route::resource('users','UserController');

Note: the “get” prefix is not needed. getIndex() = index()