2013-06-05 57 views

回答

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'); 
+0

這可能會在控制器$ URL = $這個 - > Request()方法很多有用的 - > getUri( );然後捕獲像這樣的$ schema = $ url-> getScheme(), - > getHost()等每個值。但我想直接從Model Class獲取basePath。 –