首页 > 解决方案 > 如何在 Laravel 中实现管理面板和客户面板

问题描述

**新蜜蜂

**拉拉维尔

我想使用 URL 访问管理面板http://127.0.0.1:8000/admin和使用 URL 访问客户面板http://127.0.0.1:8000/

Customer Login: http://127.0.0.1:8000/login

Admin Login: http://127.0.0.1:8000/admin/

我已经设置了一个会话变量来识别用户是否admin or customer在我的用户中,AdminController.php并检查该用户必须是管理员才能访问的每个功能。下面是脚本

管理员控制器.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Auth;
use Session;

class AdminController extends Controller
{
    public function index() {
        return view('admin.index');
    }

    public function login(Request $request) {
            
        Auth::attempt(['email' => $request->email, 'password' => $request->password, 'user_type' => 'admin']);

        //was any of those correct ?
        if ( Auth::check() ) {
            //send them where they are going 
            Session::put('userType', 'admin');
            return redirect()->route('admin.dashboard');
        }

        return redirect('/admin')->with('flash_message', 'Invalid Credentials');
    }

    public function dashboard() {
        if( Session::has('userType') and Session::get('userType') == 'admin' )
            return view('admin.dashboard');
        else
            return redirect('/admin')->with('flash_message', 'Please login to access');
    }

    public function posts() {
        if( Session::has('userType') and Session::get('userType') == 'admin' )
            return view('admin.posts');
        else
            return redirect('/admin')->with('flash_message', 'Please login to access');
    }

    public function logout() {
        Session::flush();
        return redirect()->route('admin');
    }
}

我正在检查用户是否在每个这样的功能中都是管理员if( Session::has('userType') and Session::get('userType') == 'admin' )。那么有没有更好的方法呢??????

现在,当我登录管理面板时,我也可以访问http://127.0.0.1:8000/home客户,但这不应该被访问,因为我已经以管理员身份而不是以客户身份登录。

现在如何为 2 种不同类型的用户启用会话变量并确保用户的可访问性???

网页.php

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

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

Auth::routes();

Route::get('/admin', 'AdminController@index')->name('admin');
Route::post('/admin/login', 'AdminController@login')->name('admin.login');
Route::get('/admin/dashboard', 'AdminController@dashboard')->name('admin.dashboard');
Route::get('/admin/posts', 'AdminController@posts')->name('admin.posts');
Route::get('/admin/logout', 'AdminController@logout')->name('admin.logout');

标签: laravel

解决方案


您需要的是中间件或某种第三方软件包,例如laratrsut


推荐阅读