简体   繁体   中英

How to return a redirect to a named route with a variable in Laravel 9?

so this is what I tried (doesn´t work). I am happy with any advice:)

Controller:

public function show($id){
        $URL = route('post', ['id' => $id]);
        
        return redirect($URL);
    }

Route:

Route::get('/post/{id}', [postController::class, 'show'])->name('post');

From the docs: generating-urls-to-named-routes To redirect to a named route we use: return redirect()->route('post', ['id' => $id]);

public function show($id){
   return redirect()->route('post', ['id' => $id]);
}

However, This implementation will throw an error. It's a loop. The route (in web.php) calls show() in the controller. And, the controller redirects back to the same controller. it doesn't make any sense.

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