bwrigley
6 months ago
165
3
Laravel

Picking up implicit model in middleware

Posted 6 months ago by bwrigley

Hello,

I'm building a simple forum and I want to write some middleware that inspects a ForumThread to make sure it's not locked/archived/etc. before accepting route requests for ForumReply.

But I'm not sure how to pick up the implicitly bound ForumThread that i'm passing in.

So my routes look like this:

 Route::middleware(['forum.thread.unlocked'])->group(function (): void {
    Route::group(['prefix' => 'forum_reply', 'as' => 'forum_reply.'], function (): void {
        Route::get('create/{forum_thread}', 'Forum\[email protected]')->name('create');
        Route::post('store/{forum_thread}', 'Forum\[email protected]')->name('store');
        Route::post('resolve/{forum_thread}', 'Forum\[email protected]')->name('resolve');
    });
 });

In my controller I pick up the ForumThread from the Request in the usual way:

    public function create(ForumThread $thread) : View
    {
    //
    }

How can I also do the same in my middleware as the Request attributes appear to be empty?

I call a url:

https://domain.test/forum/forum_reply/create/215

Middleware is invoked:

    public function handle(Request $request, Closure $next)
    {
        dump($request->attributes);
    }

Dump results are:

ParameterBag {#44 ▼ #parameters: []

Please sign in or create an account to participate in this conversation.