internetbug256's avatar

Do I have to recompile something if I add a new route to a laravel nova component?

After near 5 years of using Nova, and near 1 year of developing custom components for Nova, I am starting to suspect I am not using the right tool. I am already learning FilamentPHP. Anyways, to the point.

I have a nova custom component on a project running smoothly. I tried to re-use exactly the same one by just adding a couple of new routes in the component api.php. However, those new routes do not even appear when running a "php artisan route:list".

So my question is the one in the title. Am I missing something? I tried already to run an npm run dev, composer dump-autoload, cleared caches, etc. Thanks in advance

0 likes
7 replies
LaryAI's avatar
Level 58

When you add new routes to a Laravel Nova custom component, you typically don't need to recompile your JavaScript assets unless you've made changes to the JavaScript or Vue components themselves. However, since you're dealing with routes, you should ensure that the routes are properly registered within your Laravel application.

Here are the steps you should follow to ensure your new routes are recognized:

  1. Register the Routes: Make sure that the new routes are correctly defined in your api.php or whichever routes file you're using for your Nova component. The routes should be loaded by your service provider.

  2. Clear Route Cache: Laravel caches the route configuration, so you should clear the route cache to ensure that your new routes are picked up. Run the following command:

php artisan route:clear
  1. Re-cache Routes: If your application is in production, you might want to re-cache the routes after clearing them:
php artisan route:cache
  1. Check Service Provider: Ensure that the service provider responsible for loading your custom component's routes is properly set up and is bootstrapping the routes file. It should be something like this in your service provider's boot method:
$this->loadRoutesFrom(__DIR__.'/../routes/api.php');
  1. Ensure Proper Namespacing: If your routes are namespaced, make sure that the namespace is correct and that it corresponds to the controllers' namespace.

  2. Composer Autoload: Since you've already run composer dump-autoload, it's unlikely that autoloading is the issue, but it's good that you've checked this.

  3. Check for Typos: Double-check your route definitions for any typos or errors that might prevent them from being registered correctly.

  4. Permissions: Ensure that the file permissions are correct and that the web server can read the routes file.

If you've gone through all these steps and the routes are still not showing up, you might want to check the following:

  • Middleware: Ensure that any middleware applied to the routes is not preventing them from being registered or accessed.
  • Route Service Provider Order: The order of service providers in your config/app.php file can sometimes affect the registration of routes. Make sure that your custom service provider is listed after NovaServiceProvider.

If you've done all of the above and the routes are still not showing up in php artisan route:list, you may want to create a support post on the Laravel Nova issues page or check the documentation for any additional steps specific to Nova components.

Remember, if you've made changes to the JavaScript or Vue components, you will need to recompile your assets with:

npm run dev

Or, for production:

npm run prod

This is only necessary if the JavaScript or Vue components have been modified, not for changes to PHP routes.

internetbug256's avatar

Thank you Mr.AI, but none of your suggestions solved my issue. I am looking for help in the Nova Discord Channel. Any human being experiencing the same problem out there? Regards

internetbug256's avatar

Following up. Unexpectedly I tried this on the production site, and the problem does not appear! Let me put it in a simple step by step process:

  1. find any api.php script for a nova-component you built and working fine.
  2. Check with "php artisan route:list" that the routes inside this API appear listed.
  3. Then, add any testing new route to that api.
  4. Try again the route:list, without re-compiling anything inside the component.

In a production environment, this worked fine. This is, the new testing route instantly appears listed. In my local environment (a docker instance, BTW), the route:list artisan simply REFUSES to acknowledge not only the new routes, but ANY change to the route.

"Cache!" you think. Sure. Well, believe me I've tried each and every trick to clear cache. Nothing. It does not go away, it does not give a damn error. EVEN if I leave a PHP syntax error.

Please enlight me! Which cache I am forgetting?

jlrdw's avatar

Of course clear route cache, but try deleting the two php files in bootstrap\cache. Don't delete .gitignore.

internetbug256's avatar

@jlrdw , thanks. Yup, already tried that. Both are magically rebuilt, and exactly as they were before.

internetbug256's avatar

Let's broaden the question: has anyone dealt with incredible persistent cache situations when building or changing Laravel Nova custom tools? Let me describe my miserable situation:

  • I have a nova-component card built from scratch, with the typical: php artisan nova:card acme/analytics
  • Development used to go fine. I leave the npm run watch running as it should.

Maybe months after, I have to make a small touch to it, then the nightmare begins:

  • No matter what I touch, vue.js source, or the route api.php, NOTHING seems to reflect on my custom tool.
  • Yes, I ran npm run watch
  • Yes, I cleared cache in all the possible combinations
  • yes, I deleted node_modules/ directory and ran npm installagain

Unfortunately I have no time now to investigate further, like starting a new custom tool and copy all the stuff of the old one. That used to work in the past.

I'm afraid this is bye-bye to Nova, and Hello! to FilamentPHP, which I am already testing and works like a charm.

Regards

Please or to participate in this conversation.