dpxpz37157 2016-01-22 03:15
浏览 468
已采纳

laravel5.2 Auth :: guest()返回错误“Class'App \ Http \ Controllers \ Auth'not found”

I am trying to use the inbuilt Auth for login in laravel and register. These automatically generated pages work so well, now I use Auth::guest() to check if the user is authorized to return a view: index else to login page.

But it shows:

Class 'App\Http\Controllers\Auth' not found".

Here's the code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;

class StepsController extends Controller
{
    public function step1()
    {
        if (Auth::guest())
        {
            return redirect('login');
        }else {
            return view('index');
        }
    }
}
  • 写回答

1条回答 默认 最新

  • dt888999 2016-01-22 03:31
    关注

    You need to "import" the definition of the facade of Auth and remove the use App\Http\Controllers\Auth because does not exists.

    Just add at the top (just before the class declaration):

    use Auth;
    

    And remove the other:

    Having this:

    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    use App\Http\Requests;
    use App\Http\Controllers\Controller;
    use Auth;
    
    class StepsController extends Controller
    {
    ....
    

    Hope it helps.

    PS: If you want to learn Laravel with a good guide, step-by-step, please check here: Learn Laravel

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?