Laravel 5中的两个登录表单 [英] Two login forms in laravel 5

查看:112
本文介绍了Laravel 5中的两个登录表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想知道如何在laravel 5中制作两个登录表单……原因是因为我有一个多站点项目,我拥有管理站点和公共站点在一个项目中.

我对路由进行了分组,因此管理员路由答复到一个域,公共路由答复到另一个域,如下所示:

Route::group(array( 'domain' => 'restaurant.com', 'namespace' => 'Public' ), function () {
    //some routes
});

Route::group(array( 'domain' => 'restaurant.net', 'namespace' => 'Admin' ), function () {
    //some routes
});

我还在这样的每组路由中创建了用于身份验证的自定义路由(此路由用于公共):

Route::controllers([
    'auth' => 'Auth\AuthController',
    'password' => 'Auth\PasswordController',
]);

Route::get( '/register' , [
    'as' => 'publicRegister' ,
    'uses' => 'Auth\AuthController@getRegister'
] );

Route::post( '/registrar' , [
    'as' => 'publicPostRegister' ,
    'uses' => 'Auth\AuthController@postRegister'
] );

Route::get( '/login' , [
    'as' => 'publicLogin' ,
    'uses' => 'Auth\AuthController@getLogin'
] );

Route::post( '/login' , [
    'as' => 'publicPostLogin' ,
    'uses' => 'Auth\AuthController@postLogin'
] );

Route::get( '/logout' , [
    'as' => 'publicLogout' ,
    'uses' => 'Auth\AuthController@getLogout'
] );

我还在每个父文件夹中使用其控制器('AuthController','PasswordController')创建了Auth文件夹,我的控制器如下:

app    
|---Http
    |---Controllers
        |----------Public
        |          |---Auth
        |          |    |---AuthController
        |          |    |---PasswordController
        |          |--- ...
        | 
        |----------Admin
                  |---Auth
                  |    |---AuthController
                  |    |---PasswordController
                  |--- ...

因此对于视图,我有单独的Auth视图,如下所示:

resources
    |---views
        |----------Public
        |          |---Auth
        |          |    |---login.blade.php
        |          |    |---password.blade.php
        |          |    |---register.blade.php
        |          |    |---reset.blade.php
        |          |--- ...
        | 
        |----------Admin
                   |---Auth
                   |    |---login.blade.php
                   |    |---password.blade.php
                   |    |---register.blade.php
                   |    |---reset.blade.php
                   |--- ...

在我的模型中,users表具有一个 type 列,该列将过滤来自Public或Admin站点的用户. 这里的主要问题是:如何为我的项目制作两个登录表单? 我想要的是公共用户无法登录到管理站点,反之亦然.

到目前为止,我已经尝试覆盖AuthenticatesAndRegistersUsers函数,例如getLogingetRegister以及变量,例如$loginPath$redirectTo,但是在登录表单中调用publicPostLogin(结帐路线)时采取行动{{ route('publicPostLogin') }}的公共部门根本行不通...

解决方案

您是否听说过 laravel .在这个库中,可以在一个laravel应用程序中登录的用户有两种或多种. 在我们的例子中,有两种类型的用户 Admin Public 表示用户权限.您通过插入完全错误的亲爱的用户类型来识别用户.

您必须使用此库.只需按照链接步骤安装库即可.并分配两个不同的表,例如在我们的案例中,在restaurant.com中,用户类型为Public,这意味着有一个使用User表的简单用户./p>

在本例中,用于管理的另一方面是restaurant.net.此登录表单使用admin表进行登录.

忘记密码和重置密码功能在一个应用程序中分别起作用.

'multi' => [ 'admin' => [ 'driver' => 'database', 'table' => 'admin', 'email' => 'client.emails.password' ], 'users' => [ 'driver' => 'database', 'table' => 'users', 'email' => 'client.emails.password', ] ],

I've been wondering how could i make two login forms in laravel 5 for a while... The reason of this is because i have a multi-site project, i've got the admin site, and the public site in one project.

I've grouped the routes so the admin routes answer to a domain and public routes answer to another domain like this:

Route::group(array( 'domain' => 'restaurant.com', 'namespace' => 'Public' ), function () {
    //some routes
});

Route::group(array( 'domain' => 'restaurant.net', 'namespace' => 'Admin' ), function () {
    //some routes
});

I've also created custom routes for authentication in each group of routes like this (this ones are for Public):

Route::controllers([
    'auth' => 'Auth\AuthController',
    'password' => 'Auth\PasswordController',
]);

Route::get( '/register' , [
    'as' => 'publicRegister' ,
    'uses' => 'Auth\AuthController@getRegister'
] );

Route::post( '/registrar' , [
    'as' => 'publicPostRegister' ,
    'uses' => 'Auth\AuthController@postRegister'
] );

Route::get( '/login' , [
    'as' => 'publicLogin' ,
    'uses' => 'Auth\AuthController@getLogin'
] );

Route::post( '/login' , [
    'as' => 'publicPostLogin' ,
    'uses' => 'Auth\AuthController@postLogin'
] );

Route::get( '/logout' , [
    'as' => 'publicLogout' ,
    'uses' => 'Auth\AuthController@getLogout'
] );

I've also created the Auth folder with it's controllers ('AuthController', 'PasswordController') in each parent folder, my controllers are like this:

app    
|---Http
    |---Controllers
        |----------Public
        |          |---Auth
        |          |    |---AuthController
        |          |    |---PasswordController
        |          |--- ...
        | 
        |----------Admin
                  |---Auth
                  |    |---AuthController
                  |    |---PasswordController
                  |--- ...

And so for the views i've got separate Auth views like this:

resources
    |---views
        |----------Public
        |          |---Auth
        |          |    |---login.blade.php
        |          |    |---password.blade.php
        |          |    |---register.blade.php
        |          |    |---reset.blade.php
        |          |--- ...
        | 
        |----------Admin
                   |---Auth
                   |    |---login.blade.php
                   |    |---password.blade.php
                   |    |---register.blade.php
                   |    |---reset.blade.php
                   |--- ...

In my models the users table has a type column that will filter users from Public or Admin site. The main question here is: How could i make two login forms for my project? What i would like is that Public Users couldn't log into the Admin site and viceversa.

What i've tried so far is override the AuthenticatesAndRegistersUsers functions like getLogin, getRegister and also variables like $loginPath, $redirectTo but when calling publicPostLogin(checkout routes) in the login form of Public that has as action {{ route('publicPostLogin') }} it just don't works...

解决方案

Do you hear about Multiauth in laravel. in this library there are two or more type user can login in one laravel application. In our case there are two type user Admin and Public that means User right.And you identified user by insert usertype that totally wrong my dear.

You have to use this library.just follow that link step to install library.And assign two different table like in our case in restaurant.com there is user type is Public that means there is simple user that uses User table.

On another hand for admin in our case there is restaurant.net.This login form use admin table to login.

Both forgot password and reset password functionality works separately in one application.

'multi' => [ 'admin' => [ 'driver' => 'database', 'table' => 'admin', 'email' => 'client.emails.password' ], 'users' => [ 'driver' => 'database', 'table' => 'users', 'email' => 'client.emails.password', ] ],

这篇关于Laravel 5中的两个登录表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆