Zend framework2 如何从zend framework 2中的模型捕获basePath?

Zend framework2 如何从zend framework 2中的模型捕获basePath?,zend-framework2,Zend Framework2,我想捕捉基本路径,如视图。在视图中,通过使用辅助函数(如$this->basePath())轻松获取基本路径我想从模型中获取基本路径值。我不知道如何在模型中执行,但在控制器中可以执行以下操作: <?php $myUrl = $this->url()->fromRoute('home'); 将getter/setter添加到您的模型中: TestModel.php <?php class TestModel { protected $_basePat

我想捕捉基本路径,如视图。在视图中,通过使用辅助函数(如
$this->basePath())轻松获取基本路径我想从模型中获取基本路径值。

我不知道如何在模型中执行,但在控制器中可以执行以下操作:

<?php
   $myUrl =  $this->url()->fromRoute('home');

将getter/setter添加到您的模型中:

TestModel.php

<?php
class TestModel   
{
    protected $_basePath;

    /**
     * @param string
     */
    public function setBasePath($path)
    {
        $this->_basePath = $path;
    }
}
如果模型中有可用的服务管理器/服务定位器,则可以使用上述方法之一直接获取模型中的值

 $path = $serviceManager->get('Request')->getBasePath();
如果查看ViewHelper是如何实例化的,您会看到它首先检查配置:

$config = $serviceLocator->get('Config');
if (isset($config['view_manager']) && isset($config['view_manager']['base_path'])) {
     $basePath = $config['view_manager']['base_path'];
} 
else {
    $basePath = $serviceLocator->get('Request')->getBasePath();
}

这在控制器$url=$This->getRequest()->getUri()中可能非常有用;然后捕获每个值,如$schema=$url->getScheme(),->getHost()等,但我想直接从模型类获取basePath。
$config = $serviceLocator->get('Config');
if (isset($config['view_manager']) && isset($config['view_manager']['base_path'])) {
     $basePath = $config['view_manager']['base_path'];
} 
else {
    $basePath = $serviceLocator->get('Request')->getBasePath();
}