Home  >  Article  >  Backend Development  >  laravel5 在模板中如何不通过控制器传送变量方式获取访问的路由?

laravel5 在模板中如何不通过控制器传送变量方式获取访问的路由?

WBOY
WBOYOriginal
2016-06-06 20:25:051198browse

如题,我知道在controller里可以得到,但是,能否在blade模板中直接函数的方式取得访问的路由或者方法名等。laravel提供类似的方法了吗?

回复内容:

如题,我知道在controller里可以得到,但是,能否在blade模板中直接函数的方式取得访问的路由或者方法名等。laravel提供类似的方法了吗?

方法还是有的,但是这种做法不应该被鼓励,你可以通过@inject的语句直接获得(注入)服务

例子

<code class="blade">@inject('request', 'Illuminate\Http\Request')

request method: {{ $request->getMethod() }}</code>

文档
http://laravel.com/docs/5.1/blade#service-injection

可以。直接写一个函数

<code>function getCurrentRouteMethod(){
    return app('request')->getMethod();
}</code>

在模板里直接调用

<code>{{ getCurrentRouteMethod() }}</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn