dsvd407787736 2017-06-07 12:37
浏览 52
已采纳

如何使用相同的控制器对类似的Laravel路由进行分组?

I want to pretty-up my routes, ie, I have such entries:

// DataTable
Route::get('dt/reservations/{room_id]', 'DataTablesController@reservations')->where(['room_id', '[0-9]+']);
Route::get('dt/rooms/{area_id]', 'DataTablesController@rooms')->where(['area_id', '[0-9]+']);
Route::get('dt/departments', 'DataTablesController@departments');
Route::get('dt/addresses', 'DataTablesController@areas');
Route::get('dt/areas', 'DataTablesController@areas');

I would like to make it more understandable. I can add prefix what would give me:

// DataTable
Route::group(['prefix' => 'dt'], function () {
    Route::get('reservations/{room_id]', 'DataTablesController@reservations')->where(['room_id', '[0-9]+']);
    Route::get('rooms/{area_id]', 'DataTablesController@rooms')->where(['area_id', '[0-9]+']);
    Route::get('departments', 'DataTablesController@departments');
    Route::get('addresses', 'DataTablesController@areas');
    Route::get('areas', 'DataTablesController@areas');
});

But can I somehow make the rest too? The route name and method name will always be the same. Is it possible to make something like:

// DataTable
Route::group(['prefix' => 'dt'], function () {
    Controller => DataTablesController,
    Methods => [
        'reservations',
        'rooms',
        'departments',
        'addresses',
        'areas'
    ];
});
  • 写回答

1条回答 默认 最新

  • dscizpq790832708 2017-06-07 12:50
    关注

    Although a very good feature. But it can't be done in Laravel

    All your routes must be explicit, Laravel won't/can't assume that you are using same controller for all the routes. So you will have to define all the routes explicitly.

    Only Resource Controllers can have implicit routing in Laravel

    Take a look here....

    Route use the same controller

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化