Use automatic controller routes in Laravel is a bad idea

丶灬走出姿态 提交于 2020-01-02 04:19:07

问题


I'm coming From CodeIgniter to Laravel.

So, is a bad idea using automatic routes to all of controllers?

Route::controller(Controller::detect());

Should I use this instead creating routes in routes.php?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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




回答5:


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



来源:https://stackoverflow.com/questions/15231107/use-automatic-controller-routes-in-laravel-is-a-bad-idea

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