Laravel路线控制器

时间:2016-08-11 12:53:59

标签: php laravel

我在routes.php中有一个有效的解决方案,但我知道laravel可以更好地处理休息路线。我已经尝试使用他们的文档来实现宁静的资源控制器,但没有运气。

这就是我现在所拥有的

Route::get('/invoices', 'InvoicesController@showInvoices');
Route::get('/invoices/data', 'InvoicesController@getInvoices');

基本上,showInvoices返回发票视图,getInvoices返回DataTables的JSON字符串,该字符串从发票视图中调用。

所以我希望能够调用/发票来获取视图,然后使用JavaScript调用/发票/数据。

有关如何将其转换为资源控制器或更合适的控制器的任何建议吗?

2 个答案:

答案 0 :(得分:0)

是的,有一种更清洁的方式。路由控制器支持Laravel 5.3。然后删除了这个功能,转而使用显式路由,这样当你有很多路由时,路由文件会处于混乱状态。

幸运的是,我写了一个名为AdvancedRoute的课程,它可以作为替代品。

在您的情况下,您可以像这样使用它:

Route::get('/invoices', 'InvoicesController@showInvoices');
Route::get('/invoices/data', 'InvoicesController@getInvoices');

变为:

AdvancedRoute::controller('/invoices', 'InvoicesController');

为您自动构建显式路由。请记住,您必须通过在方法名称前加上请求方法来遵循约定,我个人觉得这非常干净且开发人员友好:

InvoicesController@getInvoices => /invoices
InvoicesController@getInvoicesData => /invoices/data

完整信息如何在GitHub仓库中安装和使用find:

https://github.com/lesichkovm/laravel-advanced-route

希望你觉得这很有用。

答案 1 :(得分:0)

您可以像这样创建“资源”路线:

Route::resource('/invoices', 'InvoicesController');

这将为特定的/invoices路由/资源提供RESTful路由(GET,POST,PUT等)。您可以通过执行php artisan route:list

来检查这一点

您可以了解更多here

我希望这会有所帮助。

干杯!

相关问题