douyao6842 2017-12-05 08:12
浏览 47
已采纳

Laravel Route基于查询字符串

Currently in my routes.php file I've got the following:

Route::get('import', 'Controller@import');

But as for the requests they'd be sent in the following way:

/import?flag=0&data={} //flag could be anything from 0-4 & data is json encoded info
/import?flag=5&data={}
/import?flag=6&data={}

These are the only 3 requests that I'd have, I'm aware that Laravel supports where method on the route but only regular expressions but not on query parameters, is there a solution for this that I'm overlooking?

The expression method would be something like so:

Route::get('import?flag={id}&data={data}', 'Controller@importFlag0-4')->where('id', '<=', '4');
Route::get('import?flag={id}&data={data}', 'Controller@importFlag5')->where('id', '5');
Route::get('import?flag={id}&data={data}', 'Controller@importFlag6')->where('id', '6');
  • 写回答

2条回答 默认 最新

  • dounai6626 2017-12-05 08:36
    关注

    As mentioned by @lagbox, why would you need different routes, when you could handle all those routes conditionals in the import() method

    public function import(Request $request)
    {
        $flag = $request->get('flag');
        $data = $request->get('data');
        if ($flag <= 4) {
            $this->importFlagTillFour($flag, $data)
        }
        if ($flag == 5) {
            $this->importFlagFive($flag, $data)
        }
        if ($flag == 6) {
            $this->importFlagSix($flag, $data)
        }
    }
    

    and declare the called methods in the same class

    public function importFlagTillFour($flag, $data) {}
    public function importFlagFive($flag, $data) {}
    public function importFlagSix($flag, $data) {}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗