Monday, May 15, 2017

[Laravel5] Route : Attribute [controllers] does not exist.


Error message

InvalidArgumentException in RouteRegistrar.php line 75:
Attribute [controllers] does not exist.

Source Code: 

Route::controllers([
    'lists' => 'ListsController',
    'tasks' => 'TasksController',
]);

Reason

Route::controllers method (Implicit controller) defining series of URIs and associated controllers, reflection is used parse and register all of the controller’s routable methods. However, this controller method in route is deprecated since 5.2 (or 5.3, i can't sure) but it works at 5.1 (according to user: SaeedPrez at https://laracasts.com/discuss/ and RïshïKêsh Kümar ay https://stackoverflow.com),  check your laravel version.


Method 1

You can use this laravel routes publisher package to replace deprecated Route::controller() and Route::controllers() with explicit routes:
https://github.com/themsaid/laravel-routes-publisher.

Method 2

You can use the resource method (RESTful Resource controller) which get similar purpose with controller method (Implicit controller):

https://stackoverflow.com/questions/23505875/laravel-routeresource-vs-routecontroller

Example code

Using Route::resource method:
Route::resource('lists','ListsController');
Route::resource('tasks','TasksController');
but you also need to change the controllers method name such as something like:

From :
<?php namespace example\Http\Controllers;
   
    class ListsController extends Controller {

        public function getIndex()
        {
            return view('lists.index');
        }

        public function getCreate()
        {
            return view('lists.create');
        }
       
        public function postStore()
        {
            return view('lists.store')
        }
    }
?>
To:
<?php namespace example\Http\Controllers;
    class ListsController extends BaseController {

    public function index() {
        return view('lists.index');
    }

    public function create()
    {
        return view('lists.create');
    }

    public function store()
    {
        return view('lists.store')
    }

}
For detail of RESTful Resource controller and Implicit controller, you can reference from the answer at stackoverflow.com by RyanWinchester :
https://stackoverflow.com/questions/23505875/laravel-routeresource-vs-routecontroller 
and resource controller doc with laravel 5.4 :
https://laravel.com/docs/5.4/controllers#resource-controllers

Reference

https://laracasts.com/discuss/channels/laravel/implicit-controllers-laravel-54
https://laracasts.com/discuss/channels/laravel/routecontrollers-in-laravel-54
https://stackoverflow.com/questions/43572635/php-laravel-attribute-controller-does-not-exist

No comments :

Post a Comment