Laravel: Get base url

后端 未结 17 2015
梦谈多话
梦谈多话 2020-12-07 10:09

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();
<
17条回答
  •  忘掉有多难
    2020-12-07 10:24

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

提交回复
热议问题