cloudcreativity/laravel-json-api

Question about Custom Actions

spuxx1701 opened this issue · 2 comments

Hey there!

I'm attempting to set up a custom action for my resource 'email-verifications'. It should simply trigger re-sending the email that is used to verify a new account.

I feel I've set up everything according to the docs, but I'm still getting 'NotFoundHttpException' exceptions. This is what my api looks like:

    JsonApi::register('default')->withNamespace('App\Http\Controllers\Api')->singularControllers()->routes(function ($api) {
        [...]
        $api->resource("email-verifications")->controller()->routes(function ($emailVerifications) {
            $emailVerifications->get('{record}/request', 'request');
        });
    });

My EmailVerificationController has this method:

    public function request(FetchResource $request): \Illuminate\Http\Response
    {
        [...]
    }

And I should also note that the basic CRUD stuff is working fine, e.g. this request:

http://.../api/v1/email-verifications/<record>

But this request throws an exception:

http://.../api/v1/email-verifications/<record>/request

Any idea what I'm doing wrong? Also: I wouldn't want 'record' to be the actual ID, but another property - can I even try to catch that myself or will my request be turned down before I can even try to process it?
Anyway, I'd still like to get it right because I'd like to use that functionality later in cases where I actually do have the correct id.

I think a starting point with this is to see what routes are registered using php artisan route:list. Can you narrow down what the route is and that the request is right?

Thanks for the quick reply. So, the route didn't show up in route:list, so it obviously wasn't registered correctly. I tried running php artisan route:cache and when I did, laravel threw an exception. Turned out I hade a spelling mistake in the route/api file that prevented any new routes from being registered (another controller was addressed with ../htttp/...).

Unfortunately (and as I expected), laravel-json-api throws a ResourceNotFound exception long before I can try to handle the request myself if it doesn't find a record with the {record} id, so I'm going to have to use a standard laravel route and controller.

Thanks again and have a great weekend. :)