如何将一个Laravel控制器与多个Guard结合使用,而不是复制控制器 [英] How to use One Laravel Controller with Multiple Guard instead of Duplicating the Controller
问题描述
我有两个警卫队
Admin
User
我还创建了控制器,用户可以在其中管理自己的数据,而管理员也可以管理用户数据.所以我创建了两个控制器
Also i have created controllers where user can manage his own data and admin can also manage user data. So i created two Controllers
Controllers
Admin
EducatonBackgroundController
User
EducationBackgroundController
在User/EducationBackgroundController中,我具有此功能,可获取当前登录用户的教育背景并显示在用户视图中
In User/EducationBackgroundController i have this function which fetch education background of a current logged user and display on the user view
public function index(Education $education)
{
try {
$educations = $education->where('user_id',$this->userLoggedID())->with('organization','program','country','city')->get();
return view('users.education.index',compact('educations'));
}
catch (Exception $e) {
abort(404);
}
}
在Admin/EducationBackgroundController中,我具有此功能,可获取所有用户的教育背景并显示在admin视图中
In Admin/EducationBackgroundController i have this function which fetch education background of all users and display on the admin view
public function index(Education $education)
{
try {
$educations = $education->with('organization','program','country','city','user')->get();
return view('admin.users.education.index',compact('educations'));
}
catch (Exception $e) {
abort(404);
}
}
从观察上看,这些功能相似,但在视图返回和数据获取方面有所不同.
From observation these functions are similar but differ on view return and data fetch.
那么我该如何创建一个可以同时被管理员和用户卫队使用的控制器,而不是为两个卫队使用重复的控制器和视图.
So How can i create a one Controller which can be used by both Admin and User guards instead of duplicate Controller and View for both Guards.
推荐答案
我通过添加第二组路由来完成类似的操作,例如:
I did something similar by adding a second set of routes, like this:
<?php
Route::middleware(['auth:admin_api'])->group(function () {
Route::prefix('admin')->group(function () {
Route::name('api.admin.')->group(function () {
////////////////////////////////////////////////////////////
/// PLACE ADMIN API ROUTES HERE ////////////////////////////
////////////////////////////////////////////////////////////
Route::apiResource('test','App\Http\Controllers\API\MyController');
////////////////////////////////////////////////////////////
});
});
});
Route::middleware(['auth:api'])->group(function () {
Route::name('api.')->group(function () {
////////////////////////////////////////////////////////////
/// PLACE PUBLIC API ROUTES HERE ///////////////////////////
////////////////////////////////////////////////////////////
Route::apiResource('test', 'App\Http\Controllers\API\MyController');
////////////////////////////////////////////////////////////
});
});
因此,当管理员用户进入admin/test时,它将使用admin auth保护,而当普通用户进入/test时,它将使用标准auth保护.这两个都使用相同的控制器.
So when an admin user goes to admin/test, it uses the admin auth guard, and when a normal user goes to /test it uses the standard auth guard. Both of these use the same controller.
然后,我为我的应用创建了基本控制器.这是我确定使用guard来访问构造函数中的路由的方式:
I then created a base controller for my app. Here is how I determined with guard is being used to access the route in the constructor:
<?php
use Illuminate\Http\Response;
use App\Http\Controllers\Controller;
class BaseController extends Controller
{
protected $user;
protected $isAdmin = false;
public function __construct()
{
if(Auth::guard('admin_api')->check()) {
$this->user = Auth::guard('admin_api')->user();
$this->isAdmin = true;
} elseif(Auth::guard('api')->check()) {
$this->user = Auth::guard('api')->user();
$this->isAdmin = false;
} else {
return response()->json([
'message' => 'Not Authorized',
], 401);
}
}
这篇关于如何将一个Laravel控制器与多个Guard结合使用,而不是复制控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!