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

route:cache ignores application routing configuration #451

Open
stefanfisk opened this issue Mar 19, 2025 · 5 comments
Open

route:cache ignores application routing configuration #451

stefanfisk opened this issue Mar 19, 2025 · 5 comments
Labels
bug Something isn't working

Comments

@stefanfisk
Copy link
Contributor

Version

v5.0.1

What did you expect to happen?

I'd expect route:cache to cache all configured routes.

What actually happens?

It only caches routes defined in routes/web.php.

The root cause is that GetsFreshApplication creates its application instance using the default configuration:

return $application::configure($app->basePath())
.

I discovered it because routes/api.php stopped working as soon as i cached routes.

Steps to reproduce

  1. Defined a route in routes/api.php
  2. Configure the application to use routes/api.php
  3. Run route:clear
  4. Run route:list to see your API route
  5. Run route:cache
  6. Run route:list and find that your API route is missing
  7. Run route:clear
  8. Run route:list to see your API route again

System info

Ubuntu 24.04.2

Log output

Please confirm this isn't a support request.

No

@stefanfisk stefanfisk added the bug Something isn't working label Mar 19, 2025
@retlehs
Copy link
Member

retlehs commented Mar 19, 2025

Thanks for the issue, but you'll need to update the reproduction steps with more details.

Post your routes/api.php code — we can't see it

Post your code for booting Acorn — we can't see it

@stefanfisk
Copy link
Contributor Author

Here's my routes/web.php:

<?php

use Illuminate\Support\Facades\Route;

Route::view('/welcome/', 'welcome')->name('welcome');

Here's my routes/api.php:

use Illuminate\Support\Facades\Route;

Route::get('/greeting', function () {
    return 'Hello World';
});

Here's my MU plugin:

<?php

use Roots\Acorn\Application;

add_action('after_setup_theme', function () {
    Application::configure()
        ->withRouting(
            web: base_path('routes/web.php'),
            api: base_path('routes/api.php'),
        )
        ->boot();
}, 0);

The actual contents doesn't matter. If you look at the line of code I linked, the app instance is constructed using the default config. If you compare Acorn's implementation to the original you can see that they use the actual app bootstrapper to create a new instance:

protected function getFreshApplication()
.

@Log1x
Copy link
Member

Log1x commented Mar 19, 2025

Hey. I'm aware of this but haven't figured out a solution yet. Pulling the fresh application as you can see looking at Foundation is easier said then done in this environment since Acorn can be booted from anywhere vs. having a single source of truth at bootstrap/app.php to re-require in.

@stefanfisk
Copy link
Contributor Author

@Log1x Yeah, I can see how that complicates things. How about a filter to optionally provide the path to the equivalent of bootstrap/app.php? If the filter returns null the default config is used, if not the file is required and the returned app used.

So something like add_filter('acorn/bootstrap-app-file', fn() => __DIR__ . '/app.php'); in a MU plugin, functions.php or wherever one prefers. And then those who use the filter can boot the app via add_action('after_theme_setup', function() { apply_filters('acorn/bootstrap-app-file', null)->bootAcorn(); });.

Since it would all be opt-in I don't think that it would affect people that do not use the filter.

@Log1x
Copy link
Member

Log1x commented Mar 20, 2025

I thought of that or checking for a constant. Another way might be setting the configuration state onto a property on the Application container before booting when calling ->boot() so it can be re-referenced when creating a fresh application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants