Reputation: 345
I am trying to redirect the page from a controller to an action in another controller in Laravel 5.3
. The error returned is:
InvalidArgumentException in UrlGenerator.php line 605:
Action App\Http\Controllers\StartChoosingController@index not defined.
My codes are as follows:
As I looked over the internet to find an answer I could discover that my problem might be because of not having used the proper namespacing
:
https://stackoverflow.com/questions/29822302/laravel-action-not-defined
Would you please tell me how and what to add to the namespace
of use
part of my code to fix the issue? Thank you very much in advance.
Upvotes: 3
Views: 6034
Reputation: 101
If you are using:
redirect()->action([App\Http\Controllers\StartChoosingController::class, 'index']);
please make sure that the above App\Http\Controllers\StartChoosingController::class is 'assigned' to an actual URL so that Laravel knows where actually do redirect.
Here is the full working example: web.php
Route::get('some', function () {
return redirect()->action([App\Http\Controllers\StartChoosingController::class, 'index']);
});
Route::get('some/index', [App\Http\Controllers\StartChoosingController::class, 'index']);
app/Http/Controllers/StartChoosingController.php
<?php
namespace App\Http\Controllers;
class StartChoosingController extends Controller
{
public function index()
{
return 'hi';
}
}
Upvotes: 0
Reputation: 1448
For my fellow Laravel 8 users, if you're experiencing the same issue try return redirect()->action([TargetController::class, 'controller_method']);
Upvotes: 1
Reputation: 163798
If you're using Route::resource()
for controller routes, try to change index()
method to showAll()
and add parameter:
public function showAll($userTableData)
And use it:
redirect()->action('StartChoosingController@showAll', ['userTableData' => $user_table_data]);
Also, you'll need to define new route:
Route::get('show-all/{userTableData}', 'StartChoosingController@showAll')
If userTableData
is not a string, but an object, you should pass data with post method and hidden inputs.
Upvotes: 2