213

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?

Syscall
  • 16,959
  • 9
  • 22
  • 41
Inigo
  • 6,604
  • 15
  • 52
  • 94

18 Answers18

318

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('/');
    }
}
hannesvdvreken
  • 4,458
  • 1
  • 13
  • 17
177

Laravel < 5.2

echo url();

Laravel >= 5.2

echo url('/');

Hope this helps you

Jeff Puckett
  • 28,726
  • 15
  • 96
  • 149
Nguyễn Thành Bồi
  • 2,057
  • 2
  • 12
  • 8
  • 4
    Could you expand this answer to include an explanation instead of just a code-snippet? –  Nov 12 '14 at 17:07
  • 2
    Exam: I have site in local: http://localhost/abc In Codeigniter: echo base_url(); => I get http://localhost/abc In Laravel: echo url(); => I get http://localhost/abc too. – Nguyễn Thành Bồi Nov 13 '14 at 03:24
  • 1
    Use this for an url with segmentation: url().'/'.\Request::segment(1).'/'.\Request::segment(2) – 0x1ad2 Nov 27 '14 at 10:04
  • 20
    Note that this no longer works in 5.2: https://github.com/laravel/framework/issues/11479 You can use `url('/')` instead however – 4lun Dec 23 '15 at 18:31
  • 2
    `asset('/')` seems better to me because it has trailing slash which is necessary in base href. – vintproykt Apr 09 '19 at 10:46
  • I tried url(/some/route) in routes/web.php and it is still not working on deployed server – Sebastian Nov 17 '20 at 21:01
55

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

DrewT
  • 4,671
  • 2
  • 34
  • 53
  • 4
    Note: if your website is served over https you can use the `secure_url()` function the same way, and this will produce an https link. Using `url()` on an https site will still produce an http link. – DrewT Dec 14 '16 at 21:15
  • 1
    This one worked for me. Also useful to know the secure_url() syntax as well. Thanks. – jimmyplaysdrums Jan 18 '17 at 15:15
  • 1
    I'm using Lumen 5.4. There `url()` generates a http or https link based on the protocol of the request. On the other hand `secure_url()` doesn't exist. Did this change in Laravel 5.4 too? – Andreas Linnert Feb 18 '17 at 18:35
  • No, it's part of Larvel 5.4. I can't really comment because I have never used Lumen but the 5.4 Laravel documentation for `secure_url()` is available here: https://laravel.com/docs/5.4/helpers#method-secure-url – DrewT Feb 20 '17 at 18:27
33

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') }}">
Community
  • 1
  • 1
Sobin Augustine
  • 3,145
  • 2
  • 22
  • 41
19

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

asset('/');
dan-klasson
  • 12,215
  • 11
  • 49
  • 91
16

This:

echo url('/');

And this:

echo asset('/');

both displayed the home url in my case :)

joshuamabina
  • 1,210
  • 17
  • 25
GGadash
  • 161
  • 1
  • 3
13

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

Akshay Khale
  • 7,087
  • 7
  • 43
  • 51
4

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

Config::get('app.url')
Mostafa Norzade
  • 1,059
  • 2
  • 17
  • 35
Kenyon
  • 667
  • 1
  • 9
  • 23
3

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

CptChaos
  • 49
  • 4
  • Not sure why this got downvoted, as it actually works and no one gave the option as well? – CptChaos Jul 30 '16 at 17:52
  • 2
    Not my downvote but I'm guessing the reason is that you can't guarantee the naming of your root route to be "index" in every app. – Jonathan Aug 09 '16 at 08:20
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".

2

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.

ITWitch
  • 1,449
  • 3
  • 19
  • 35
2

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

Ethan
  • 4,165
  • 4
  • 22
  • 41
闫鹏程
  • 21
  • 2
2

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;
}
Paresh Barad
  • 1,239
  • 9
  • 18
2

Check this -

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

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

vikash singh
  • 1,329
  • 15
  • 25
msayubi76
  • 396
  • 2
  • 10
1

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

0

you can get it from Request, at laravel 5

request()->getSchemeAndHttpHost();
Soleil
  • 309
  • 3
  • 9
0

There are multiple ways:

  • request()->getSchemeAndHttpHost()

  • url('/')

  • asset('')

  • $_SERVER['SERVER_NAME']

X 47 48 - IR
  • 574
  • 1
  • 6
  • 18
-1

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

Mostafa Norzade
  • 1,059
  • 2
  • 17
  • 35