配置不同路由,laravel手动验证登陆

需求:配置一个模块Admincp,在app\Http目录下,这个目录作为后台应用

步骤一:在app/Http创建Admincp目录,在routes目录下创建admincp.php文件

步骤二:打开app/Providers/RouteServiceProvider.php文件,添加

protected $admincpNamespace = 'App\Http\Admincp';

重写map方法:

public function map()

{

    if(request()->server('SERVER_NAME')=='www.laravel55.com'){

        $this->mapAdmincpRoutes();

    }else{

        $this->mapWebRoutes();

        $this->mapApiRoutes();

    }

}

其中www.laravel55.com是你本地自己配置的域名。

其中mapAdmincpRoutes方法:

protected function mapAdmincpRoutes()

{

//        Route::prefix('api')这边是指整个路由的前缀

        Route::middleware('admincp')

            ->namespace($this->admincpNamespace)

            ->group(base_path('routes/admincp.php'));

    }

步骤三:打开app/Http/Kernel.php文件,在$middlewareGroups下复制一个web,并且修改成admincp,并增加一个中间件AdmincpAuth:

protected $middlewareGroups = [

    'web' => [

        \App\Http\Middleware\EncryptCookies::class,

        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,

        \Illuminate\Session\Middleware\StartSession::class,

        // \Illuminate\Session\Middleware\AuthenticateSession::class,

        \Illuminate\View\Middleware\ShareErrorsFromSession::class,

        \App\Http\Middleware\VerifyCsrfToken::class,

        \Illuminate\Routing\Middleware\SubstituteBindings::class,

    ],

    'api' => [

        'throttle:60,1',

        'bindings',

    ],

    //这边是新增的

    'admincp' => [

        \App\Http\Middleware\EncryptCookies::class,

        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,

        \Illuminate\Session\Middleware\StartSession::class,

        // \Illuminate\Session\Middleware\AuthenticateSession::class,

        \Illuminate\View\Middleware\ShareErrorsFromSession::class,

        \App\Http\Middleware\VerifyCsrfToken::class,

        \Illuminate\Routing\Middleware\SubstituteBindings::class,

        \App\Http\Middleware\AdmincpAuth::class,

    ],

];

步骤四:在app\Http\Middleware目录下新建一个AdmincpAuth.php文件;其中的内容:

namespace App\Http\Middleware;

use Closure;

class AdmincpAuth

{

    /**

* Handle an incoming request.

*

    * @param  \Illuminate\Http\Request  $request

    * @param  \Closure  $next

    * @return mixed

*/

    public function handle($request, Closure $next)

{

        return $next($request);

    }

}

步骤五:创建UserController.php控制器

//登录

//登陆

public function login(Request $request){

    if($request->isMethod('post')){

        $guard=Auth::guard('admincp');

        $credentials=$request->only('name','password');

        if (!$guard->validate($credentials)) {

            return '帐号或密码不正确';

        }

        $user = $guard->getLastAttempted();

        $guard->login($user,true);

        return redirect()->intended('/index');

    }

    return view('admincp.user.login');

}

//退出

public function logout(){

    $guard=Auth::guard('admincp');

    $guard->logout();

    return redirect(route('user.login'));

}

登陆成功之后打印出相应的信息:

dd(Auth::guard('admincp')->user());

步骤六:打开app/config/auth.php文件,guards配置admincp数组,在providers创建一个admincp_users数组:

'guards' => [

        'web' => [

            'driver' => 'session',

            'provider' => 'users',

        ],

        'api' => [

//            'driver' => 'token',

            'driver' => 'passport',

            'provider' => 'users',

        ],

        'admincp' => [

            'driver' => 'session',

            'provider' => 'admincp_users',

        ],

    ],

'providers' => [

    'users' => [

        'driver' => 'eloquent',

        'model' => App\User::class,

    ],

    'admincp_users' => [

        'driver' => 'eloquent',

        'model' => App\Models\Admincp\Users::class,

    ],

    // 'users' => [

//    'driver' => 'database',

//    'table' => 'users',

// ],

],

步骤七:最后创建一张admincp_users表,和users表一样复制一张。

步骤八:成功!

你可能感兴趣的:(配置不同路由,laravel手动验证登陆)