Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - Artisan gives wrong base url

My app/config/app.php has

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

Then I have a function that can be called from the application and also from an 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 same behaviour.

Note: I do not have any other app.php file defined for other environments that could overwrite the global one.

Any ideas why this happens ? Thanks!

like image 579
medowlock Avatar asked Sep 09 '14 17:09

medowlock


People also ask

How set API base URL in Laravel?

You can use that code but replace the line $generator->forceSchema('https'); in the provider boot method, with $generator->forceRootUrl(Request::getScheme() . '://www.yourdomain.com'); . And now your URL should be generated like you want.

How can I change URL in Laravel 8?

To change the App URL of a Laravel application, you can specify it from the . env (environment) file. By having the APP_URL updated instead of using the "http://localhost" you will have a pretty URL when generating images, assets path and etc.

Why php artisan serve not working?

Reasons why php artisan serve not working You are running php artisan serve command in wrong directory. php artisan serve is laravel command only work with laravel project. Check project directory and run in root directory of your project. The requested host or port is not available.

What is artisan in Laravel?

Artisan is the command line interface included with Laravel. Artisan exists at the root of your application as the artisan script and provides a number of helpful commands that can assist you while you build your application.

How to make a request from the command line in Laravel?

When you make a request through your web browser, the request goes to ~/public/index.php and the necessary $_SERVER variables are populated with the correct information for Laravel to populate the base part of the URL. However, when you make the request using Artisan on the command line, the request goes to the ~/artisan script.

What is url in Laravel?

A Laravel-generated URL consists of a number of parts: Scheme: http://, https://, etc. Host: dev.domain.com, localhost, etc. Base: something/somethingElse (the subdirectory on the web server) Tail: myRoute, (the Laravel route parameters)

Where does Laravel load the console in Laravel?

Within the commands method of your app/Console/Kernel.php file, Laravel loads the routes/console.php file: * Register the closure based commands for the application.


2 Answers

A Laravel-generated URL consists of a number of parts:

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

These parts are then concatenated to form the URL.

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

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

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

From what I can find, it doesn't look like there is any appetite from the Laravel team to enable the application to function out-of-the-box in a subdirectory, e.g. Bugfix: domain routing for subfolder.

I ended up working around it in the way described by @medowlock for my scripts that would be called from the command line, e.g.:

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

This concatenates the application URL set in the app/config/app.php and the relative URL route.

like image 161
Marchie Avatar answered Sep 27 '22 21:09

Marchie


If you use Laravel 4.2, you can call the URL::forceRootUrl function to explicitly define what the root url of your laravel application is.

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

Calls to URL::to will use the URL define in your app config file after forcing the Root URL.

Unfortunately, this isn't available in Laravel 4.1 or Laravel 4.0

like image 43
Maxime Rainville Avatar answered Sep 27 '22 20:09

Maxime Rainville