Laravel 5.3+ 中的 Route::controller() 替代方案
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39258923/
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
Route::controller() alternative in Laravel 5.3+
提问by Artur Grigio
I just upgraded from Laravel 5.2 to 5.3. I am using Laravel-DataTablespackage for several tables in my application.
我刚刚从 Laravel 5.2 升级到 5.3。我在我的应用程序中为多个表使用Laravel-DataTables包。
After upgrade when I run artisan serve
I'm receiving:
运行升级后,我artisan serve
收到:
[BadMethodCallException]
Method controller does not exist.
I've tracked the issue down to this piece of code in my routes.php
(now web.php
)
我已经在我的routes.php
(现在web.php
)中将问题追溯到这段代码
Route::controller('datatables', 'ProfileController', [
'anyOrders' => 'datatables.dataOrders',
'anyProperties' => 'datatables.dataProperties',
]);
This is the suggested way to route the queries for DataTables Documentation.
这是路由DataTables Documentation查询的建议方法。
Was the Route::controller()
deprecated, and what is the alternative to for these routes?
是否Route::controller()
已弃用,这些路线的替代方案是什么?
回答by
The explicit routes
will be:
明确的routes
将是:
Route::get('/datatables/orders', array('middleware' => 'auth', 'uses' => 'ProfileController@anyOrders'))->name('datatables.dataOrders');
Route::get('/datatables/properties', array('middleware' => 'auth', 'uses' => 'ProfileController@anyProperties'))->name('datatables.dataProperties');
回答by Milan
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.
这就是为什么我写了一个名为 AdvancedRoute 的类,作为替代品。
It can be used by simply replacing Route::controller with AdvancedRoute::controller like this:
它可以通过简单地用 AdvancedRoute::controller 替换 Route::controller 来使用,如下所示:
AdvancedRoute::controller('users','UserController');
Full information how to install and use find at the GitHub repo at:
有关如何在 GitHub 存储库中安装和使用 find 的完整信息,请访问:
https://github.com/lesichkovm/laravel-advanced-route
https://github.com/lesichkovm/laravel-advanced-route
Hope you find this useful.
希望您觉得这个有帮助。
回答by ceejayoz
https://laravel.com/docs/5.3/upgrade#upgrade-5.3.0
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.
以下功能在 5.2 中已弃用,并将在 2016 年 6 月的 5.3 版本中删除:
Route::controller
不推荐使用隐式控制器路由。请在您的路由文件中使用显式路由注册。这很可能会被提取到一个包中。
回答by Fernce Borcena
You can use resource().
您可以使用资源()。
Route::resource('users','UserController');
Note: the "get" prefix is not needed. getIndex()
= index()
注意:不需要“get”前缀。getIndex()
=index()