根据用户的类型/角色重定向到不同的视图。使用Laravel 5.8

时间:2019-07-15 06:58:43

标签: php laravel

我只是想在不同的视图中重定向不同类型的用户。

我看了很多教程,但是它似乎不适用于我到目前为止所做的事情。所以我有一个表,该表具有用户名,密码和类型(枚举类型分别为admin和superAdmin),每个表的每个表都具有其uniques属性。我已经做了这种问题,但是没有laravel框架。我正在使用laravel的Auth。我怎么能用一个简单的方法做到这一点。太谢谢你了!

4 个答案:

答案 0 :(得分:1)

如果您已运行命令php artisan make:auth,则您的LoginController.php文件夹中有app\Http\Controllers\Auth。 您可以将方法redirectTo放在该类中,您可以在其中放置重定向逻辑

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;

class LoginController extends Controller
{

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected function redirectTo(){
        $user = Auth::user();
        if($user->hasRole('SUPER_ADMIN')){
            return '/superadminroute';
        } elseif($user->hasRole('ADMIN')){
            return '/adminroute';
        }

        // Of Course You Can Use route('routename') Helper fuction to generate Route
        return '/';
    }
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

}

此功能在用户成功登录并且需要将用户重定向到指定路由时运行。

希望这对您有帮助。

答案 1 :(得分:1)

这将起作用:

protected function redirectTo(){
    $user = Auth::user();
    if($user->type == "SUPERADMIN"){ // Use your user type
        return '/superadminroute';
    } elseif($user->type == "ADMIN"){ // Use your user type
        return '/adminroute';
    } else{
        return '/home';
    }
}

放置为Auth\LoginController。确保删除protected $redirectTo = '/home';;

答案 2 :(得分:0)

您可以像使用Laravel中间件一样简单。也许下面的一些代码可以帮助您:

首先,制作一个新的中间件php artian make:middleware UserVerify

第二,编写一些逻辑代码以通过User role重定向用户:

public function handle($request, Closure $next)
{
    if(\Auth::user()->type == 'superAdmin')
    {
        return $next($request);
    }
    return redirect()->route('route_name');
}

第三,将此中间件注册到您的项目App\Http\Kernel.php

最后,将此中间件应用于所需的路由。

答案 3 :(得分:0)

最简单的方法是这样做。通过此用户,您可以看到您以超级管理员身份分配给他们的内容。有此表:

userstable with ID, fullname, username, password and role attributes
roletable with ID and roleName attributes
moduletable with ID, moduleName and route attributes
assignModulestable with ID, roleID and moduleID attributes
asignRole table with ID, roleID and userID attributes

ROLE table:
ID  roleName
--------------
1       admin
2       sub admin


MODULE table:
ID  moduleName         route
------------------------------
8 | View Members       | /members
7 | Members Withdrawal | /withdrawals
6 | View Payment       | /payment


assignModules table:
ID  roleID  moduleID
--------------------
5  | 1    |    8
6  | 1    |    7
21 | 1    |    6
22 | 2    |    8
23 | 1    |    7

asignRole table:
ID  roleID  userID
-----------------
1 | 1 | 39
2 | 2 | 43
5 | 2 | 44
4 | 2 | 46

users table:
id  name    email                  password  role
--------------------------------------------------
39  Luke    lukem@gmail.com         1234      1
43  John    john@gmail.com          1234      2
44  mark    mark@gmail.com          1234      2
46  Peter   peter@gmail.com         1234      2

the above will be the table structure and the data therein. try to normalised the table with primary and foreign keys. 

create your corresponding forms for data insertion. As you can see above, we have two roles; admin and subadmin with ids 1 and 2 respectively

define your route and link name in module table as shown in module table
in your assign module table assign a role to a module using the ids
in the assign role table define the user and roles as shown in assignRole table above.
then you have your users table with role attribute defined

最后,在您的layout.blade.php中添加以下内容:

<li>
                                <a href="#"><i class="fa fa-user-o fa-fw"></i> Management<span class="fa arrow"></span></a>
                                <ul class="nav nav-second-level">


                                    </li>
                                 @php
                                    $userModule = DB::table('assignModules')
                                    ->join('modules', 'modules.moduleID', '=', 'assignModules.moduleID')
                                    ->where('assignModules.roleID', '=', Auth::user()->role)
                                    ->whereRaw('modules.moduleID = assignModules.moduleID')
                                    ->distinct()
                                    ->select('modules.moduleName', 'modules.moduleID','modules.route')
                                    ->orderBy('modules.moduleID', 'ASC')
                                    ->get();
                                    @endphp
                              @if($userModule)
                                @foreach($userModule as $module)

                                     <li>
                                         <a href="{!! url($module->route) !!}"><i class="fa fa-user"></i> {{ $module->moduleName }}</a>
                                     </li>

                                @endforeach
                              @endif

                                </ul>
                                <!-- /.nav-second-level -->
                            </li>
                          @if( Auth::user()->role==1 )
                            <li>
                                <a href="#"><i class="fa fa-sitemap fa-fw"></i> Technical Settings<span class="fa arrow"></span></a>
                                <ul class="nav nav-second-level">
                                    <li>
                                        <a href="{{ url('/assign-user') }}"><i class="fa fa-user"></i> Assign User</a>
                                    </li>
                                     <li>
                                        <a href="{{ url('/module') }}"><i class="fa fa-user"></i> Create Modules</a>
                                    </li>
                                    <li>
                                        <a href="{{ url('/assign-module') }}"><i class="fa fa-user"></i> Assign Module</a>
                                    </li>


                                </ul>
                                <!-- /.nav-second-level -->
                            </li>
                           @else



                           @endif