Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle nova config file in tools routing stubs #6106

Closed
RomainMazB opened this issue Dec 13, 2023 · 4 comments
Closed

Handle nova config file in tools routing stubs #6106

RomainMazB opened this issue Dec 13, 2023 · 4 comments
Labels
needs more info More information is required

Comments

@RomainMazB
Copy link

RomainMazB commented Dec 13, 2023

  • Laravel Version: 10.37.1
  • Nova Version: 4.32.9
  • PHP Version: 8.2

Description:

When creating our fist Tool, the documentation states:

The route group specifies that all "API routes", which will typically be invoked from the client via Nova.request, should receive a /nova-vendor/tool-name URL prefix, where tool-name is the "kebab-case" name of your tool.

As expected, the stub for the Tools prefix the route group with the /nova-vendor/tool-name part.

But, when using Nova.request, the config nova.path (which defaults to /nova) is prepended to the request, and I guess (not tested) that the nova.domain is also supported by the javascript library.

I suggest that the stub should include the config path and domain to avoid losing time to figure why we get a 404 with a basic routing file for our tool.

@crynobone
Copy link
Member

It not clear what's the issue here, please provide full reproducing repository based on fresh installation as suggested in the bug report template (or you can refer to https://github.com/nova-issues for example)

@crynobone crynobone added the needs more info More information is required label Dec 14, 2023
@RomainMazB
Copy link
Author

Hi @crynobone
I guess these snippets of code are enough to understand:

This is the default routes method of the ToolServiceProvider when using the nova:tool command:

protected function routes()
{
  if ($this->app->routesAreCached()) {
    return;
  }

  Nova::router(['nova', Authenticate::class, Authorize::class], 'world-creator')
    ->group(__DIR__.'/../routes/inertia.php');

  Route::middleware(['nova', Authorize::class])
    ->prefix('nova-vendor/world-creator') // <--- Here is the problem
    ->group(__DIR__.'/../routes/api.php');
}

I created a basic route inside my tool routes/api.php file:

use MazLaboratory\WorldCreator\Http\Controllers\WorldCreator;

Route::put('/create', [WorldCreator::class, 'create']);

And inside my Tool.vue frontend file, I want to call this create controller method using the recommended Nova.request library:

export default {
  data: () => ({
    formData: {
      world_name: '',
      map_name: '',
      maximum_players: undefined,
      map_width: undefined,
      map_height: undefined,
    }
  }),
  methods: {
    async createWorld() {
      await Nova.request().put(`nova-vendor/world-creator/create`, this.formData)
        .then(response => Nova.success(response.data))
        .catch(error => Nova.error(error.response.data))
    }
  }
}

Reading the Tool documentation and trusting the ToolServiceProvider stub, this should work ...
But it fails into a 404 because the route group is not prefixed by the config nova.prefix and nova.domain which looks like to be included inside Nova.request but not inside the ToolServiceProvider stub!
image

The final working code for my ToolServiceProvider looks like this and this is how I expected the stub to be:

Route::middleware(['nova', Authorize::class])
    ->prefix(config('nova.path').'/nova-vendor/world-creator')
    ->group(__DIR__.'/../routes/api.php');

In my case, the nova.domain config is null but I guess that I would have to add ->domain(config('nova.domain')) to this code if not.

@crynobone
Copy link
Member

nova-vendor normally should be the at the root level similar to nova-api and configurable nova

@RomainMazB
Copy link
Author

RomainMazB commented Dec 14, 2023

!!!! Holy s...

My bad! I looked into public packages to understand why they did not faced this problem or how did they solved it.
My mistake was that I did not prepended the Nova.request url with a slash. Making the request called from the actual "directory" (/nova) and not from the root level...

// This            vvv
Nova.request().put(`/nova-vendor/world-creator/create`, this.formData)

// Instead of this:
Nova.request().put(`nova-vendor/world-creator/create`, this.formData)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs more info More information is required
Projects
None yet
Development

No branches or pull requests

2 participants