首页 文章

Laravel:获取基本网址

提问于
浏览
141

简单的问题,但答案似乎很难得到 . 在Codeigniter中,我可以加载url helper,然后就可以了

echo base_url();

获取我网站的网址 . Laravel有同等的东西吗?

14 回答

  • 40

    Laravel提供了大量的帮助功能,您可以简单地满足您的要求

    使用Laravel Helpers的 url() 功能

    但是在 Laravel 5.2 的情况下你将不得不使用 url('/')

    here是Laravel的所有其他帮助程序函数的列表

  • 12

    这个:

    echo url('/');
    

    还有这个:

    echo asset('/');
    

    在我的情况下都显示了主页:)

  • 223

    您可以使用URL facade来调用URL generator

    所以你可以这样做:

    URL::to('/');
    

    您还可以使用应用程序容器:

    $app->make('url')->to('/');
    $app['url']->to('/');
    App::make('url')->to('/');
    

    或者注入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('/');
        }
    }
    
  • 1

    Laravel <5.2

    echo url();
    

    Laravel> = 5.2

    echo url('/');
    

    希望这对你有所帮助

  • 2

    对于Laravel 5,我通常使用:

    <a href="{{ url('/path/uri') }}">Link Text</a>
    

    我的理解是使用 url() 函数调用 Facade 作为 URL::to()

  • 1

    要使它与非漂亮的URL一起使用,我必须这样做:

    asset('/');
    
  • 1

    另一种可能性: {{ URL::route('index') }}

  • 1

    更新了2018年Laravel版本(5.7)文档以及更多url()函数及其用法 .

    问题:要在Laravel中获取网站的URL?这是一个普遍的问题,所以我们可以分开它 .

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

    要获取您配置的应用程序网址,您可以使用Config :: get('app.url')

  • 0

    我用过这个,它在Laravel 5.3.18中为我工作:

    <?php echo URL::to('resources/assets/css/yourcssfile.css') ?>
    

    重要说明:仅当您已从网址中删除"public"时,此功能才有效 . 为此,您可以查看this helpful tutorial .

  • 107

    您还可以使用URL :: to('/')在Laravel中显示图像 . 请看下面:

    <img src="{{URL::to('/')}}/images/{{ $post->image }}" height="100" weight="100">
    

    假设您的图像存储在“公共/图像”下 .

  • 1

    顺便说一下,如果您的路线有如下名称:

    Route::match(['get', 'post'], 'specialWay/edit', 'SpecialwayController@edit')->name('admin.spway.edit');
    

    您可以像这样使用 route() 函数:

    <form method="post" action="{{route('admin.spway.edit')}}" class="form form-horizontal" id="form-spway-edit">
    

    其他有用的功能:

    $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

  • 10

    您可以按照以下方式使用外墙或辅助功能 .

    echo URL::to('/');
    echo url();
    

    Laravel使用Symfony Component for Request,Laravel内部逻辑如下 .

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

    我找到了另一种方法来获取基本URL以显示环境变量APP_URL的值

    env('APP_URL')
    

    这将显示基本网址,如http://domains_your//yours_website . 请注意,它假定您已在.env文件中设置环境变量(存在于根文件夹中) .

相关问题