2013-11-23 18 views
5

ZF2コントローラ内のリンクURLを作成します。2. 私はこの使用して、view.phtmlにリンクを作成する必要がある場合:私はZendのフレームワークでスターター午前

$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のurlを作成する一般的な方法です。 –

0

使用するURL()コントローラプラグイン:

public function fooAction(){ 

    //other code 

    $this->url('routename'); 

    //other stuff 
} 
3

ボークなViewHelper:

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' =>真] *オプションである

関連する問題