2

I'm making a middleware that check the ownership of an tournament.

So in the url, I have :

http://laravel.dev:8000/tournaments/1/edit

I need to get the "1" in $tournamentId

In the middleware, I only have $request and $closure parameters, so I tried

$tournamentId = $request->get("tournaments");

and as @Amir Bar says:

$tournamentId = $request->tournaments;

I checked the routes with

php artisan route:list

And I get

GET|HEAD | tournaments/{tournaments}        | tournaments.show         | App\Http\Controllers\TournamentController@show      | auth,roles,ownTournament |
DELETE   | tournaments/{tournaments}        | tournaments.destroy      | App\Http\Controllers\TournamentController@destroy   | auth,roles,ownTournament |
PATCH    | tournaments/{tournaments}        |                          | App\Http\Controllers\TournamentController@update    | auth,roles,ownTournament |
PUT      | tournaments/{tournaments}        | tournaments.update       | App\Http\Controllers\TournamentController@update    | auth,roles,ownTournament |
GET|HEAD | tournaments/{tournaments}/edit   | tournaments.edit         | App\Http\Controllers\TournamentController@edit      | auth,roles,ownTournament |          | auth,roles,ownTournament |

but nothing works...

My middleware:

class OwnTournament
{
    /**
     * Check the ownership of tournaments
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        dd($request->route());
        return $next($request);
    }
}

Any idea how should I do it???

2 Answers 2

5

if your route look like:

 Route::get('tournaments/{tournamentId}/edit');

do this in the middleware:

 $tournamentId =  $request->tournamentId

if your route is like:

Route::resource('name')

do this:

$tournamentId =  $request->name;

you can also do this to get more info about whats happening:

dd($request->route()->parameters());
Sign up to request clarification or add additional context in comments.

6 Comments

it didn't work :( I use Route::resource('tournaments', 'TournamentController'); in my routes.php
update my question too. dd($request->route()->parameters()); gives me an error.
what dd($request->route()); give you?
can you please post your middleware?
first instruction is dd,is it still empty
|
0

This has been my solution.

public function handle($request, Closure $next)
{
    if (Auth::check()) {
        $user = Auth::user();
        $tournaments = $user->tournaments;
        $tournament = null;
        if ($request->tournaments != null || $request->tournamentId != null) {
            $tournament = $request->tournaments;
            if ($tournament != null) {
                if (!$tournaments->contains($tournament)) {
                    return "You don't have privileges to access this resource";
                }
            }
        }
    }


    return $next($request);
}

Thing is middleware is called twice, and the first time, it has access to nothing. I don't exactly know why...

So basically, resolved, but without understanding why it happens, so half resolved :(

Thanks @Amir Bar for helping me!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.