如何使用包含斜杠字符的参数定义Laravel路线 [英] How to define a Laravel route with a parameter that contains a slash character

查看:279
本文介绍了如何使用包含斜杠字符的参数定义Laravel路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个包含斜线/字符的参数定义一条路由,就像example.com/view/abc/02一样,其中abc/02是参数.

I want to define a route with a parameter that will contain a slash / character like so example.com/view/abc/02 where abc/02 is the parameter.

如何防止Laravel读取斜线作为下一个route参数的分隔符?因此,我现在得到一个404 not found error.

How can I prevent Laravel from reading the slash as a separator for the next route parameter? Because of that I'm getting a 404 not found error now.

推荐答案

将下面的全部路由添加到routes.php的底部,并记住之后再运行composer dump-autoload.请注意,使用-> where"来指定参数的可能内容,从而使您能够使用包含斜杠的参数.

Add the below catch-all route to the bottom of your routes.php and remember to run composer dump-autoload afterwards. Notice the use of "->where" that specifies the possible content of params, enabling you to use a param containing a slash.

//routes.php
Route::get('view/{slashData?}', 'ExampleController@getData')
    ->where('slashData', '(.*)');

比起在控制器中,您只需像平常一样处理数据(就像它不包含斜杠一样).

And than in your controller you just handle the data as you'd normally do (like it didnt contain the slash).

//controller 
class ExampleController extends BaseController {

    public function getData($slashData = null)
    {
        if($slashData) 
        {
            //do stuff 
        }
    }

}

这应该为您工作.

此外,您在这里还获得了有关路由参数的详细Laravel文档:[文档]

Additionally, here you have detailed Laravel docs on route parameters: [ docs ]

这篇关于如何使用包含斜杠字符的参数定义Laravel路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆