PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, November 15, 2022

[FIXED] How to change which template is extended based on user authentication

 November 15, 2022     laravel, laravel-6, laravel-blade     No comments   

Issue

I am using Laravel 6.20.11, and I have published the Laravel blade error templates. What I need to have happen is that if the user is authenticated, the error message extends the internal-only layout, and if not, it extends the public layout like so:

@auth
    @extends ('int-layouts.partials.wrapper')
    @section ('error')
        <div class="be-content">
            <div class="main-content container-fluid">
                <div class="container py-5">
                    <div class="row">
                        <div class="col-lg-8">
                            <div class="text-block">
                                 <h1>404 - Not Found</h1>
                                  <p class="text-muted text-uppercase mb-4">Sorry, we couldn't find the page you were looking for.
                                  Please contact the administrator<br/>
                                  Or go back to the <a class="navbar-brand py-1" href="/controlpanel/">Dashboard</a></p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    @endsection
@endauth
@guest
    @extends ('ext-layouts.master')
    @section('error')
        <div class="container py-5">
            <div class="row">
                <div class="col-lg-8">
                    <div class="text-block">
                         <h1>404</h1>
                          <p class="text-muted text-uppercase mb-4">Sorry,You are trying to reach the page that does not exist.<br>
                           Please contact the administrator <br/> Or go back to the <a class="navbar-brand py-1" href="/">Home Page</a></p>
                    </div>
                </div>
            </div>
        </div>
    @endsection
@endguest

The problem I am having is that when I test the 404 error using abort(404) or by trying to access a route that doesn't exist, the error message displays twice and the page inherits the int-layouts.partials.wrapper even when the user isn't authenticated.

Is this expected behavior or am I doing something wrong in my blade file?


Solution

You can try to use a conditional in one @extends directive:

@extends (auth()->check() ? 'int-layouts.partials.wrapper' : 'ext-layouts.master')

Then you can use a conditional after this inside your section.

@section('error')
    @auth
    @else
    @endif
@endsection

The @extends directive is not actually returning output directly into the compiled result like most directives do. The compiled PHP for this is held in an array to be added later.

Everything inside the ( ) of the directive is being used as a literal to call make on the View Factory:

"<?php echo \$__env->make({$expression}, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>"

If you found the compiled view for this Blade template you would see this:

<?php echo \$__env->make(auth()->check ? 'int-layouts.partials.wrapper' : 'ext-layouts.master', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>

The first argument to make ends up being your view name when this compiled view is executed.



Answered By - lagbox
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing