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
?
2
Answers
By default, if you define your route codes in
.../routes/api.php
file, theapi/
prefix is assigned to your routes by default. But from your code above it seems you are defining your routes in theroutes/web.php
file(just a guess).A way to add the prefix to your route would be this way:
UPDATE:
Define your routes this way
The
api/
prefix is added automaticallyWhen you define routes inside the
routes/api.php
file, all routes are automatically prefixed withapi/
. The likely cause of your issue is the following route definition:You’re not able to specify particular actions for a route when using the
apiResource
orapiResources
route facades. Each array element you pass toapiResouces
should be akvp
(key-value pair) where thekey
is astring
specifying theroute
and thevalue
is also astring
specifying theController
thatroute
maps to (see api docs). What you’ve done with the above is provide thevalue
as anarray
, which results in anErrorException
due to anArray to string conversion
.What you can do is the folllowing:
I don’t know the HTTP verbs you want to use with the
verify
route, so I have usedmatch
with bothGET
andPOST
but you can replace that with whatever works for you.If you have other actions inside your
RegisterVerifyController
other thanverify
and still want to use it as anapiResource
, you can add it back in to your group, just don’t specify any actions: