Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create link url in zf2 controller

I am a starter in zend-framework 2. If i need to create a link in view.phtml, using this:

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

Now i need to create a link in controller and save to database. Any Idea?

like image 883
Arash SA Avatar asked Nov 23 '13 11:11

Arash SA


2 Answers

try this:

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
}
like image 115
Mohammad Mehdi Habibi Avatar answered Sep 23 '22 19:09

Mohammad Mehdi Habibi


invoke viewhelper:

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

zf v2.4

also

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

['force_canonical' => true] *is optional

like image 22
Isaac Limón Avatar answered Sep 21 '22 19:09

Isaac Limón