Laravel 5 获取视图名称 [英] Laravel 5 get view name

查看:37
本文介绍了Laravel 5 获取视图名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力获取 L5 中的视图名称.就像在 WP 中一样,我想为样式添加一个特定的页面名称(视图名称),如下所示:

<div id="page" class="page-login"><h1>Inloggen</h1>

<!-- 视图名称:register.blade.php !--><div id="page" class="page-register"><h1>注册人</h1>

在 L4 中,可以使用 composer 在所有视图中共享 var (如何在 Laravel 4 的主图层中获取当前视图名称?).但我的主布局只需要一次视图名称.

这样做:

给我以下错误调用未定义的方法IlluminateViewFactory::getName().

提前致谢!

解决方案

通过向引导方法添加视图编写器并使用*"与所有视图共享来更新您的 AppServiceProvider:

composer('*', function($view){$view_name = str_replace('.', '-', $view->getName());view()->share('view_name', $view_name);});}/*** 注册任何应用服务.** @return 无效*/公共函数 register(){//}}

{{$view_name}} 将可用于您的刀片模板.

I'm struggling to get the view name in L5. Just as in WP, I'd like to add a specific page name (view name) for styling, like so:

<!-- View name: login.blade.php !-->
<div id="page" class="page-login">
    <h1>Inloggen</h1>
</div>

<!-- View name: register.blade.php !-->
<div id="page" class="page-register">
    <h1>Registreren</h1>
</div>

In L4 it can be done using composer to share the var across all views (How can I get the current view name inside a master layour in Laravel 4?). But I only need the view name once for my master layout.

Doing this:

<div id="page" class="page-{{ view()->getName() }}">

Gives me the following error Call to undefined method IlluminateViewFactory::getName().

Thanks in advance!

解决方案

Update your AppServiceProvider by adding a view composer to the boot method and using '*' to share it with all views:

<?php

namespace AppProviders;

use IlluminateSupportServiceProvider;    

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        view()->composer('*', function($view){
            $view_name = str_replace('.', '-', $view->getName());
            view()->share('view_name', $view_name);
        });

    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

{{$view_name}} will be made available to your blade templates.

这篇关于Laravel 5 获取视图名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆