如何在 ZF2 控制器中获取 baseUrl? [英] How to get baseUrl in ZF2 controller?

查看:20
本文介绍了如何在 ZF2 控制器中获取 baseUrl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 zf2 控制器中,我想检索应用程序基本 URL(例如 http://domain.com).

In my zf2 controller I want to retrieve the application base URL (for example http://domain.com).

我尝试了以下调用,但它返回一个空字符串.

I tried the following call but it returns an empty string.

$this->request->getBasePath();

然后我怎样才能在我的控制器中获取 URL 的 http://domain.com 部分?

How can I then get the http://domain.com part of URL in my controller?

推荐答案

我知道这不是最好的方法,但是,嘿,它有效:

I know this is not the prettiest way of doing it but, hey, it works:

public function indexAction()
{
    $uri = $this->getRequest()->getUri();
    $scheme = $uri->getScheme();
    $host = $uri->getHost();
    $base = sprintf('%s://%s', $scheme, $host);

    // $base would be http://domain.com
}

或者,如果您不介意将所有内容缩短为两行:

Or if you don't mind shortening everything you could do it in two lines:

public function indexAction()
{
    $uri = $this->getRequest()->getUri();
    $base = sprintf('%s://%s', $uri->getScheme(), $uri->getHost());
}

这篇关于如何在 ZF2 控制器中获取 baseUrl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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