Home > Software engineering >  Invalid Route Action When Routing invokable class
Invalid Route Action When Routing invokable class

Time:08-12

iam new in laravel , and i wrote this code at routes/api.php in laravel 9

Route::group([
    'prefix' => 'auth',
    'namespace' => 'Auth'
], function(){
    Route::post('register', 'RegisterController');
});

and then i got cant run php artisan serve , it said

 UnexpectedValueException 

  Invalid route action: [Auth\RegisterController].

  at G:\PRODUCTIVITY\SANBERCODE\LARAVEL-VUE\TUGAS\laravel-vue-crowdfunding-website-batch-37\crowdfunding-website\vendor\laravel\framework\src\Illuminate\Routing\RouteAction.php:92
     88▕      */
     89▕     protected static function makeInvokable($action)
     90▕     {
     91▕         if (! method_exists($action, '__invoke')) {
  ➜  92▕             throw new UnexpectedValueException("Invalid route action: [{$action}].");
     93▕         }
     94▕
     95▕         return $action.'@__invoke';
     96▕     }

  1   G:\PRODUCTIVITY\SANBERCODE\LARAVEL-VUE\TUGAS\laravel-vue-crowdfunding-website-batch-37\crowdfunding-website\vendor\laravel\framework\src\Illuminate\Routing\RouteAction.php:47
      Illuminate\Routing\RouteAction::makeInvokable("Auth\RegisterController")

  2   G:\PRODUCTIVITY\SANBERCODE\LARAVEL-VUE\TUGAS\laravel-vue-crowdfunding-website-batch-37\crowdfunding-website\vendor\laravel\framework\src\Illuminate\Routing\Route.php:190
      Illuminate\Routing\RouteAction::parse("api/auth/register", ["Auth\RegisterController", "Auth\RegisterController"])

someone please help me :)

CodePudding user response:

Add RegisterController function

     Route::group([
    'prefix' => 'auth',
    'namespace' => 'Auth'
], function(){
    Route::post('register', 'RegisterController@store');
});

CodePudding user response:

You are missing a parameter in your post function from Route.

You want something like

Route::post('route_name', 'Controller@myFunction')

Or in your case:

Route::post('register', 'RegisterController@registerFunctionName');

Other variation per 9.x documentation:

Route::post('register',  [RegisterController::class, 'registerFunctionName']);

Please refer to: https://laravel.com/docs/9.x/routing

CodePudding user response:

This is an invokable controller yes?

you need to just alter the syntax

Route::group([
    'prefix' => 'auth',
    'namespace' => 'Auth'
], function(){
    Route::post('register', [RegisterController::class]);
});

and then import the class at the top of your routes file and make sure you have a single public method of __invoke() in your controller.

  • Related