Php Zend Framework 2:basePath()不打印任何内容

Php Zend Framework 2:basePath()不打印任何内容,php,zend-framework,zend-framework2,Php,Zend Framework,Zend Framework2,我想知道为什么basePath()在layout.phtml中不打印任何内容。我正在使用zend skeleton应用程序。虚拟主机会造成这个问题吗? 下面是代码片段和输出 echo $this->headLink(array('rel' => 'shortcut icon', 'type' => 'image/vnd.microsoft.icon', 'href' => $this->basePath() . '/img/favicon.ico'))

我想知道为什么basePath()在layout.phtml中不打印任何内容。我正在使用zend skeleton应用程序。虚拟主机会造成这个问题吗? 下面是代码片段和输出

echo $this->headLink(array('rel' => 'shortcut icon', 'type' => 'image/vnd.microsoft.icon', 'href' => $this->basePath() . '/img/favicon.ico'))
                        ->prependStylesheet($this->basePath() . '/css/style.css')
                        ->prependStylesheet($this->basePath() . '/css/bootstrap-theme.min.css')
                        ->prependStylesheet($this->basePath() . '/css/bootstrap.min.css');
HTML输出

        <link href="/css/bootstrap.min.css" media="screen" rel="stylesheet" type="text/css">
<link href="/css/bootstrap-theme.min.css" media="screen" rel="stylesheet" type="text/css">
<link href="/css/style.css" media="screen" rel="stylesheet" type="text/css">
<link href="/img/favicon.ico" rel="shortcut icon" type="image/vnd.microsoft.icon">

我如何解决这个问题。非常感谢您的帮助。提前谢谢。

像这样试试

    $print = $this->headLink(array('rel' => 'shortcut icon', 'type' => 'image/vnd.microsoft.icon', 'href' => $this->basePath() . '/img/favicon.ico'))
                                    ->prependStylesheet($this->basePath() . '/css/style.css')
                                    ->prependStylesheet($this->basePath() . '/css/bootstrap-theme.min.css')
                                    ->prependStylesheet($this->basePath() . '/css/bootstrap.min.css');


>    echo $print;
或者作为临时解决办法

<link href="<?php echo $this->basePath(); ?>/css/style.css" rel="stylesheet" type="text/css"/>
您应该尝试使用视图帮助器,而不是basePath()

例如:

$this->prependStylesheet($this->serverUrl('/css/style.css'))
     ->prependStylesheet($this->serverUrl('/css/bootstrap-theme.min.css'))
     ->prependStylesheet($this->serverUrl('/css/bootstrap.min.css'));

尝试将此添加到控制器:

public function baseUrl() {
    $protocol = isset($_SERVER['HTTPS']) ? 'https' : 'http';
    $server = $_SERVER['HTTP_HOST'];
    $port = $_SERVER['SERVER_PORT'] != 80 ? ":{$_SERVER['SERVER_PORT']}" : '';
    $path = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/\\') . '/';

    return "$protocol://$server$port$path";
}

如果仍然不起作用,请将其作为变量传递给视图,以便在视图中使用。

它应该输出什么?在我的文件夹结构中,输出应该如下所示:不应该。您应该将虚拟主机设置为
/zend/skeleton/public
,因为这是公用文件夹(可以从外部看到),然后链接
/css/bootstrap
就可以了。这里有人解决了您的问题吗?如果没有,那么我已经为你更新了一个临时解决方案,呃,我不是提问者。我想说的是,你的建议和提问者的代码没有实际的区别。在这两个示例中都将调用toString方法。您并不是仅仅通过拆分他的代码来解决问题。我只是建议使用Zend 2文档化方法