我是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
答案 0 :(得分:1)
if 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预定义的一个middlware组,如下所示。
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
});
这&#39;它
答案 1 :(得分:0)
要检查是否有用户登录,只需使用此
即可@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
答案 2 :(得分:0)
花了几个小时来处理这个bug我发现我必须将所有路由放在routes.php文件中的web中间件中..只有'/'路由在中间件之外,这就是为什么它不是工作。所以现在我做了这个 - &gt;
Route::get('/', function () {
return view('frontend.home');
});
进入这个
Route::group(['middleware' => ['web']], function () {
Route::get('/', function () {
return view('frontend.home');
});
});
并修复了错误:D