Laravel - Artisan gives an invalid base url

There is /config/app.php in my application

'url' => 'http://dev.domain.com/something/somethingElse' 

Then I have a function that can be called from the application, as well as from the artisan command. But URL::route('myRoute') returns different results. When called from the application, it returns http://dev.domain.com/something/somethingElse/myRoute , but in the artisan command http://dev.domain.com/myRoute .

URL::to has the same behavior.

Note. I do not have another app.php file for other environments that can overwrite the global one.

Any ideas why this is happening? Thanks!

+6
source share
2 answers

A URL created using Laravel consists of several parts:

  • Schema: http:// , https:// etc.
  • Host: dev.domain.com , localhost , etc.
  • Base: something/somethingElse (subdirectory on the web server)
  • Tail: myRoute , (Laravel route parameters)

These parts are then combined to form a URL.

Ultimately, Laravel generates the URL using the $_SERVER query $_SERVER . The prepareBaseUrl() function in Symfony\Component\HttpFoundation\Request is what is ultimately used to determine the base portion of the URL.

When you make a request through your web browser, the request is sent to ~/public/index.php , and the necessary $_SERVER are populated with the correct information for Laravel to fill in the base part of the URL.

However, when you make a request using Artisan on the command line, the request goes to ~/artisan script. This means that the $_SERVER not populated the same way, and Laravel cannot determine the base portion of the URL; instead, it returns an empty string. ''

From what I can find, it doesn't look like the Laravel team has an appetite so that the application can function out of the box in a subdirectory, for example. Bugfix: domain routing for a subfolder .

I ended up working on this method described by @medowlock for my scripts that will be invoked from the command line, for example:

Config::get('app.url') . URL::route('route.name', ['parameter'=>'value'], false)

This combines the application URL specified in app/config/app.php and the relative URL route.

+10
source

If you are using Laravel 4.2, you can call the URL::forceRootUrl function to explicitly determine which root URL of your laravel application is.

 URL::forceRootUrl( Config::get('app.url') ); 

URL::to calls will use the URL definition in your application configuration file after forcing the root URL.

Unfortunately, this is not possible in Laravel 4.1 or Laravel 4.0

+6
source

Source: https://habr.com/ru/post/975037/


All Articles