23

in Config/app.php in laravel source, what is the actual use of url ?

It says Application URL to be used by artisan command line tool , so what should it be actually?

I mean should it be http://mydomainname.com or should it be /var/www/laravel/ or /var/www/laravel/public

Current Configuration

/*
|--------------------------------------------------------------------------
| Application URL
|--------------------------------------------------------------------------
|
| This URL is used by the console to properly generate URLs when using
| the Artisan command line tool. You should set this to the root of
| your application so that it is used when running Artisan tasks.
|
*/

'url' => 'http://localhost/',

Provided my application source is located at /var/www/ directory and laravel public folder is /var/www/laravel/public And the http://mydomainname.com is pointed to resolve at /var/www/laravel/public directory

Use Case:

I'll be using laravel schedular from /app/Console/Kernel.php which will be dispatching periodic sendMail commands and that in turn will queue up the mails to be sent in database and queue listner than will process the queue as normal

Queues are working fine at localhost (my local xamp server) however I am concerned as what should be the url value in production

echoashu
  • 912
  • 3
  • 14
  • 31

2 Answers2

38

When a user visits your website, Laravel gets a lot of information it needs about the request from PHP's superglobals ($_SERVER, $_GET, $_POST, etc.). Part of this information is the request URL.

For example, if you access the request methods url() or path(), this information was retrieved via the $_SERVER superglobal:

$url = Request::url();
$path = Request::path();

However, artisan, commands, jobs, etc. don't have the benefit of this information. It is not a normal HTTP request coming in from the user, it is a PHP command being run from the command line. Because of this, Laravel needs a way to determine what the url of the application should be. This is where the configuration value comes in.

In your example, you plan on sending out emails from a queue. Imagine you need to include a link to a route of your website in one of the emails, so you use the UrlGenerator to get the url for the link (URL::route('route.name')). Since this code is being run inside a command, and isn't related to any type of HTTP request, the base application url will be picked up from the configuration value you set in config/app.php.

As should hopefully be a little more clear now, the url value should be set to the http url for your application, not any type of directory path. In your example, it should be http://mydomainname.com.

patricus
  • 59,488
  • 15
  • 143
  • 145
  • 2
    That's the best answer and descriptive enough to understand. Thank You :-) – echoashu Jun 30 '15 at 07:34
  • 1
    I was looking for informations on that Laravel configuration parameter, this answer was perfect. Thanks. – Dario Corno Mar 02 '16 at 03:58
  • 1
    It all makes sense now. Thank you! – Staysee Mar 05 '16 at 18:56
  • what if i don't have a domain?and have only server ip ?? – mercury May 14 '17 at 00:21
  • I was going to comment on IP. I use a LAN server, so i use the ip instead e.g. `http://192.168.1.20` I haven't generated anything that is needed for this yet, which is why I'm reading this post, i was hoping to find more info as to what exactly its for, and this topic isn't clear enough IMO. I knew it was for `artisan`, but what are some of the uses for the app? besides email? – blamb May 14 '17 at 21:25
7

when on production, it should be set to

'url' => 'http://your-live-domain.com',

As you mentioned, it's going to be used by the artisan commands and queues.

You can leverage .env to store your live domain. http://laravel.com/docs/5.1#environment-configuration

Almazik G
  • 1,066
  • 1
  • 9
  • 21
  • as for the .env idea , does the application then automatically get production url since i cant find ENV variable being used in config/app.php ? Anyway that url worked :-) – echoashu Jun 30 '15 at 07:37
  • 3
    @echoashu For the `.env` idea, you would set the value in your configuration like so: `'url' => env('URL', 'http://localhost/'),`. Now, if you supply a `URL` value in your `.env` file, the `url` config value will be set to that. If you don't supply a `URL` value in your `.env` file, the `'url'` config value will be set to the default value passed to the `env()` function (in this case `'http://localhost/'`). – patricus Jun 30 '15 at 07:50
  • perfect!! I seen similar logic in database.php and other config files too and thought the same. That said, we can store as many important static constants in .env file and access them with `env()` function ? Correct me if wrong – echoashu Jun 30 '15 at 08:53
  • 2
    @echoashu yes, correct. Event though it's preferred way to go around. This lets you keep your sensitive data apart from version control. – Almazik G Jun 30 '15 at 10:07