简体   繁体   中英

How to do a simple redirect in Laravel?

I have a function in Laravel. At the end I want to redirect to another function. How do I do that in Laravel?

I tried something like:

return redirect()->route('listofclubs');

It doesn't work. The route for "listofclubs" is:

Route::get("listofclubs","Clubs@listofclubs");

If you want to use the route path you need to use the to method:

return redirect()->to('listofclubs');

If you want to use the route method you need to pass a route name , which means you need to add a name to the route definition. So if modify your route to have a name like so:

// The `as` attribute defines the route name
Route::get('listofclubs', ['as' => 'listofclubs', 'uses' => 'Clubs@listofclubs']);

Then you can use:

return redirect()->route('listofclubs');

You can read more about named routes in the Laravel HTTP Routing Documentation and more about redirects in the Redirector class API Documentation where you can see the available methods and what parameters each of them accepts.

Simply name your route:

Route::get('listofclubs',[
    'uses' => 'Clubs@listofclubs',
    'as'   => 'listofclubs'
]);

Then later

return redirect()->route('listofclubs');

One method is to follow the solution provided by others. The other method would be, since you intend to call the function directly then you can use

return redirect()->action('Clubs@listofclubs');

Then in route file

Route::get("listofclubs","Clubs@listofclubs");

Laravel will automatically redirect to /listofclubs.

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