【发布时间】:2020-11-23 18:01:46
【问题描述】:
我有一个有多个子域的项目。 例如,我有一个学生子域,它转到学生控制器,它看起来像这样:
Route::domain('students.domain.test')->group(function () {
Route::get('/', function () {
return "done reaching the students page";
});
});
第二种类型的域是“domain.test”和我在请求级别检查的任何子域,这也很好。
Route::get('/', [HomeController::class, 'index'])->name('index');
但在第二种类型的域之前,我想为我在数据库中拥有的特定类型的实体创建子域。
Route::domain('{someTypes}.domain.test')
->group(function () {
Route::get('/', function () {
return "done reaching SomeTypes Page";
});
});
我的Entity表有这些属性:Id、Title、Type“我要检查的类型是否为5”。
我尝试使用中间件:
public function handle($request, Closure $next, ...$types)
{
$currentEntity = app('current_entity');
if ($currentEntity->entityType()->whereIn('title->en', $types)->exists()) {
return $next($request);
}
abort(404, 'Sorry, Request Not Found');
}
我将它应用到我的路线中:
Route::group([
'middleware' => ['type:journal']
],function () {
Route::get('/', function(){
return 'journals logic goes here';
});
});
我还有另一个中间件可以忽略这样的类型:
public function handle($request, Closure $next, ...$types)
{
$currentEntity = app('current_entity');
if ($currentEntity->entityType()->whereIn('title->en', $types)->exists()) {
abort(404, 'Sorry, Request Not Found');
}
return $next($request);
}
并将其应用于其他路线,如下所示:
Route::group([
'middleware' => ['except_entity:journal']
], function(){
Route::get('/', function(){
return 'default pages when journals fails';
})->name('index');
我希望它清楚我想要实现的目标。
【问题讨论】:
-
你遇到了什么问题?
-
我想检查“someType”是否是类型为 5 而不是任何其他类型的实体上的 id
-
然后创建一个中间件并执行检查
-
我有一个中间件,但它在第一种类型上中止了 404,如果类型检查失败,我可以让它将结果传递给其他路由吗?
-
我认为您应该尝试使用中间件,如果不起作用,请在
SO中询问