Laravel: Get base URL
PhpLaravelBase UrlPhp Problem Overview
Simple question, but the answer seems quite hard to come by. In Codeigniter, I could load the URL helper and then simply do
echo base_url();
to get my site's URL. Is there an equivalent in Laravel?
Php Solutions
Solution 1 - Php
You can use the URL facade which lets you do calls to the URL generator
So you can do:
URL::to('/');
You can also use the application container:
$app->make('url')->to('/');
$app['url']->to('/');
App::make('url')->to('/');
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('/');
}
}
Solution 2 - Php
Laravel < 5.2
echo url();
Laravel >= 5.2
echo url('/');
Solution 3 - Php
For Laravel 5 I normally use:
<a href="{{ url('/path/uri') }}">Link Text</a>
I'm of the understanding that using the url()
function is calling the same Facade
as URL::to()
Solution 4 - Php
Updates from 2018 Laravel release(5.7) documentation with some more url() functions and it's usage.
> Question: To get the site's URL in Laravel? > > This is kind of a general question, so we can split it.
1. Accessing The Base URL
// Get the base URL.
echo url('');
// Get the app URL from configuration which we set in .env file.
echo config('app.url');
2. Accessing The Current URL
// Get the current URL without the query string.
echo url()->current();
// Get the current URL including the query string.
echo url()->full();
// Get the full URL for the previous request.
echo url()->previous();
3. URLs For Named Routes
// http://example.com/home
echo route('home');
4. URLs To Assets(Public)
// Get the URL to the assets, mostly the base url itself.
echo asset('');
5. File URLs
use Illuminate\Support\Facades\Storage;
$url = Storage::url('file.jpg'); // stored in /storage/app/public
echo url($url);
Each of these methods may also be accessed via the URL facade:
use Illuminate\Support\Facades\URL;
echo URL::to(''); // Base URL
echo URL::current(); // Current URL
How to call these Helper functions from blade Template(Views) with usage.
// http://example.com/login
{{ url('/login') }}
// http://example.com/css/app.css
{{ asset('css/app.css') }}
// http://example.com/login
{{ route('login') }}
// usage
<!-- Styles -->
<link href="{{ asset('css/app.css') }}" rel="stylesheet">
<!-- Login link -->
<a class="nav-link" href="{{ route('login') }}">Login</a>
<!-- Login Post URL -->
<form method="POST" action="{{ url('/login') }}">
Solution 5 - Php
To get it to work with non-pretty URLs I had to do:
asset('/');
Solution 6 - Php
This:
echo url('/');
And this:
echo asset('/');
both displayed the home url in my case :)
Solution 7 - Php
Laravel provides bunch of helper functions and for your requirement you can simply
use url() function of Laravel Helpers
but in case of Laravel 5.2 you will have to use url('/')
here is the list of all other helper functions of Laravel
Solution 8 - Php
To just get the app url, that you configured you can use :
Config::get('app.url')
Solution 9 - Php
Another possibility: {{ URL::route('index') }}
Solution 10 - Php
Check this -
<a href="{{url('/abc/xyz')}}">Go</a>
This is working for me and I hope it will work for you.
Solution 11 - Php
There are multiple ways:
-
request()->getSchemeAndHttpHost()
-
url('/')
-
asset('')
-
$_SERVER['SERVER_NAME']
Solution 12 - Php
You can also use URL::to('/') to display image in Laravel. Please see below:
<img src="{{URL::to('/')}}/images/{{ $post->image }}" height="100" weight="100">
Assume that, your image is stored under "public/images".
Solution 13 - Php
I used this and it worked for me in Laravel 5.3.18:
<?php echo URL::to('resources/assets/css/yourcssfile.css') ?>
IMPORTANT NOTE: This will only work when you have already removed "public" from your URL. To do this, you may check out this helpful tutorial.
Solution 14 - Php
By the way, if your route has a name like:
Route::match(['get', 'post'], 'specialWay/edit', 'SpecialwayController@edit')->name('admin.spway.edit');
You can use the route()
function like this:
<form method="post" action="{{route('admin.spway.edit')}}" class="form form-horizontal" id="form-spway-edit">
Other useful functions:
$uri = $request->path();
$url = $request->url();
$url = $request->fullUrl();
asset()
app_path();
// ...
https://github.com/laravel/framework/blob/5.4/src/Illuminate/Foundation/helpers.php
Solution 15 - Php
You can use facades or helper function as per following.
echo URL::to('/');
echo url();
> Laravel using Symfony Component for Request, Laravel internal logic as per > following.
namespace Symfony\Component\HttpFoundation;
/**
* {@inheritdoc}
*/
protected function prepareBaseUrl()
{
$baseUrl = $this->server->get('SCRIPT_NAME');
if (false === strpos($this->server->get('REQUEST_URI'), $baseUrl)) {
// assume mod_rewrite
return rtrim(dirname($baseUrl), '/\\');
}
return $baseUrl;
}
Solution 16 - Php
I found an other way to just get the base url to to display the value of environment variable APP_URL
env('APP_URL')
which will display the base url like http://domains_your//yours_website. beware it assumes that you had set the environment variable in .env file (that is present in the root folder).
Solution 17 - Php
you can get it from Request, at laravel 5
request()->getSchemeAndHttpHost();
Solution 18 - Php
I also used the base_path()
function and it worked for me.