laravel - 如何在 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');
解决方案
推荐阅读
- mysql - Telerik Report Designer 呈现空白报告
- angular - 读取承诺值并将其传递给可观察对象的下一个方法
- terraform - 有没有办法列出 terraform 资源的所有输入变量?
- gradle - 带有 Lombok 的 Gradle 随机中断
- html - 从 jQuery 1.12.4 迁移到 3.3.0 问题
- php - 如果它是数字数组中的第 10 个数字,则在 PHP 中标记或标记一个数值
- node.js - Node JS,导出一个类并使用不同的名称对其进行 cosumin
- sql - 索引扫描期间有太多的缓冲区命中+读取
- python - SSM 的 BOTO3 服务员类型
- python - 模型方法之和 django