Laravel 5.2+ 获取当前路径对象及参数

版权声明:尊重原创喔,转载请注明 https://blog.csdn.net/lgyaxx/article/details/79286911

view中,我们经常需要获取当前路径及其参数,例如添加导航高亮。

Laravel 5.2+版本开始,我们可以使用Route Facade来获取当前的路径对象(Route Object),然后我们可以通过该对象获取我们需要的参数及路径:

    $route = Route::current(); //获取当前路径对象
    $uri = $route->uri(); //获取当前路径URI,例如'articles/{id}'
    $name = $route->getName(); //等同于$name = Route::currentRouteName();
    $param = $route->parameter('id'); //获取当前路径传入的名为id的参数

注意,以上$route->uri()获取的URI中,若路径含参,那么URI的形式为path/{parameter},而要获取具体的参数,必须调用$route->parameter('parameter_name')

猜你喜欢

转载自blog.csdn.net/lgyaxx/article/details/79286911
今日推荐