在有条件@if内扩展布局,导致布局重复 [英] Extending layout inside conditional @if causing duplication of layout

查看:74
本文介绍了在有条件@if内扩展布局,导致布局重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究laravel 5.5.我正在检查用户是否这样登录:

I'm working on laravel 5.5. I'm checking whether the user is logged in like this :

@if(Auth::check())
//some logic

@else
//somelogic

@endif

但是if/else语句将执行两次,具体取决于用户是否登录.您为什么认为这种情况正在发生? 我已经尝试过此处所述的其他方法来检查身份验证.我仍然遇到相同的错误.

But the if/else statement is getting executed twice depending whether the user is logged in or not. Why do you think that's happening? I have tried other ways to check authentication as mentioned here. Still I'm getting the same error.

我正在使用的布局.

<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
<head>
   <meta charset="utf-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial- 
    scale=1">

    <title>{{ config('app.name', '') }}</title>  

   <!-- Styles -->
   <link href="{{ asset('css/app.css') }}" rel="stylesheet">
</head>
<body>
<div id="app">

    @include('inc.navbar')
      <div class="container">
     @yield('content')
  </div>
  </div>

   <!-- Scripts -->
  <script src="{{ asset('js/app.js') }}"></script>
</body>
</html>

实施逻辑

@if(Auth::user())
@extends('layouts.app')

@section('content')
  <h1> Exercises</h1>
@endsection
 @else
  @extends('layouts.app')

  @section('content')
  <h1> Nope</h1>
 @endsection
@endif

推荐答案

像这样更改您的页面. @extends不应被调用两次.

Change your page like this. @extends should not be called twice.

@extends('layouts.app')
  @section('content')
    @if(Auth::user())
      <h1> Exercises</h1>
    @else
      <h1> Nope</h1>
    @endif
  @endsection

如果要扩展使用不同的布局,也可以使用三元运算符:

You can also use a ternary operator if you want different layouts for extend :

@extends(Auth::check() ? 'layouts.app' : 'layouts.app2')

这篇关于在有条件@if内扩展布局,导致布局重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆