How can I create a Zend Framework controller / action URL outside the controller?

I am using the zend structure 1.11.2. I want to create URLs for controller actions that can be used inside email texts.

Inside the controllers, I did this with Zend_View_Helper_ServerUrl and Zend_View_Helper_Url as follows:

$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 inside the controller, but instead from the command line. I have already managed to start the zend controller / action by following the steps described here Starting the Zend Framework action from the command line . But Application_Router_Cli does not have an implementation for the assemble function. I have never done anything with zend routing. Can someone give me a hint how to implement it?

+4
source share
4 answers

Instead of subclassing from Zend_Controller_Router_Abstract to Application_Router_Cli (as described in this tutorial), subclassing from Zend_Controller_Router_Rewrite. This should give you the assembly () function and allow you to use the URL helper, as you would normally use it in a web context.

+1
source

I'm not sure what default route means in your application.ini , but I know your code should 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 route is routed manually using the array module , controller , view

therefore, the second help parameter url view should be zero, and if you have a route in application.ini , and you want this link to match this route, your function should be like this:

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

hope i just explain it

+3
source

optima1 answer started me on the right track, but I would like to share some additional steps that I need to help the url() and serverUrl() CLI serverUrl() script.

Medium detection

The first task is to determine the server environment. This often happens in a .htaccess variable or FastCGI environment variable, so it does not load into the CLI. I am using the INI file to match the server name in the environment.

 [ServerEnvironments] PROD-SERVER = "production" STAGE-SERVER = "staging" MY-LOCAL-VM = "development" 

Then my CLI script sets the APPLICATION_ENV constant accordingly.

 $iniConfig = parse_ini_file(rtrim(dirname(__FILE__), '\/') . '/servers.ini', TRUE); $environment = isset($iniConfig['ServerEnvironments'][getenv('COMPUTERNAME')]) ? $iniConfig['ServerEnvironments'][getenv('COMPUTERNAME')] : 'production'; defined('APPLICATION_ENV') || define('APPLICATION_ENV', $environment); 

Superglobal Server Population

The rest of the script continues normally, as shown in David's example . However, I load all resources (empty call to bootstrap() ). I also add an additional INI file when creating an instance of Zend_Application , for example:

 $application = new Zend_Application( APPLICATION_ENV, array('config' => array(APPLICATION_PATH . '/configs/application.ini', APPLICATION_PATH . '/configs/cli.ini'))); 

The INI CLI file will store the request information, which is usually provided by the web server.

 [production] cli.request.serverVars.HTTPS = "on" cli.request.serverVars.HTTP_SCHEME = "https" ; Host name, including port if non-standard. cli.request.serverVars.HTTP_HOST = "example.com" ; Absolute path to the web root. Exclude trailing slash. cli.request.serverUrl = "https://example.com" ; Relative path from web root to the Zend Framework application. Include leading slash. cli.request.baseUrl = "/ZF" [staging : production] cli.request.serverUrl = "https://stage.example.com" cli.request.serverVars.HTTP_HOST = "stage.example.com" [development : production] cli.request.serverUrl = "https://dev.example.com:4433" cli.request.serverVars.HTTP_HOST = "dev.example.com:4433" 

After calling bootstrap() CLI script populates the $_SERVER module. Note that during bootstrap, I save the application configuration to Zend_Registry .

 $config = Zend_Registry::get('config'); foreach($config->cli->request->serverVars as $name => $value) { $_SERVER[$name] = $value; } 

Build URIs and Submit

The last hurdle is that I use custom routes instead of /module/controller/action , and the latter format is disabled ( Zend_Controller_Router_Rewrite::removeDefaultRoutes() ). Thus, the CLI script should indicate the route, not the module, controller, and action.

 $route = 'My/Custom/Route'; $spoofedUri = $config->cli->request->serverUrl . rtrim($config->cli->request->baseUrl, '/') . '/' . $route; $request = new Zend_Controller_Request_Http($spoofedUri); $request->setBaseUrl($config->cli->request->baseUrl); $front = Zend_Controller_Front::getInstance(); $front->setRequest($request) ->setResponse(new Zend_Controller_Response_Cli()) ->dispatch(); 

This may not solve every mismatch between the web and CLI requests (you may need to fill out more $_SERVER ), but url() and serverUrl() were set for me.

+1
source

In your CLI script, if you do not load a resource of the form:

$application->getBootstrap()->bootstrap(array('db', 'mail', 'view'));

then you can use the same view helpers, as always, right?

0
source

Source: https://habr.com/ru/post/1338956/


All Articles