Inigo
Inigo

Reputation: 8685

Laravel: Get base URL

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?

Upvotes: 273

Views: 750354

Answers (18)

X 47 48 - IR
X 47 48 - IR

Reputation: 1478

There are multiple ways:

  • request()->getSchemeAndHttpHost()
  • url('/')
  • asset('')
  • config('app.url')
  • $_SERVER['SERVER_NAME']

Upvotes: 10

Laravel < 5.2

echo url();

Laravel >= 5.2

echo url('/');

Upvotes: 228

Minh Anh Vương
Minh Anh Vương

Reputation: 11

I also used the base_path() function and it worked for me.

Upvotes: 0

Kenyon
Kenyon

Reputation: 817

To just get the app url, that you configured you can use :

Config::get('app.url')

Upvotes: 7

Soleil
Soleil

Reputation: 401

you can get it from Request, at laravel 5

request()->getSchemeAndHttpHost();

Upvotes: 1

msayubi76
msayubi76

Reputation: 1658

Check this -

<a href="{{url('/abc/xyz')}}">Go</a>

This is working for me and I hope it will work for you.

Upvotes: 7

Adeel Raza Azeemi
Adeel Raza Azeemi

Reputation: 793

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).

Upvotes: 3

Sobin Augustine
Sobin Augustine

Reputation: 3765

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') }}">

Upvotes: 58

Paresh Barad
Paresh Barad

Reputation: 1609

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;
}

Upvotes: 3

GGadash
GGadash

Reputation: 171

This:

echo url('/');

And this:

echo asset('/');

both displayed the home url in my case :)

Upvotes: 17

闫鹏程
闫鹏程

Reputation: 31

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

Upvotes: 3

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".

Upvotes: 4

ITWitch
ITWitch

Reputation: 1729

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.

Upvotes: 3

CptChaos
CptChaos

Reputation: 59

Another possibility: {{ URL::route('index') }}

Upvotes: 4

Akshay Khale
Akshay Khale

Reputation: 8361

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

Upvotes: 14

DrewT
DrewT

Reputation: 5072

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()

Upvotes: 62

dan-klasson
dan-klasson

Reputation: 14180

To get it to work with non-pretty URLs I had to do:

asset('/');

Upvotes: 21

hannesvdvreken
hannesvdvreken

Reputation: 4968

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('/');
    }
}

Upvotes: 366

Related Questions