Framework: route method usage in configs

Created on 16 Apr 2017  路  7Comments  路  Source: laravel/framework

  • Laravel Version: 5.4.18
  • PHP Version: 7.1

Description:

Not sure is it bug or misuse.
I use configs.php which is loaded in boot method of service provider, and config contains method route, so cannot resolve urls. Error visible:

InvalidArgumentException in UrlGenerator.php line 304:
Route [module-action] not defined.

This is due nameList variable, which is still empty in configs, I debugged and removed route temporary, to see when it is filled,so only after refreshNameLookups call nameList is full.

Question would be, should I remove route from configs, as it is forbidden, or what to do?

My RouteServiceProvider extends Illuminate\Foundation\Support\Providers\RouteServiceProvider, so I see that boot method is different loading than my configs, maybe I should wrap $this->app->booted(function () {}); also?

    public function boot()
    {
        $this->setRootControllerNamespace();

        if ($this->app->routesAreCached()) {
            $this->loadCachedRoutes();
        } else {
            $this->loadRoutes();

            $this->app->booted(function () {
                $this->app['router']->getRoutes()->refreshNameLookups();
                $this->app['router']->getRoutes()->refreshActionLookups();
            });
        }
    }

Steps To Reproduce:

MyServiceProvider.php

class MyServiceProvider extends ServiceProvider
{
    protected $defer = false;
    public function boot(): void
    {
        $this->mergeConfigFrom(__DIR__ . '/../Config/config.php', 'module');
    }
    public function register(): void
    {
        $this->app->register(RouteServiceProvider::class);
    }
}

web.php

Route::get('action', 'MyController@action')->name('module-action');

config.php

return [
    'url_action' => route('module-action'),
];

Most helpful comment

You can just use name of route in config?and then call route and pass it where you need it

All 7 comments

One more sample:

Argument 2 passed to Illuminate\Routing\UrlGenerator::__construct() must be an instance of Illuminate\Http\Request, null given, called in /vagrant/laravelproject/vendor/laravel/framework/src/Illuminate/Routing/RoutingServiceProvider.php on line 64
/vagrant/laravelproject/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php:101
/vagrant/laravelproject/vendor/laravel/framework/src/Illuminate/Routing/RoutingServiceProvider.php:64
/vagrant/laravelproject/vendor/laravel/framework/src/Illuminate/Container/Container.php:716
/vagrant/laravelproject/vendor/laravel/framework/src/Illuminate/Container/Container.php:598
/vagrant/laravelproject/vendor/laravel/framework/src/Illuminate/Container/Container.php:567
/vagrant/laravelproject/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:708
/vagrant/laravelproject/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:107
/vagrant/laravelproject/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:741
/vagrant/laravelproject/config/config.php:43
/vagrant/laravelproject/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/LoadConfiguration.php:70
/vagrant/laravelproject/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/LoadConfiguration.php:39
/vagrant/laravelproject/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:208
/vagrant/laravelproject/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:270
/vagrant/laravelproject/tests/CreatesApplication.php:32
/vagrant/laravelproject/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:89
/vagrant/laravelproject/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:66

Reproducable with:

class MyControllerTest extends TestCase
{
    /**
     * @test
     */
    public function it_must_fetch(): void
    {
        $this->assertNotNull(config('config.url_action'));
    }
}

It doesn't even call test method, fails on loadConfigurationFiles method in createApplication call of CreatesApplication trait

Assuming I'm understanding what you're trying to do, configuration files are loaded before the service container starts booting service providers, which is where routes are parsed and built. I don't think you can use route() in config and get the results you expect.

So how to achieve, that url would be the same in route and config, having nothing in common? This is the issue. First example is when config is in project, second - in vendor directory.

@themsaid, question still relevant and unanswered :)
So, how to use route function in config file, while bot requires that application would be booted?

You can't. As stated before, the config is one of the first things that is parsed, well before routes are available. It's simply not possible without a significant change to the framework architecture.

And what about vice versa, using the routes as defered and then could use config in them? I guess that switching the lines would be that workflow?

            $this->app->booted(function () {
                $this->loadRoutes();
                $this->app['router']->getRoutes()->refreshNameLookups();
                $this->app['router']->getRoutes()->refreshActionLookups();
            });

RouteServiceProvider boot method

You can just use name of route in config?and then call route and pass it where you need it

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kerbylav picture kerbylav  路  3Comments

felixsanz picture felixsanz  路  3Comments

JamborJan picture JamborJan  路  3Comments

PhiloNL picture PhiloNL  路  3Comments

lzp819739483 picture lzp819739483  路  3Comments