laravel 5.2如何获取刀片中的路径参数? [英] laravel 5.2 How to get route parameter in blade?

查看:41
本文介绍了laravel 5.2如何获取刀片中的路径参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的网址 http://project.dev/blogs/image-with-article 所以,这里我需要参数image-with-article 在我的刀片中显示一个名为slug的参数,这是在我的路由文件中,我需要刀片中的slug参数.

this is my url http://project.dev/blogs/image-with-article so, here I need the parameter image-with-article in my blade to display which is a parameter named slug here is in my routes file I need the slug paramter in blade.

Route::get('/blogs/{slug}', ['as'=>'blog.by.slug', 'uses'=> 'CmsController@show']);

推荐答案

我不确定您的意思.如果您尝试在Blade模板中构造路线,请使用

I'm not sure what you mean. If you're trying to construct the route in a Blade template, use

<a href="{{ route('blog.by.slug', ['slug' => 'someslug']) }}">...</a>

如果您尝试访问给定的参数,建议您从控制器传递该参数:

If you're trying to access the given parameter, I would suggest passing it from the controller:

// CmsController
public function show($slug)
{
    // other stuff here
    return view('someview', compact('slug'));
}

// someview.blade.php
{{ $slug }}

如果您真的需要从视图访问参数而无需先从控制器发送它,则不应该这样做,但是您可以使用外观:

And if you really need to access the parameter from the view without first sending it from the controller... you really shouldn't, but you can use the facade:

{{ Request::route('slug') }}

这篇关于laravel 5.2如何获取刀片中的路径参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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