简体   繁体   English

身份验证中间件未重定向到登录页面

[英]Auth middleware not redirecting to login page

I have this situation where I have a route /admin which requires Auth middleware to be activated. 在这种情况下,我有一个路由/admin ,需要激活Auth中间件。 I have the middleware requirement indicated in the web.php route. 我在web.php路由中指出了中间件要求。 Also, I do have the default auth setup of laravel. 另外,我确实有laravel的默认auth设置。 The kernel.php does have the middleware indicated too. kernel.php确实也指示了中间件。

But, weirdly enough /admin brings me to a white page with nothing. 但是,足够奇怪的是/admin却使我无所事事进入白页。 When logged in, the problem isn't there. 登录后,问题就不存在了。 It had been working and all of a sudden it was not working anymore. 它一直在工作,突然间它不再工作了。

The auth middleware is as it is: 身份验证中间件是这样的:

   <?php

namespace App\Http\Middleware;

use Illuminate\Auth\Middleware\Authenticate as Middleware;

class Authenticate extends Middleware
{
    /**
     * Get the path the user should be redirected to when they are not authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return string
     */
    protected function redirectTo($request)
    {
        return route('login');
    }
}

The controller: 控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\NewsletterSubscribers;
use App\User;
use File;

class adminController extends Controller
{
    //
    public function __construct()
    {
         $this->middleware('auth');    
        $this->middleware('admin');
    }

    public function index(){

        return view('admin.home');
    }

    public function changebg(){

        return view('admin.changebg');
    }

    public function changebgimage(Request $request){
     $this->validate($request,
    [
     'image'=>'required|image|mimes:jpg,JPG,jpeg,JPEG|max:4096|dimensions:max_width:1600,max_height:1100',
      ]
    );

        $path="images/";
        $imagepath="images/bg.jpg";
        if( File::exists($imagepath))
        {
        unlink($imagepath);
    }

  if ( ! File::exists($path) )
  {
  File::makeDirectory($path,0777,true);
  }
        $getimageName = "bg.jpg";
        $request->image->move(public_path($path), $getimageName);

        return view('admin.home');
    }

    public function newslettersubscriberslist(){

        $newslettersubscribers= NewsletterSubscribers::all();
        $count=0;
        return view('admin.subscriberslist',compact('newslettersubscribers','count'));

    }

    public function registerAdmin(){
        return view('auth.adminregister');
    }

    public function viewAdmins(){

        $admins= User::select('users.*')->where('role','=','admin')->get();
        //print_r($admins);
        $count=0;
        return view('admin.adminlist',compact('admins','count'));
    }

    public function viewUsers(){

        $users= User::select('users.*')->where('role','=','user')->get();
        //print_r($admins);
        $count=0;
        return view('admin.userlist',compact('users','count'));
    }    

}

The admin middleware: 管理员中间件:

<?php

namespace App\Http\Middleware;

use Closure;
use Auth;

class Admin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (Auth::check() && Auth::user()->role == 'admin') {
            return $next($request);
        }

        else {
            return redirect('/login');
        }
    }
}

The the route I'm using: 我正在使用的路线:

Route::get('/admin', 'AdminController@index')->name('adminhome')->middleware('auth');

I dont find anything weird here but weirdly enough the problem exists. 我在这里没有发现任何奇怪的东西,但是奇怪的是这个问题存在了。 Can you guys trace somethin unusual here or somewhere it can be?? 你们可以在这里或某个地方找到不寻常的东西吗?

First of all, make sure you have error reporting turned on. 首先,请确保您已打开错误报告。 Also take a look at laravel log. 还可以看看laravel日志。 Looking at your code the problem might be case of AdminController . 查看您的代码,问题可能出在AdminController情况下。 In routes you have 'AdminController@index' but the class you showed has name adminController and it should be AdminController . 在路由中,您具有'AdminController@index'但您显示的类的名称为adminController ,并且应该为AdminController I also don't know what is the name of file but it should be again AdminController.php 我也不知道文件的名称是什么,但是应该再次是AdminController.php

You can use the middleware like this 您可以像这样使用中间件

Route::group(['middleware'=>'auth'], function()
{
  Route::get('/admin', 'AdminController@index')->name('adminhome');
}

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

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