Reputation: 16415
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");
Upvotes: 3
Views: 199
Reputation: 44586
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.
Upvotes: 5
Reputation: 2468
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.
Upvotes: 0
Reputation: 17553
Simply name your route:
Route::get('listofclubs',[
'uses' => 'Clubs@listofclubs',
'as' => 'listofclubs'
]);
Then later
return redirect()->route('listofclubs');
Upvotes: 0