我想像视图一样捕获基本路径.在视图基本路径中通过使用辅助函数轻松获取,比如$this->basePath();我想从模型中获取basepath值.
将getter/setter添加到您的模型中:
TestModel.php
<?php
class TestModel
{
protected $_basePath;
/**
* @param string
*/
public function setBasePath($path)
{
$this->_basePath = $path;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,在实例化模型时注入此项
服务管理器配置:
'factories' => array(
'Application\Model\TestModel' => function($sm){
$model= new \Application\Model\TestModel();
// Just grab what we want from the view helper
$helper = $sm->get('viewhelpermanager')->get('basePath');
$path = $helper(); // or $helper('filenamehere') for added file path
// Alternatively you can just use the request to get the path
//$path = $sm->get('Request')->getBasePath();
$model->setBasePath($path);
return $model;
},
Run Code Online (Sandbox Code Playgroud)
如果您的模型中有服务管理器/服务定位器,您可以使用上述方法之一直接获取模型中的值.
$path = $serviceManager->get('Request')->getBasePath();
Run Code Online (Sandbox Code Playgroud)
如果你看看如何实例化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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3969 次 |
| 最近记录: |