2013-11-23 76 views
5

我在Zend的框架首发2. 如果我需要在view.phtml创建一个链接,使用此:创建ZF2控制器链接的URL

$this->url('router',array()) 

现在我需要创建控制器链接,保存到数据库。 任何想法?

+1

使用'URL()'控制器插件 - > http://framework.zend.com/manual/2.2/en/modules/zend .mvc.plugins.html#zend-mvc-controller-plugins-url – Crisp

回答

14

试试这个:

public function someAction() 
{ 
    //codes 

    //use url plugin in controller 
    $link = $this->url()->fromRoute('router', array()); 
    //or use ViewHelperManager in controller or other place that you have ServiceManager 
    $link = $this->getServiceLocator()->get('ViewHelperManager')->get('url')->__invoke('router',array()); 

    //codes 
} 
+0

更好的方法是使用$ this-> url()插件,你不必从ViewHelperManager中取得它。 – kilop

+0

是的,但这是在控制器或onBootstrap中创建网址的一般方法。 –

0

使用URL()控制器插件:

public function fooAction(){ 

    //other code 

    $this->url('routename'); 

    //other stuff 
} 
3

调用视图助手:

public function algoAction() 
{ 
    $url = $this->getServiceLocator() 
      ->get('viewhelpermanager') 
      ->get('url'); 
    $link = $url('router',array()); 
} 

ZF V2.4

$this->url()->fromRoute('home',[],['force_canonical' => true]); 

[ 'force_canonical'=>真] *是可选