此代码在RedirectIfAuthenticated.php中意味着什么?

时间:2018-10-19 10:11:57

标签: laravel laravel-5

我在RedirectIfAuthenticated.php中遇到了这个特定的代码,但我不明白这实际上是有效的吗?

public function handle($request, Closure $next, $guard = null)
 {
     if (Auth::guard($guard)->check()) {
        return redirect('/home');
     }
     else {
        return redirect()->action('AdminController@login')->with('flash_message_error','Please login to access ');
     }

   return $next($request);
 }

现在我要问的重点是,如果我注释掉上面代码中的else部分,则可以访问使用php artisan make:auth形成的默认页面(登录,注册等)。但是,一旦取消注释其他部分,便无法访问默认页面,但我直接点击了在项目中创建的AdminController的login页面。为什么会这样?

作为参考,我还提供了控制器和路由代码。

AdminController

class AdminController extends Controller {

public function login(Request $request) 
{

    if ($request->isMethod('post')) {
        $data = $request->input();
        if (Auth::attempt(['email' => $data['email'], 'password' => $data['password'], 'admin' => '1'])) { 

            return redirect('/admin/dashboard');
        } else {
            return redirect('/admin')->with('flash_message_error','Invalid Username or Password');
        }
    }


    return view('admin\admin_login');
}

public function dashboard()
    {

        return view('admin.dashboard');
    }

Routes

Route::match(['get','post'],'/admin','AdminController@login');
Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Route::group(['middleware'=>['auth']],function(){

    Route::get('admin/dashboard', 'AdminController@dashboard');
    Route::get('settings','AdminController@settings');
});


Route::get('logout', 'AdminController@logout');

2 个答案:

答案 0 :(得分:2)

使用伪代码:

if __name__ == '__main__': # to avoid new window with a new process
    multiprocessing.freeze_support() # support multiprocessing in pyinstaller
    from kivy.lang.builder import Builder
    from kivy.clock import Clock
    from kivy.uix.popup import Popup
    from kivy.uix.textinput import TextInput
    from kivy.uix.scatterlayout import ScatterLayout
    from kivy.graphics.transformation import Matrix
    from kivy.uix.scatter import Scatter
    from kivy.uix.screenmanager import ScreenManager, Screen
    from kivy.properties import BooleanProperty, ListProperty, BoundedNumericProperty, StringProperty, NumericProperty
    from kivy.uix.boxlayout import BoxLayout
    from kivy.app import App
    from kivy.uix.behaviors import ButtonBehavior
    from kivy.uix.image import Image
    from kivy.core.window import Window

Laravel的中间件中不存在else部分,看来您已经添加了它。那不应该存在,因为这将导致该中间件总是将某人重定向到某个地方。

该中间件应该用于“仅来宾”路线。因此,如果您通过了身份验证,它将重定向到“ home”,否则,它将运行下一个中间件。

答案 1 :(得分:1)

RedirectIfAuthenticated.php是运行直到您看到请求结果的中间件。它只会检查您是否登录并重定向到相应的路由。

相关问题