Laravel身份验证和过滤器

问题描述 投票:0回答:1

我需要帮助,有人可以告诉我,如果我的工作与身份验证用户配置文件一致吗?我有下一个文件:

文件routes.php(本例中我只使用了两个组)

<?php
    //home
    Route::get('/',function()
    {
        return Redirect::to('login');
    });

    //login get
    Route::get('login','AuthController@showLogin');

    //login for form
    Route::post('login','AuthController@postLogin');

    //routes for admin
    Route::group(array('before' => 'adminFilter'),function()
    {
        Route::get('/adminHomePage',function()
        {
            return View::make('adminHome');
        });
    });

    //route for common user
    Route::group(array('before' => 'commonUserFilter'),function()
    {
        Route::get('/commonUserPage',function()
        {
            return View::make('commonPage');
        });
    });

    Route::get('logout','AuthController@logout');
?>

文件filters.php

<?php 
    Route::filter('adminFilter', function($route, $request)
    {
        if (Auth::user()->profile != 1)
        {
            return Redirect::to('/logout');
        }
    });

    Route::filter('commonUserFilter',function($route, $request)
    {
        if (Auth::user()->profile != 2)
        {
            return Redirect::to('/logout');
        }
    });
?>

文件AuthController.php

<?php
    public function showLogin()
    {
        return View::make('login');
    }

    public function postLogin()
    {
        //Get user data from login form 
        $user = array(
        'user' => Input::get('username'),
        'password' => Input::get('password'));

        if(Auth::attempt($user,true))
        {           
            switch (Auth::user()->profile) 
            {
                case 1:
                    //home admin
                    return Redirect::to('/adminHomePage');
                    break;

                case 2:
                    //home common user
                    return Redirect::to('/commonUserPage');
                    break;
            }
        }
        else
        {
            return Redirect::to('login')
            ->with('mensaje_error','Incorrect data.')
            ->withInput();
        }
    }

    public function logOut()
    {
        Auth::logout();
        return Redirect::to('/login')
        ->with('mensaje_error', 'Your session was closed.');        
    }
?>
laravel authentication filter
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.