如何知道 Laravel 6 中 blade 文件中的控制器和方法名称?

How to know the controller and method name inside blade file in Laravel 6?

我是 Laravel 的新手。我正在练习在 Laravel 6 中建立博客。我有一个 layout.blade.php 文件,其中写入了所有基本的 HTML。在 layout.blade file 的内容部分,我想根据控制器和方法名称 @include 其他 blade 文件。

我有 2 个控制器,HomeControllerArticleController。在 HomeController 文件中,有一个方法 index(),其中 returns 视图 home.blade.php。同样,home.blade.php @extends layout.blade.php。现在我想知道哪个控制器和方法调用了 layout.blade.php 文件中的视图文件。

我想要这样的东西-

<!-- This is layout.blade.php file -->
<html>
<head>
</head>
<body>
  @if (Controller == HomeController AND Method == index)
    @include('home')
  @endif

  @if (Controller == ArticleController AND Method == index)
    @include('articles')
  @endif
</body>
</html>

我在 Google 上没有找到任何答案。我在 Whosebug 中遇到了一些问题,但他们的答案非常混乱,而且那些版本也较旧。

谢谢。

编辑 1:

  1. 由于信誉评分低,我无法在评论中回复。如我一般 新的,我知道blade的一些基本标签,所以如果还有其他的 实现这个的解决方案,请与我分享。
  2. 我找到了 在提出这个问题之前。但我不认为它回答了我的问题 问题。
  3. 我知道 Controller == HomeController AND Method == index 很疯狂。 我只是说明了我需要什么。我知道基本的 @yield@section 标签,但我不想在我的案例中使用它。因为,如果我做一个 card/home 部分完全在不同的文件中,我可以调用它 其他文件稍后。如果我使用 @yield,我将不得不使用 @section 还有不同的文件,我不想这样做。

如果我没理解错的话,你不需要做这种疯狂的事情

Controller == HomeController AND Method == index

如果您只想设置母版页。 Laravel 让它变得非常简单。

这是主页。

<!-- Stored in resources/views/layouts/app.blade.php -->

<html>
    <head>
        <title>App Name</title>
    </head>
    <body>
        <div class="container">
            @yield('content')
        </div>
    </body>
</html>

对于其他页面

<!-- Stored in resources/views/index.blade.php -->

@extends('layouts.app')

@section('content')
    <p>This is my body content. From index page.</p>
@endsection

对于控制器,您只需 return blade.php 查看文件名。

    public function index()
    {
        return view('index');
    }

Route::currentRouteAction() 方法似乎是你想要的。它在 api 文档中。

string|null currentRouteAction()

Get the current route action.

Return Value string|null

https://laravel.com/api/6.x/Illuminate/Routing/Router.html#method_currentRouteAction

我运行这个就知道控制器了。你可以试试。

{{ dd(request()->route()->getAction()) }}