【问题标题】:Laravel: Get base URLLaravel:获取基本 URL
【发布时间】:2014-05-28 09:39:50
【问题描述】:

简单的问题,但似乎很难得到答案。在 Codeigniter 中,我可以加载 URL 帮助程序,然后简单地做

echo base_url();

获取我网站的 URL。 Laravel 中是否有等价物?

【问题讨论】:

    标签: php laravel base-url


    【解决方案1】:

    您可以使用 URL 外观来调用 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('/');
        }
    }
    

    【讨论】:

    • 尽管它可能在示例中出现,但它是相对于 Laravel 的根路径的,所以如果你安装在 /something/ 中,它会生成正确的 URL。
    • @deFreitas 和 #ceejayoz 如何在 Laravel 本地化中使用 URL::to?
    • @MubasharIqbal 如果我理解你的问题,{{URL::to('/my-page.html')}} 在视图中,echo URL::to('/my-page.html'); 在代码中
    • 不错的处理程序,就我而言:&lt;a class="button is-primary" href="&lt;?= URL::to('/'); ?&gt;/atencion/reporte" target="_blank"&gt;,谢谢!
    【解决方案2】:

    Laravel

    echo url();
    

    Laravel >= 5.2

    echo url('/');
    

    【讨论】:

    • 您能否扩展此答案以包含解释而不仅仅是代码-sn-p?
    • 考试:我在本地有站点:localhost/abc 在 Codeigniter 中:echo base_url(); => 我在 Laravel 中得到 localhost/abc: echo url(); => 我也得到localhost/abc。
    • 将此用于带有分段的 url:url().'/'.\Request::segment(1).'/'.\Request::segment(2)
    • 请注意,这在 5.2 中不再有效:github.com/laravel/framework/issues/11479 不过您可以改用 url('/')
    • asset('/') 对我来说似乎更好,因为它有斜杠,这在基本 href 中是必需的。
    【解决方案3】:

    对于 Laravel 5,我通常使用:

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

    我了解到使用url() 函数调用的Facade 与URL::to() 相同

    【讨论】:

    • 注意:如果您的网站是通过 https 服务的,您可以以同样的方式使用 secure_url() 函数,这将产生一个 https 链接。在 https 站点上使用 url() 仍会生成 http 链接。
    • 这个对我有用。了解secure_url() 语法也很有用。谢谢。
    • 我使用的是 Lumen 5.4。 url() 会根据请求的协议生成 http 或 https 链接。另一方面,secure_url() 不存在。 Laravel 5.4 也有这种变化吗?
    • 不,它是 Larvel 5.4 的一部分。我无法真正发表评论,因为我从未使用过 Lumen,但 secure_url() 的 5.4 Laravel 文档可在此处获得:laravel.com/docs/5.4/helpers#method-secure-url
    【解决方案4】:

    2018 Laravel release(5.7) 文档的更新,增加了一些 url() 函数及其用法。

    问题:在 Laravel 中获取网站的 URL?

    这是一个一般性的问题,所以我们可以拆分它。

    1.访问基本 URL

    // Get the base URL.
    echo url('');
    
    // Get the app URL from configuration which we set in .env file.
    echo config('app.url'); 
    

    2。访问当前 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.命名路由的 URL

    // http://example.com/home
    echo route('home');
    

    4.资产的 URL(公共)

    // Get the URL to the assets, mostly the base url itself.
    echo asset('');
    

    5.文件网址

    use Illuminate\Support\Facades\Storage;
    
    $url = Storage::url('file.jpg'); // stored in /storage/app/public
    echo url($url);
    

    也可以通过 URL 外观访问这些方法中的每一个:

    use Illuminate\Support\Facades\URL;
    
    echo URL::to(''); // Base URL
    echo URL::current(); // Current URL
    

    如何使用刀片模板(视图)调用这些 Helper 函数。

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

    【讨论】:

      【解决方案5】:

      要让它与不漂亮的 URL 一起工作,我必须这样做:

      asset('/');
      

      【讨论】:

      • 对我来说这是最好的&lt;base href="{{ asset('/') }}" /&gt;
      • 向你致敬!它正好适合&lt;base href="..."/&gt;,因为斜杠
      【解决方案6】:

      这个:

      echo url('/');
      

      还有这个:

      echo asset('/');
      

      在我的例子中都显示了主页 url :)

      【讨论】:

        【解决方案7】:

        Laravel 提供了一堆辅助函数,你可以简单地满足你的要求

        使用 Laravel Helpers 的 url() 函数

        但在 Laravel 5.2 的情况下,您必须使用 url('/')

        here 是 Laravel 的所有其他辅助函数的列表

        【讨论】:

        • @sambellerose 你想访问内部文件夹/文件吗?url('/css/style.css')
        【解决方案8】:

        要获取您配置的应用程序网址,您可以使用:

        Config::get('app.url')
        

        【讨论】:

        • 这个定义只在 Laravel cli 中使用。 app.url 它似乎是一个后备
        • 好吧,env('APP_URL') 可能是最好的选择。
        【解决方案9】:

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

        【讨论】:

        • 不知道为什么这被否决了,因为它确实有效,而且没有人提供选项?
        • 不是我的反对意见,但我猜原因是您不能保证根路由的命名在每个应用程序中都是“索引”。
        【解决方案10】:

        检查这个 -

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

        这对我有用,我希望它对你有用。

        【讨论】:

          【解决方案11】:

          有多种方式:

          • request()-&gt;getSchemeAndHttpHost()

          • url('/')

          • asset('')

          • $_SERVER['SERVER_NAME']

          【讨论】:

            【解决方案12】:

            你也可以使用 URL::to('/') 在 Laravel 中显示图片。请看下面:

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

            假设您的图片存储在“public/images”下。

            【讨论】:

              【解决方案13】:

              我使用了这个,它在 Laravel 5.3.18 中对我有用:

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

              重要提示:这仅在您已从 URL 中删除“公共”时才有效。为此,您可以查看this helpful tutorial。

              【讨论】:

                【解决方案14】:

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

                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

                【讨论】:

                  【解决方案15】:

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

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

                  Laravel 使用 Symfony 组件进行请求,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;
                  }
                  

                  【讨论】:

                    【解决方案16】:

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

                    env('APP_URL')
                    

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

                    【讨论】:

                      【解决方案17】:

                      你可以从请求中得到它,在 laravel 5

                      request()->getSchemeAndHttpHost();
                      

                      【讨论】:

                        【解决方案18】:

                        我还使用了base_path() 函数,它对我有用。

                        【讨论】:

                          猜你喜欢
                          • 1970-01-01
                          • 2017-04-27
                          • 1970-01-01
                          • 2015-06-11
                          • 2016-05-20
                          • 1970-01-01
                          • 2019-04-06
                          • 2012-12-24
                          • 1970-01-01
                          相关资源
                          最近更新 更多