【问题标题】:Laravel 5.2 if else cond. in blade file not workingLaravel 5.2 if else cond.在刀片文件中不起作用
【发布时间】:2016-04-21 10:50:10
【问题描述】:

我是 laravel 的新手。我可以知道为什么我的 if else 条件不起作用吗?问题是,即使管理员登录,我也看不到此页面上的管理面板按钮和注销按钮。我只看到登录按钮。我想我的 home.blade.php 文件没有逻辑错误。此问题仅针对此刀片文件。其他刀片文件在 if else 条件下工作正常。这是我的刀片文件。我还分享了这个项目的 github 链接:https://github.com/TawsifKarim/jambura(它是产品的基本 CRUD 应用程序)所以你可以下载运行并检查我的 views/frontend/home.blade.php 文件中的错误

homelayout.blade.php

<html>
<head>
  <title>Product Management</title>
  <meta charset ="utf-8">
  <link rel="icon" type="png" href="img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js" ></script>
    <link href="https://fonts.googleapis.com/css?family=Lato:100" rel="stylesheet" type="text/css">
    <link rel="stylesheet" href="{{ asset('css/style.css') }}">
    <script src="js/this.js"></script>
<body>

@yield('content')

</body>
</html>

home.blade.php

@extends('layout.homelayout')
@section('content')
<h2 id="Sitename">Product Management System</h2>

<img id="bannerimg" src="img/bg.jpg" class="img-responsive">

 <div class="container-fluid">
        <div class="row">
            <div class="col-md-2" id="homebutton">

                  <a href="/" class="btn btn-info btn-lg btn-block" id="Hbut">Home</a>
                  <a href="/Product" class="btn btn-info btn-lg btn-block" id="Hbut">Products</a>
                  <a href="#" class="btn btn-info btn-lg btn-block" id="Hbut">Contact us</a>
                  <a href="#" class="btn btn-info btn-lg btn-block" id="Hbut">About</a>

                  @if(!Auth::guest())
                  @if(Auth::user()->user_type_id == 1)
                  <a href="{{ url('/AdminPanel') }}" class="btn btn-info btn-lg btn-block" id="Hbut">Admin Panel</a>
                  @endif
                  @endif

                  @if(Auth::guest())
                  <a href="{{ url('auth/login') }}" class="btn btn-info btn-lg btn-block" id="Hbut">Log in</a>
                  @else
                  <a href="{{ url('auth/logout') }}" class="btn btn-info btn-lg btn-block" id="Hbut">Log Out</a>
                  @endif

            </div>
            <div class="col-md-10">
                <div class="jumbotron">
                        <p id="startttl"> Laravel Project by Tawsif</p>
                        <p id="logindes">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex</p>
                </div>
            </div>
        </div>
      </div> 


@stop

【问题讨论】:

  • 您确定您已登录吗?请包含您的routes.php 文件
  • 你用的是laravel的认证吗?例如。 Auth::attempt(['email' =&gt; $email, 'password' =&gt; $password]) 如果没有,那么您的代码将无法工作,让我们更多地查看您的代码,以便我们为您提供帮助。 .
  • 是的,路线是什么。这些路由在“网络”中间件组中吗?
  • 我也有同样的问题...但是在我的本地计算机(Windows - 不是宅基地)中它可以完美运行...当推送到服务器时,停止工作...

标签: php laravel if-statement laravel-5.2 laravel-blade


【解决方案1】:

如果 else 条件正常工作,但 Auth 使用会话,并且会话在 laravel 5.1 中默认启动,默认中间件。

protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,//here is starting of session code
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
    ];

但在 laravel 5.2 中间件组概念是新添加的。 laravel 5.2 预定义了一个中间件组,如下所示。

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
    ]
];

所以如果你想使用 session 或 auth,你必须在下面的路由中添加路由组

Route::group(['middleware' => ['web']], function () {
    //here you all routes
});

就是这样。

【讨论】:

    【解决方案2】:

    花了几个小时处理这个错误后,我发现我必须将所有路由放在 routes.php 文件中的 web 中间件中。只有“/”路由在中间件之外,这就是为什么它不是在职的。所以现在我做了这个->

       Route::get('/', function () {
        return view('frontend.home');
       });
    

    进入这个

    Route::group(['middleware' => ['web']], function () {
    Route::get('/', function () {
            return view('frontend.home');
           });
    });
    

    并且该错误已修复:D

    【讨论】:

      【解决方案3】:

      要检查是否有用户登录,只需使用这个

      @if(Auth::user())
          // your code will be here
      @endif
      

      在你的情况下,你可以使用这个

      @if(Auth::user())
          <a href="{{ url('/AdminPanel') }}" class="btn btn-info btn-lg btn-block" id="Hbut">Admin Panel</a>
          <a href="{{ url('auth/logout') }}" class="btn btn-info btn-lg btn-block" id="Hbut">Log Out</a>
      @else
          <a href="{{ url('auth/login') }}" class="btn btn-info btn-lg btn-block" id="Hbut">Log in</a>
      @endif
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-07-10
        • 2019-01-31
        • 2021-07-11
        • 2017-07-09
        • 1970-01-01
        • 2020-06-20
        • 2016-05-31
        相关资源
        最近更新 更多