SoFunction
Updated on 2025-04-04

Detailed explanation of php framework Yaf routing rewrite

Usually, for the friendly URL format, the site URL will be rewrite. You can rewrite it in the webserver (Nginx) configuration or on the program side.

The following uses the Yaf framework to rewrite the URL and perform some sorting to facilitate future viewing

There are mainly the following ways to rewrite the URL of YAF, which can be used in combination.

Method 1:Perform in configuration file

;a rewrite route match request /product/*/*
routes.route_name.type="rewrite"
routes.route_name.match="/product/:name/:value"
routes.route_name.=product
routes.route_name.=info

;a regex route match request /list/*/*
routes.route_name1.type="regex"
routes.route_name1.match="#^list/([^/]*)/([^/]*)#"
routes.route_name1.=Index
routes.route_name1.=action
routes.route_name1.map.1=name
routes.route_name1.map.2=value

;a simple route match /**?c=controller&a=action&m=module
routes.route_name2.type="simple"
routes.route_name2.controller=c
routes.route_name2.module=m
routes.route_name2.action=a

;a simple router match /**?r=PATH_INFO
routes.route_name3.type="supervar"
routes.route_name3.varname=r

;a map route match any request to controller
routes.route_name4.type="map"
routes.route_name4.controllerPrefer=TRUE
routes.route_namer.delimiter="#!"

Then add the initialization function to it. The function name can be taken according to your needs and must be called with _

<?php
class Bootstrap extends Yaf_Bootstrap_Abstract{
  public function _initConfig() {
    $config = Yaf_Application::app()->getConfig();
    Yaf_Registry::set("config", $config);
  }

  public function _initRoute(Yaf_Dispatcher $dispatcher) {
    $router = $dispatcher->getRouter();
    /**
     * we can add some pre-defined routes in 
     */
    $router->addConfig(Yaf_Registry::get("config")->routes);
    /**
     * add a Rewrite route, then for a request uri: 
     * http://***/product/list/22/foo
     * will be matched by this route, and result:
     *
     * [module] => 
     * [controller] => product
     * [action] => info
     * [method] => GET
     * [params:protected] => Array
     *   (
     *     [id] => 22
     *     [name] => foo
     *   )
     * 
     */
    $route = new Yaf_Route_Rewrite(
      "/product/list/:id/:name",
      array(
        "controller" => "product",
        "action"   => "info",
      )
    ); 
    
    $router->addRoute('dummy', $route);
  }
?>

Method 2: Configure directly in the program and in an array

The following function is placed

public function _initRoute(Ap_Dispatcher $dispatcher) {
  //Register your own routing protocol here, and use static routing by default  $router = Ap_Dispatcher::getInstance()-&gt;getRouter();
  $routeConfig = array(
  $router = Ap_Dispatcher::getInstance()-&gt;getRouter();
  $routeConfig = array(
    "item" =&gt; array(
      "type" =&gt; "regex",
      "match" =&gt; "#^/(software|game)/(.*).html$#",
      "route" =&gt; array('action' =&gt; 'item'),
      "map" =&gt; array( 1 =&gt; 'data_type', 2 =&gt; 'docid' ),
    ),
    //Regular match    "category" =&gt; array(
      "type" =&gt; "regex",
      "match" =&gt; "#^/(software|game|video)/(.*)/(list_(.*).html)?$#",
      "route" =&gt; array('action' =&gt; 'list' ),
      "map" =&gt; array( 1 =&gt; 'data_type', 2 =&gt; 'cid',4 =&gt; 'page_num' ),
    ),
    //Use dynamic results:a means action    "name" =&gt; array(
      "type" =&gt; "rewrite",    //Yaf_Route_Rewrite route
      "match" =&gt; "/user-list/:a/:id", //match only /user-list/      "route" =&gt; array(
        'controller' =&gt; "user", //route to user controller,
        'action'   =&gt; ":a", //Use dynamic action      ),
    ),
  );
  $router-&gt;addConfig(new Ap_Config_Simple($routeConfig));
}

Method 3:

  /**
  * Add a rewrite route to Yaf_Router route stack
  */
  Yaf_Dispatcher::getInstance()->getRouter()->addRoute("name",
    new Yaf_Route_rewrite(
      "/product/:name/:id/*", //match request uri leading "/product"
      array(
        'controller' => "product", //route to product controller,
      ),
    )
  );

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.