2013-11-23 31 views
5

Jestem rozrusznik w zend-framework 2. jeśli trzeba utworzyć łącze w view.phtml, wykorzystując w ten sposób:Utwórz link URL w sterowniku ZF2

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

Teraz trzeba utworzyć łącze w kontrolerze i zapisz do bazy danych. Każdy pomysł?

+1

Użyj 'url()' plugin sterownik -> http://framework.zend.com/manual/2.2/en/modules/zend .mvc.plugins.html # zend-mvc-controller-plugins-url – Crisp

Odpowiedz

14

spróbować tego:

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

Dużo lepiej jest użyć $ this-> url(), nie musisz go pobierać z ViewHelperManager. – kilop

+0

tak, ale jest to ogólny sposób tworzenia adresu URL, w kontrolerze lub w onBootstrap. –

0

Zastosowanie url() kontroler wtyczki:

public function fooAction(){ 

    //other code 

    $this->url('routename'); 

    //other stuff 
} 
3

invoke viewhelper:

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

ZF v2.4

również

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

[ 'force_canonical' => true] * jest obowiązkowe