我想捕捉像視圖一樣的基本路徑。在基地路徑很容易通過使用幫助功能,如$this->basePath();
我想從模型獲得基地路徑值。如何在zend框架2中從模型中捕獲basePath?
3
A
回答
5
添加的getter/setter到您的型號:
TestModel.php
<?php
class TestModel
{
protected $_basePath;
/**
* @param string
*/
public function setBasePath($path)
{
$this->_basePath = $path;
}
}
現在在實例化您的模型時注入此代碼
Service Manager配置:
'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;
},
如果有可用的服務管理/服務定位器模型裏面你可以只直接獲得使用上述方法中的一個模型裏面的值。
$path = $serviceManager->get('Request')->getBasePath();
如果你看一下視圖助手是如何實例你看到它首先檢查配置:
$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();
}
0
我不知道如何做的模型,但在控制器,你可以這樣做:
<?php
$myUrl = $this->url()->fromRoute('home');
相關問題
- 1. 如何從模型或助手中獲取basepath Zend Framework 3
- 2. Zend框架2:如何從模型獲取自定義配置?
- 3. 如何填充表單從模型中選擇Zend框架2
- 4. zend框架2如何捕捉異常?
- 5. 如何在Zend Framework 2中更改模塊的basePath
- 6. Zend框架2個模型架構
- 7. 如何實現在Zend框架模型
- 8. Zend框架模型
- 9. 呼叫模型+ Zend框架2
- 10. 從zend框架中的另一個模塊獲取數據2
- 11. 如何獲得模塊在Zend框架
- 12. 如何在Zend框架中捕獲SQL錯誤
- 13. 如何獲取Zend框架中所有模型的名稱
- 14. 如何在Zend框架中捕捉我自己的模型異常動作
- 15. 如何在zend框架中訪問控制器中的模型?
- 16. 在Zend框架中建模
- 17. zend框架中的Inhert模型
- 18. Zend框架:Dao模型
- 19. Zend框架模型設計
- 20. 如何在Zend框架中實現連接mysql查詢模型
- 21. 在模塊調用模型Zend框架
- 22. 從zend框架中的2個表中獲取數據
- 23. Zend框架2訪問模型類中的全局配置
- 24. Zend框架2模型中的翻譯器
- 25. 如何在zend框架中僅從模型中選擇一個特定字段
- 26. 如何從模型傳遞錯誤在Zend框架控制器
- 27. 如何在Zend框架2(ZF2)
- 28. 在zend框架中的路線2
- 29. 在Zend框架中使用Doctrine 2
- 30. 在zend框架中刪除查詢2
這可能會在控制器$ URL = $這個 - > Request()方法很多有用的 - > getUri( );然後捕獲像這樣的$ schema = $ url-> getScheme(), - > getHost()等每個值。但我想直接從Model Class獲取basePath。 –