gpt4 book ai didi

php - 如何在 Laravel 5 中检索 View 名称?

转载 作者:行者123 更新时间:2023-12-02 17:15:02 25 4
gpt4 key购买 nike

我尝试了多种方法,包括此处的热门帖子:Add View Name to CSS Class on Body

但它提取的是主模板,而不是子模板名称。我使用@yield 和@extends 等。所以目前这就是我的文件系统的一部分在我看来的样子:

-Views
--Layouts
---=master.blade.php
--Shipments
---=index.blade.php

所以我的 shipments.index 文件将@extends('layouts.master'),这样我就没有那么多冗余了。但是,引用上面的帖子,我得到以下结果:

<div id="page" class="layouts.master">

这不是我想要的,我至少更喜欢“shipments.index”,我不介意它是否更长,我只是想识别它。

最佳答案

您可以使用 Laravel View Composers

View composers are callbacks or class methods that are called when a view is rendered. If you have data that you want to be bound to a view each time that view is rendered, a view composer can help you organize that logic into a single location.

在文件app/Providers/AppServiceProvider.php中,编辑public function boot()方法

/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(Application $app)
{
view()->composer('*', function($view) {
view()->share('view_name', $view->getName());
});
}

在此之后,在任何 Blade 模板中,您现在都可以访问变量

{{ $view_name }}

关于php - 如何在 Laravel 5 中检索 View 名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46852974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com