Laravel:获取基本网址 [英] Laravel: Get base url

查看:87
本文介绍了Laravel:获取基本网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的问题,但答案似乎很难.在Codeigniter中,我可以加载url帮助器,然后简单地完成

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

获取我网站的URL.在Laravel中有等同的东西吗?

to get my site's URL. Is there an equivalent in Laravel?

推荐答案

您可以使用URL门面,它使您可以调用URL生成器

You can use the URL facade which lets you do calls to the URL generator

因此您可以这样做:

URL::to('/');

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

You can also use the application container:

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

或注入UrlGenerator:

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

这篇关于Laravel:获取基本网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆