Create Url In Console Applications

Hello,

I’am using

in console application for create url. But I don’t find how resolve

It is telling you already what to do.

In application config you have to configure urlManager property baseUrl because in console it can not detect applicatiosn base URL by itself.




'components' => [

    // ...

    'urlManager' => [

        'baseUrl' => 'http://example.com/'

    ]

]



I understand the limitations and why we would need to enter this manually. However it’s -very- painful as far as deployments and various environments go. Do you guys have any suggestions on how to handle such scenarios?

In my case I already have a custom init console controller/action that needs to be run on installation so I could probably have it prompt the user for the domain, but of course this is no good for automated testing environments. Do you have any better suggestions? (maybe I should make a test app environment for ./init?)

In CLI mode you don’t have HTTP request, thus your webserver will never be able to set some of environmental variables, e.g. $_SERVER[‘SERVER_NAME’] from which URL is normally constructed. You must always set it manually by loading correct configuration for current environment. It’s not an issue with framework or PHP :slight_smile:

The URLs should not have a trailing “/” as in the examples provided. Also you may need both the baseUrl and scriptUrl. Lastly you will need to enable prettyUrl, if you have that enabled in the web application.

        'urlManager' => [
            'enablePrettyUrl' => true,
            'baseUrl' => 'http://example.org',
            'scriptUrl' => 'http://example.org'
        ],