Laravel: Get base URL

You can use the URL facade which lets you do calls to the URL generator

So you can do:

URL::to("https://stackoverflow.com/");

You can also use the application container:

$app->make('url')->to("https://stackoverflow.com/");
$app['url']->to("https://stackoverflow.com/");
App::make('url')->to("https://stackoverflow.com/");

Or inject the UrlGenerator:

<?php
namespace Vendor\Your\Class\Namespace;

use Illuminate\Routing\UrlGenerator;

class Classname
{
    protected $url;

    public function __construct(UrlGenerator $url)
    {
        $this->url = $url;
    }

    public function methodName()
    {
        $this->url->to("https://stackoverflow.com/");
    }
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)