Home > database >  How to group routes by API Laravel?
How to group routes by API Laravel?

Time:10-16

Here is a route rules:

Route::apiResources([
    'profile' => ProfileController::class,
    'specialization' => SpecializationController::class,
    'specialization/filter' => SpecializationController::class,
]);

I try to add a custom route into apiResource above:

'register/code/verify' => [RegisterVerifyController::class, 'verify']

As result I got this:

Route::apiResources([
    'profile' => ProfileController::class,
    'specialization' => SpecializationController::class,
    'specialization/filter' => SpecializationController::class,
     'register/code/verify' => [RegisterVerifyController::class, 'verify']
]);

It does not work. How to do that to be able call route with api/ prefix like apiResources?

CodePudding user response:

When you define routes inside the routes/api.php file, all routes are automatically prefixed with api/. The likely cause of your issue is the following route definition:

'register/code/verify' => [RegisterVerifyController::class, 'verify']

You're not able to specify particular actions for a route when using the apiResource or apiResources route facades. Each array element you pass to apiResouces should be a kvp (key-value pair) where the key is a string specifying the route and the value is also a string specifying the Controller that route maps to (see api docs). What you've done with the above is provide the value as an array, which results in an ErrorException due to an Array to string conversion.

What you can do is the folllowing:

Route::apiResources([
    'profile' => ProfileController::class,
    'specialization' => SpecializationController::class,
    'specialization/filter' => SpecializationController::class,
]);

Route::match(
    ['GET', 'POST'],
    'register/code/verify', 
    [RegisterVerifyController::class, 'verify']
);

I don't know the HTTP verbs you want to use with the verify route, so I have used match with both GET and POST but you can replace that with whatever works for you.

If you have other actions inside your RegisterVerifyController other than verify and still want to use it as an apiResource, you can add it back in to your group, just don't specify any actions:

Route::apiResources([
    'profile' => ProfileController::class,
    'specialization' => SpecializationController::class,
    'specialization/filter' => SpecializationController::class,
    'register/verify' => RegisterVerifyController::class,
]);

CodePudding user response:

By default, if you define your route codes in .../routes/api.php file, the api/ prefix is assigned to your routes by default. But from your code above it seems you are defining your routes in the routes/web.php file(just a guess). A way to add the prefix to your route would be this way:

Route::group(['prefix' => 'api'], function() {
Route::apiResources([
    'profile' => ProfileController::class,
    'specialization' => SpecializationController::class,
    'specialization/filter' => SpecializationController::class,
     'register/code/verify' => [RegisterVerifyController::class, 'verify']
]);
});

UPDATE: Define your routes this way

Route::get('profile', [ProfileController::class]);
Route::get('specialization', [SpecializationController::class]);
Route::get('specialization/filter', [SpecializationController::class]);
Route::get('register/code/verify', [RegisterVerifyController::class, 'verify');

The api/ prefix is added automatically

  • Related