php laravel 5.2 如何在刀片中获取路由参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39011648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
laravel 5.2 How to get route parameter in blade?
提问by msonowal
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.
这是我的网址,
http://project.dev/blogs/image-with-article
所以,在这里我需要image-with-article
刀片中的参数来显示这是一个名为 slug 的参数在我的路由文件中我需要刀片中的 slug 参数。
Route::get('/blogs/{slug}', ['as'=>'blog.by.slug', 'uses'=> 'CmsController@show']);
回答by Joel Hinz
I'm not sure what you mean. If you're trying to construct the route in a Blade template, use
我不确定你是什么意思。如果您尝试在 Blade 模板中构建路由,请使用
<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') }}
回答by Ivan Z.
If you want to get the parameters without using the controller method
如果你想在不使用控制器方法的情况下获取参数
{{dd(request()->route()->parameters)}}