Use automatic controller routes in Laravel is a bad idea

假如想象 提交于 2019-12-05 09:40:58

Yes this is bad.

Controller::detect() is actually not present in Laravel 4 because it is a bit broken.

detect() will go through your filesystem and return controller files, but this is a bad idea because the order you define your routes matters. If you have any nested controllers you will find this breaking very easily.

detect() will also return files in a different order depending on the file system, so this leads to a lot of unpredictability.

I would argue that you should define all your routes any ways, it is a lot easier to read and debug.

One of interesting things about Laravel that CI does not have is that for certain pages, you can route directly to the view without needing a controller at all. Think about static pages like 'About Us'. CodeIgniter would need you to set up a controller + view for that, even though the controller will do barely anything. In case of Laravel, you can route directly to a view in this case.

Setting up routes manually will allow you to set these short-circuited routes.

Automatic detection is a bad idea.

You can use routes or use Route::controller('mycontroller') or and array of controllers like Route::controller(array('mycontroller', mycontroller2');

Then you get the benefit, without the autodetect.

in laravel 4 :

you can use Restful Controller like documentation http://laravel.com/docs/controllers#restful-controllers

But Route::controller() must take two parameters as minimum requirement first parameter stands for URL respond to ,and second parameter is name of controller

also

you can write third parameter into Route::controller() is an array with names of actions (name of action with HTTP verb )and routes names for this actions

ex:

Route::controller('users','UsersController',array(
            'getUsers' =>"listUsers" , 
           ));

route name for getUsers action is listUsers

Below is a good example to follow for CRUD and general purpose routing

type php arisan controller:make SampleController

edit routes.php and add

Route::resource('sample', 'SampleController'); 

Then type php artisan routes to show the newly created routes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!