2

我正在制作一个检查锦标赛所有权的中间件。

所以在网址中,我有:

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

我需要在 $tournamentId 中获得“1”

在中间件中,我只有 $request 和 $closure 参数,所以我尝试了

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

正如@Amir Bar 所说:

$tournamentId = $request->tournaments;

我检查了路线

php artisan route:list

我得到

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 |

但没有任何效果...

我的中间件:

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);
    }
}

知道我该怎么做吗???

4

2 回答 2

5

如果您的路线如下所示:

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

在中间件中执行此操作:

 $tournamentId =  $request->tournamentId

如果您的路线是这样的:

Route::resource('name')

做这个:

$tournamentId =  $request->name;

您也可以这样做以获取有关正在发生的事情的更多信息:

dd($request->route()->parameters());
于 2015-12-26T18:41:39.193 回答
0

这一直是我的解决方案。

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);
}

事情是中间件被调用了两次,第一次,它什么也没有。我完全不知道为什么...

所以基本上,解决了,但不明白为什么会发生,所以解决了一半:(

感谢@Amir Bar 帮助我!

于 2015-12-26T20:24:06.803 回答