需要帮助以限制对某些 ID 的路由访问



我需要guest's只能访问链接'fixtures/1'和'fixtures/2'

当他们管理到其他id时,例如'fixtures/3',他们应该得到类似' page not found ';

我试过了:

Route::get('/fixtures/{team}', function ($team) {
//
})->where($team, '1')->name('front.fixtures');

但它不起作用。谁能告诉我怎么加上这样的从句?

这是我的路线没有任何改变:

Route::get('/fixture/{team}', 'FrontController@fixtures')->name('front.fixtures');

编辑:

我试图限制使用if,但每次我打开页面它不检查id,这是我的方法:

public function fixtures(Request $request,Team $team = null) {
$matches = Match::where('home_team_id', $team->id)
->orWhere('away_team_id', $team->id)
->paginate(20);
$teams = Team::all();
$team = $request->route()->parameter('team');
if ($request->route('team') !== '1' || $request->route('team') !== '2') {
return view('front.fixtures')->with([
'matches' => $matches,
'teams' => $teams,
]);
} else {
abort('404');
}
}

使用这个中间件代码。

public function handle(Request $request, Closure $next)
{
$auth = Auth::check();
if($auth == false){
if ($request->route('team') == '3' || $request->route('team') == '5') {
return $next($request);
} else {
abort('404');
}
} else{
return $next($request);
}
}

你的路由应该是:

Route::get('/fixture/{team}', 'FrontController@fixtures')->middleware('name of 
middleware');

不要忘记在Karnel.php中注册中间件,在受保护的$ routemmiddleware下。

阅读更多关于中间件的信息

希望对大家有帮助。

我通常做什么

在<<p> strong>应用程序/供应商/RouteServiceProvider.php
public function boot()
{
parent::boot();
Route::bind('institutionDocument', function ($id) {
$this->CheckForSignedUser();
$document = AppModelsDocument::find($di);
return $document ?? abort(404);
});
}

和在我的路线

Route::get(
"documents/{institutionDocument}/preview",
"DocumentsController@preview"
);

最新更新