Php – How to create a Zend Framework controller/action url outside of controller

command linePHPzend-framework

I use the zend framework 1.11.2. I want to create URLs to controller actions that can be used inside email texts.

Within controllers I have done that with Zend_View_Helper_ServerUrl and Zend_View_Helper_Url like this:

$serverUrlHelper = new Zend_View_Helper_ServerUrl();
$unsubscribeUrl = $serverUrlHelper->serverUrl(
    $this->getHelper('url')->url(
       array(
           'module' => $this->getFrontController()->getDefaultModule(),
           'controller' => 'newsletter',
           'action' => 'unsubscribe',
           'email' => $email
       ),
       'default',   // the route
       true));

Now I want to do this not within a controller but instead from command line. I already managed to start a zend controller/action by following the steps described here Running a Zend Framework action from command line. But the Application_Router_Cli does not have an implementation for the assemble function. I never have done something deeply with zend routing. Could anyone could please give me an hint how to implement it?

Best Answer

I am not sure what default routes means in your application.ini but i know your code must be like this :

   $serverUrlHelper = new Zend_View_Helper_ServerUrl();
   $unsubscribeUrl = $serverUrlHelper->serverUrl(
    $this->getHelper('url')->url(
       array(
           'module' => $this->getFrontController()->getDefaultModule(),
           'controller' => 'newsletter',
           'action' => 'unsubscribe',
           'email' => $email
       ),
       null,   // the route 
       true));

since your are assmble the route manually by using array of module , controller , view

so the second param of the url view help must be null , and in the case you have a route in your application.ini and you want this link to follow that route your function must be like this :

 $serverUrlHelper = new Zend_View_Helper_ServerUrl();
   $unsubscribeUrl = $serverUrlHelper->serverUrl(
    $this->getHelper('url')->url(
        null,
       "email",   // aka the route name
       true));

hopefully i explain it in easy way

Related Topic