从上下文'Illuminate \ Pipeline \ Pipeline'调用受保护的方法App \ Http \ Middleware \ Admin :: handle()

时间:2018-10-23 04:05:52

标签: php laravel

我真的对Laravel陌生。我尝试通过遵循一些YouTube教程来创建页面。突然一切都好了,调用中出现了保护方法免受上下文管道影响的错误。我尝试在Stackoverflow中搜索该错误,但是无法。我希望任何人都可以帮助告诉我这里到底有什么问题。下面我附上我的代码;

路线:

Route::get('/','Adminauth\AuthController@showLoginForm');
Route::post('login','Adminauth\AuthController@login');
Route::group(array('middleware'=>'Admin'),function(){
Route::get('/dashboard','Admin\AdminControll@dashboard');
Route::get('/logout','Adminauth\AuthController@logout');
});
Route::get('/create',function(){
    App\User::create([
         'name' => 'sara',
         'username'=>'sara',
         'email' => 'sara@yahoo.com.my',
         'password' => bcrypt('******'),
            ]);
});

文件夹中有两个控制器 管理员和adminauth 管理员控制器:

<?php

namespace App\Http\Controllers\Admin;

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

class AdminControll extends Controller
{
   public function __construct ()
   {
    $this->middleware('Admin');
   }
   public function dashboard()
   {
    return view('Admin.layout');
   }
}

AuthController

<?php

namespace App\Http\Controllers\Adminauth;

use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Auth;

class AuthController extends Controller
{
    use RegistersUsers, ThrottlesLogins;
    protected $redirecTo = '/dashboard';
    protected $guard = 'Admin';
    public Function showLoginForm()
    {
        if(Auth::guard('Admin')->check())
        {
            return redirect('/dashboard');
        }
        return view ('login');

    }
    public function logout(){
        Auth::guard('Admin')->logout();
        return redirect('/');
    }
}

中间件(Admin.php)

<?php

namespace App\Http\Middleware;

use Closure;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\Auth;

class Admin
{
    /**
     * Get the path the user should be redirected to when they are not authenticated.
     * @param  mixed
     * @param  \Closure $next
     * @param  \Illuminate\Http\Request  $request
     * @return string|null $guard
     */
    protected function handle($request,Closure $next,$guard = 'Admin')
    {
        if(!Auth::guard($guard)->check()){
return redirect('/');
        }
 return $next($request);
}
    }

kernel.php

   */
    protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
        'Admin' => \App\Http\Middleware\Admin::class,
    ];
}

2 个答案:

答案 0 :(得分:0)

handle()方法应该是公共的,如下所示:

public function handle($request,Closure $next,$guard = 'Admin')

答案 1 :(得分:0)

在您的Middleware(Admin.php)中使handle方法对public可见。

Laravel无法在Admin类之外访问handle方法,因为它设置为protected