欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Laravel: Get base url

imple 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?

shareimprove this question
 

11 Answers 正确答案

up vote155down voteaccepted

You can get the base url of the Laravel application by:

echo url('/');

or

{{ url('/') }}

Read more about Laravel helpers here


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

 

 
3 
And despite its possible appearance from the example, this is relative to the Laravel's root path, so if you're installed in /something/ it'll make the right URL. – ceejayoz Apr 14 '14 at 12:42
1 
Also works at view, thanks – deFreitas Dec 3 '16 at 20:41

Laravel < 5.2

echo url();

Laravel >= 5.2

echo url('/');

Hope this helps you

shareimprove this answer
 
4 
Could you expand this answer to include an explanation instead of just a code-snippet? – Hosch250 Nov 12 '14 at 17:07
1 
Exam: I have site in local: localhost/abc In Codeigniter: echo base_url(); => I get localhost/abc In Laravel: echo url(); => I get localhost/abc too. – Nguyễn Thành Bồi Nov 13 '14 at 3:24
1 
Use this for an url with segmentation: url().'/'.\Request::segment(1).'/'.\Request::segment(2) – 0x1ad2 Nov 27 '14 at 10:04
18 
Note that this no longer works in 5.2: github.com/laravel/framework/issues/11479 You can use url('/')instead however – 4lun Dec 23 '15 at 18:31

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

shareimprove this answer
 
2 
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 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 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: laravel.com/docs/5.4/helpers#method-secure-url – DrewT Feb 20 at 18:27

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

asset('/');
shareimprove this answer
 
3 
To my need this is the best <base href="{{ asset('/') }}" /> – rneves Oct 27 '15 at 13:51

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

shareimprove this answer
 
   
@sambellerose and is you want to access inner folders/files you can do url('/css/style.css') – Akshay Khale May 6 '16 at 5:07

echo url('/');

echo asset('/');

both displayed the home url in my case :)

shareimprove this answer
 

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

shareimprove this answer
 
   
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 9 '16 at 8:20

To just get the app url, that you configured you can use Config::get('app.url')

shareimprove this answer
 
   
This definition is only used in Laravel cli. app.url it appears to be a fallback – Wallace de Souza Aug 24 '16 at 13:33
   
Well anymore, env('APP_URL') would probably be the best option. – Kenyon Aug 24 '16 at 21:32

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.

shareimprove this answer
 

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

shareimprove this answer
 

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

shareimprove this answer
 

来自 https://stackoverflow.com/questions/23059918/laravel-get-base-url


普通分类: