There’s no way that’s the best solution in this case.
For this you can put the check in the controller, or in a simple way on the route as mentioned in the question:
Route::get('/fotos/{user_id}', function() {
if(Auth::user()->id != request()->user_id) {
// não é... redirecionar sujeito
}
// é o utilizador login, continuar lógica, buscar as fotos do utilizador com este id
});
But attention:
Warmly use middleware for this, and Salable already has one by default:
/app/Http/Middleware/Redirectifauthenticated.php
That is, assuming that everything is okay with your authentication system, and if only the login user can access this route ("... users where user_id is equal to the login user id."), you can/should do:
Route::get('/fotos/user', function() {
// se isto for executado é porque o utilizador está login
$id_user = Auth::user()->id;
// continuar lógica, buscar as fotos do utilizador com este id
})->middleware('auth'); // auth é o alias para o middleware descrito em cima, RedirectIfAuthenticated
DOCS
I noticed comments on the question (these details should be in the question itself), that you want to make a set of routes have the same processing, auth
in this case, then you can group them (very well said by @Virgilio Novic) in the same middleware, in this case all routes of the group will be processed by middleware auth
:
Route::group(['middleware' => ['auth']], function() {
Route::get('/fotos/user', function() {
// se isto for executado é porque o utilizador está login
$id_user = Auth::user()->id;
// continuar lógica, buscar as fotos do utilizador com este id
});
Route::get('/perfil/user', function() {
// se isto for executado é porque o utilizador está login
});
});
DOCS
the technique is
middleware
. https://laravel.com/docs/5.4/middleware– novic
can I join middleware and Service Provider? wanted every route that receives the same parameter to do a validation, example every route that receives user_id has the same id validation.
– Kaninchem
There is another technique
Route::group
put all the routes inside of it, which has this logic, and the middleware for them in thegroup
. that is to say,middleware
andRoute::group
– novic
This link to
middleware
withRoute::group
=> https://laravel.com/docs/5.4/middleware#middleware-groups– novic