在 Laravel 中使用自动控制器路由是个坏主意
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15231107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use automatic controller routes in Laravel is a bad idea
提问by Leabdalla
I'm coming From CodeIgniter to Laravel.
我要从 CodeIgniter 转到 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?
我应该使用它来代替在 routes.php 中创建路由吗?
回答by Deinumite
Yes this is bad.
是的,这很糟糕。
Controller::detect() is actually not present in Laravel 4 because it is a bit broken.
Controller::detect() 实际上在 Laravel 4 中不存在,因为它有点损坏。
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() 将遍历您的文件系统并返回控制器文件,但这是一个坏主意,因为您定义路由的顺序很重要。如果您有任何嵌套的控制器,您会发现这很容易破坏。
detect() will also return files in a different order depending on the file system, so this leads to a lot of unpredictability.
detect() 还会根据文件系统以不同的顺序返回文件,因此这会导致很多不可预测性。
I would argue that you should define all your routes any ways, it is a lot easier to read and debug.
我认为您应该以任何方式定义所有路由,它更易于阅读和调试。
回答by xbonez
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.
CI 没有的关于 Laravel 的有趣的事情之一是,对于某些页面,您可以直接路由到视图,而根本不需要控制器。想想像“关于我们”这样的静态页面。CodeIgniter 需要你为此设置一个控制器 + 视图,即使控制器几乎不会做任何事情。在 Laravel 的情况下,在这种情况下,您可以直接路由到视图。
Setting up routes manually will allow you to set these short-circuited routes.
手动设置路由将允许您设置这些短路路由。
回答by user1577402
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');
您可以使用路由或使用 Route::controller('mycontroller') 或控制器数组,如 Route::controller(array('mycontroller', mycontroller2');
Then you get the benefit, without the autodetect.
然后您将获得好处,无需自动检测。
回答by ahmed hamdy
in laravel 4 :
在 Laravel 4 中:
you can use Restful Controller like documentation http://laravel.com/docs/controllers#restful-controllers
您可以使用 Restful Controller 之类的文档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
但
Route::controller()
必须以两个参数为最低要求,第一个参数代表 URL 响应,第二个参数是控制器名称
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
您可以将第三个参数写入Route::controller()
一个包含操作名称的数组(带有 HTTP 动词的操作名称)并为此操作路由名称
ex:
前任:
Route::controller('users','UsersController',array(
'getUsers' =>"listUsers" ,
));
route name for getUsers action is listUsers
getUsers 操作的路由名称是 listUsers
回答by Ryu_hayabusa
Below is a good example to follow for CRUD and general purpose routing
下面是 CRUD 和通用路由的一个很好的例子
type php arisan controller:make SampleController
类型 php arisan controller:make SampleController
edit routes.phpand add
编辑routes.php并添加
Route::resource('sample', 'SampleController');
Then type
php artisan routes
to show the newly created routes
然后键入
php artisan routes
以显示新创建的路由