duanping5306 2016-05-16 06:54
浏览 36
已采纳

从路径传递变量到控制器

I have this route.php:

Route::group(['prefix' => 'v3/page1'], function()
{
    Route::get('page1', 'TestController@page1');
});

Route::group(['prefix' => 'v4/page1'], function()
{
    Route::get('page1', 'TestController@page1');
});

As you can see, there are 2 groups that have the same routes. The only difference is that the prefix is slightly different for each group.

I need a way to pass data from route to controller. In this case Im only interested in passing the "v3" or "v4"-string from route to controller.

I have read a little about before_filter. But Im not sure if it is the right way to go. I can imagine that a solution could be to extract the url (maybe in the constructor for the controller) and from there understand if the prefix is v3 or v4. But I wonder if there is a better way, more a best practice. Maybe something with before_filter?

  • 写回答

3条回答 默认 最新

  • dongqufi82315 2016-05-16 07:04
    关注

    You can try something like:

    Route::group(['prefix' => '{version}/page1'], function(){
        Route::get('page1', 'TestController@page1');
    })->where('version', 'v[3|4]');
    

    In your controller you can get the version by $request->version

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度