$ This-& gt; url issue after ZFRewrite routing. Zend_Controller_Router_Rewrite is a standard framework router. Routing is a process. in this process, it extracts the URI endpoint (the part of the URI following the basic URL) and splits it into the parameter Zend_Controller_Router_Rewrite, which is a standard framework router. A route is a process in which it extracts the URI endpoint (the part of the URI following the basic URL) it is decomposed into parameters to determine which module, which controller, and which action in the controller should accept the request. Modules, controllers, actions, and other parameters are packaged into the Zend_Controller_Request_Http object, which is then processed by Zend_Controller_Dispatcher_Standard. Route occurs only once: before the request is initially received and the first controller is dispatched.
Zend_Controller_Router_Rewrite is designed to consider the functionality of the mod_rewrite-like when using a pure php structure. It is very loose based on Ruby on Rails and does not require any previous web server URL rewriting knowledge. It is designed to work with a single Apache mod_rewrite rule (one of them:
RewriteEngine onRewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php
Or:
RewriteEngine onRewriteCond %{SCRIPT_FILENAME} !-fRewriteCond %{SCRIPT_FILENAME} !-dRewriteRule ^(.*)$ index.php/$1 Use vro
To correctly use the rewrite router, you must initialize it, add some custom routes and inject them into the controller. The following code demonstrates the process:
getRouter(); // returns a rewrite router by default$router->addRoute('user',new Zend_Controller_Router_Route('user/:username', array('controller' => 'user', 'action' => 'info')));
The core of RewriteRouter is the definition of custom routes. A route is added by calling the addRoute method of RewriteRouter and passing a new instance of Zend_Controller_Router_Route_Interface implemented by the class. For example:
addRoute('user', new Zend_Controller_Router_Route('user/:username'));
The Rewrite router has four basic types of routes (one of which is special ):
- Zend_Controller_Router_Route
- Zend_Controller_Router_Route_Static
- Zend_Controller_Router_Route_Regex
- Default route
A route can be used countless times to create a chain or a custom application route plan. You can use any number of routes in any configuration. in addition to module routes, it is best to use them once and use them as a common route (for example, as the default one ). Each route will be described in detail later.
The first parameter of addRoute is the route name. It is used as the authority to obtain a route from a router (for example, for URL generation purposes ). The second parameter is the route itself. The most common use of the route name is through the Zend_View_url assistant method:
url(array('username' => 'martel'), 'user') ?>">Martel
A route is a simple process, which is iterated through all the routes provided and the URI definitions matching its current request. When a positive match is found, the variable value is returned from the routing instance and injected into the Zend_Controller_Request object for future use in the dispatcher and the controller created by the user. If it is a negative match, the next route in the chain is checked. The value returned from the route is from the URL parameter or the default value used for definition. You can use the Zend_Controller_Request: getParam () or Zend_Controller_Action: _ getParam () methods to access these variables.
There are three special variables available for your routing-'Module ', 'controller', and 'action '. These special variables are used by Zend_Controller_Dispatcher to find the controller and action, and then dispatch the past.
Zend_Controller_Router_Rewrite is pre-configured with the default route. it matches the URL in the form of controller/action. In addition, the module name can be specified as the first path parameter, allowing this module/controller/action form of URIs. Finally, it matches any other parameter-controller/action/var1/value1/var2/value2 appended to the URI by default.
Some routing matching examples:
// Assuming the following:$ctrl->setControllerDirectory( array( 'default' => '/path/to/default/controllers', 'news' => '/path/to/news/controllers', 'blog' => '/path/to/blog/controllers' ));Module only:http://example/news module == newsInvalid module maps to controller name:http://example/foo controller == fooModule + controller:http://example/blog/archive module == blog controller == archiveModule + controller + action:http://example/blog/archive/list module == blog controller == archive action == listModule + controller + action + params:http://example/blog/archive/list/sort/alpha/date/desc module == blog controller == archive action == list sort == alpha date == desc
The default route is a simple Zend_Controller_Router_Route_Module object stored in RewriteRouter whose index is 'default. It is created more or less as follows:
addRoute('default', $compat);
If you do not want this special default route in your route plan, you can rewrite your own 'default' route (for example, store it under 'default) or use removeDefaultRoutes () to completely clear it:
removeDefaultRoutes();
Zend_Controller_Router_Route
Zend_Controller_Router_Route is a standard framework route. It combines the ease of use of flexible routing definition. Each route contains basic URL ing (static and dynamic parts (variables) and can be initialized by default or according to different requirements.
Let's imagine that our hypothetical application will need some information pages of wide area content authors. We want to point the browser to http://domain.com/author/martelto see information called "martel. The routing with this function looks like this:
'profile', 'action' => 'userinfo' ));$router->addRoute('user', $route);
The first parameter of the constructor in Zend_Controller_Router_Route is the definition of the route. it will match a URL. The routing definition contains static and dynamic parts, which are separated by the forward slash. The static part is only a simple character: author. The dynamic part, called a variable, is marked with the preset colon: username.
When you direct your browser to ingress. The variables returned in this example may be represented as the following key-value pair array:
'martel', 'controller' => 'profile', 'action' => 'userinfo');
Later, based on these values, Zend_Controller_Dispatcher_Standard should call the userinfoAction () method in the ProfileController class (in the default module. You will be able to access all variables by using the Zend_Controller_Action: _ getParam () or Zend_Controller_Request: getParam () method:
getRequest(); $username = $request->getParam('username'); $username = $this->_getParam('username');}
The routing definition can include an additional special character-wildcard character-expressed. It is used to obtain parameters, similar to the default module routing (var => value defined in URI ). The following routes imitate the module routing behavior more or less:
'default'));$router->addRoute('default', $route);Variable request
When the variable request is set, the third parameter can be added to the constructor of Zend_Controller_Router_Route. These are defined as part of a regular expression:
2006, 'controller' => 'archive', 'action' => 'show' ), array('year' => '\d+'));$router->addRoute('archive', $route);
With the route defined above, the router will match only when the year variable contains digital data, for example, http://domain.com/archive/2345. Urllike http://example.com/archive/testwill not be matched and the control will be passed to the next link.
A simple application example
- Url (array ('controller' => 'index'), null, true);?> "Title =" Home "> homepage
- Url (array ('controller' => 'news', 'Action' => 'index'), null, true);?> "Title =" news "> news
Bytes. Routing is a process. in this process, it extracts the URI endpoint (the part of the URI following the basic URL) and splits it into parameters...