What is the significance of Application URL in laravel 5

25,520

Solution 1

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.

Solution 2

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

Share:
25,520

Related videos on Youtube

echoashu
Author by

echoashu

Updated on June 30, 2020

Comments

  • echoashu
    echoashu almost 4 years

    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

    • Bankzilla
      Bankzilla almost 9 years
      If the current url is set to localhost then use the domain name
  • echoashu
    echoashu almost 9 years
    That's the best answer and descriptive enough to understand. Thank You :-)
  • echoashu
    echoashu almost 9 years
    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 :-)
  • patricus
    patricus almost 9 years
    @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/').
  • echoashu
    echoashu almost 9 years
    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
  • Almazik G
    Almazik G almost 9 years
    @echoashu yes, correct. Event though it's preferred way to go around. This lets you keep your sensitive data apart from version control.
  • Dario Corno
    Dario Corno about 8 years
    I was looking for informations on that Laravel configuration parameter, this answer was perfect. Thanks.
  • Staysee
    Staysee about 8 years
    It all makes sense now. Thank you!
  • mercury
    mercury almost 7 years
    what if i don't have a domain?and have only server ip ??
  • blamb
    blamb almost 7 years
    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?