0

I am new to Laravel. I was practicing setting up a blog in Laravel 6. I have a layout.blade.php file where all the basic HTML is written. In the content section of the layout.blade file, I want to @include other blade file depending on the controller and method name.

I have 2 controllers, HomeController and ArticleController. In HomeController file, there is a method index() which returns the view home.blade.php. Again, home.blade.php @extends layout.blade.php. Now I want to know which controller and method called the view file inside from layout.blade.php file.

I want something like this-

<!-- 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>

I didn't find any answer on Google. I got some questions in StackOverflow, but they have very confusing answers and those versions are older also.

Thank you.

EDIT 1:

  1. Due to the low reputation score, I cannot reply in comments. As I am new, I know some basic tags of blade, so if there is any other solution to achieve this, please share it with me.
  2. I found Get Laravel 5 controller name in view before making this question. But I don't think it answers my question.
  3. I know Controller == HomeController AND Method == index is crazy. I just illustrated what I need. I know basic @yield and @section tags, but I don't want to use this in my case. Because, if I make a card/home section entirely in a different file, I can call it in other files later. If I use @yield, I will have to use @section in different files also, which I don't want to do.
Khalilullah
  • 89
  • 4
  • 10
  • 1
    I think `Route::currentRouteAction()` should do the trick but your approach seems weird to me. Maybe a blade `@stack` could suit your needs better. – IGP Apr 08 '20 at 06:15
  • Does this answer your question? [Get Laravel 5 controller name in view](https://stackoverflow.com/questions/29549660/get-laravel-5-controller-name-in-view) – Yasin Patel Apr 08 '20 at 06:18
  • @IGP, please share it as an answer. This is what I wanted. – Khalilullah Apr 08 '20 at 06:40
  • @YasinPate, I found the question before making this question, but sadly it doesn't answer my question. – Khalilullah Apr 08 '20 at 06:41
  • @S.M.Khalilullah, instead of checking controller and action, directly check with route url by : if(Request::url() === 'your url here') – Yasin Patel Apr 08 '20 at 06:47

3 Answers3

2

Route::currentRouteAction() method seems to be what you want. It's in the api documentation.

string|null currentRouteAction()

Get the current route action.

Return Value string|null

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

IGP
  • 14,160
  • 4
  • 26
  • 43
0

if I understand you correctly, you do not need to do this crazy thing like this

Controller == HomeController AND Method == index

If all you want is to setup a master page. Laravel makes it really easy.

This is the master page.

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

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

For other pages

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

@extends('layouts.app')

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

And for the controller, you just return the blade.php view file name.

    public function index()
    {
        return view('index');
    }
Andy Song
  • 4,404
  • 1
  • 11
  • 29
  • Thanks. But I don't want to use yield-section. Rather, I want to use something like include because if I make a file for a specific section, I can include it anywhere without defining the section. – Khalilullah Apr 08 '20 at 06:43
-1

I run this one to know the controller. You can try it.

{{ dd(request()->route()->getAction()) }}
Atiqur
  • 1
  • 2