简体   繁体   English

无法访问Auth ::在laravel 5.3中查看我的视图

[英]Can't Access Auth::check in my view in laravel 5.3

I'm new to laravel framework and trying to implement login system to my website. 我是laravel框架的新手,并试图在我的网站上实现登录系统。 My web site has a landing page. 我的网站有一个登陆页面。 Base on user login status I need to change this links like bellow. 根据用户登录状态,我需要像bellow一样更改此链接。

<ul class="secondary_menu">
  @if(Auth::guard('customer')->check())
  <li><a href="{{url('/logout')}}" >Logout</a></li>
  @else
  <li><a href="{{url('/login')}}" >Login or Register</a></li>
  @endif
</ul> 

Here's the controller responsible for loading this view 这是负责加载此视图的控制器

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;

class CustomerController extends Controller
{
    public function index()
    {
        return view('public.index');
    }

}

I'm getting an error that saying that Class 'Auth' not found 我收到一条错误,说Class 'Auth' not found

I try adding use Auth and use \\Auth as suggested in other slimier kind of questions in stackoverflow but did not works for me. 我尝试添加use Auth并按照stackoverflow中其他更简单的问题的建议use \\Auth ,但对我不起作用。 What am i doing wrong. 我究竟做错了什么。 Isn't is possible to check call Auth from view ? 是不是可以从视图中检查呼叫Auth

Put the following code to use the Auth in your code: 将以下代码用于代码中的Auth:

use Illuminate\Support\Facades\Auth;

and then use it like: 然后使用它像:

if (Auth::check())
{
   // your code
}

As alternative to Auth:: facade you can use global helper: 作为Auth:: facade的替代,您可以使用全局帮助器:

auth()->check();
auth()->user();

在您的控制器上试试这个

use Illuminate\Support\Facades\Auth;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM