Search code examples
laravelapisigned-url

Laravel's signedURL generates wrong URL when using api middlware


I have a front-end running on Nuxt and Laravel as a back-end service. When I generate the signedURL using the Laravel's API middleware - the path includes "api" in the URL, resulting into a page not found exception on the Nuxt side

So, here are the steps to better understand what's happening:

  1. User clicks a button in Nuxt application and sends the ajax request to the Laravel API
  2. API Controller generates the signedURL

$signedUrl = URL::signedRoute('register', ['email' => $this->request->email, 'group_id' => $this->request->group_id], null, false);

  1. Generated URL includes the "api" in the path, which of course, cannot be accessed http://localhost:3000/api/register?email=ss%40gmail.com&group_id=2&signature=ce4fba05bf5ccae6ea20a6043a47ca11de603238214deda7202d19f2989272cb

Is there a way to get rid of the /api/ from the generated URL? I've tried setting 4th param (absolute) in the method signedRoute to false, but it doesn't help.


Solution

  • The default api routes have prefix 'api' as seen in your RouterProvider:

        protected function mapApiRoutes()
        {
            Route::prefix('api')
                   ...;
        }
    

    When you generate the signed URL, for your route 'register' which is using the prefix api , the generated URL will be as expected: www.mydomain.com/api/register?...