简体   繁体   中英

Laravel Route-Model binding doesn't work if route is defined in route group

i have this route,

Route::get('/posts/show/{post}', 'PostsController@show');

//corresponding controller method
public function show(Post $post){
  //method logic
}

this works perfectly when route is defined outside of Route::group.

But this fails;

Route::group(['domain' => '{user}.localhost.com'], function () {
...
    Route::get('/posts/show/{post}', 'PostsController@show');
...
}

Erro Output;

 Argument 1 passed to App\Http\Controllers\PostsController::show() must be an instance of App\Post, string given

To see what was being passed as parameter, i modified the PostsController::show() to below;

public function show($post){
    return $post;
}

//it returned the subdomain part of the url.

I can safely say route group is working as intended because i have other routes and they work. (as long as they don't use Route-Model binding ofc)

i have found 2 other posts addressing the same issue but they didn't helped me with this.

You have created a subdomain wildcard route which accepts one parameter which is the {user} and inside your route group you are accepting another parameter which is the {post} if you want to use route model binding, arrange the parameters in the appropriate order. example $user , $post

So your controller should look like

public function show($user,Post $post){
  //method logic
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM