PHP framework Yaf route rewriting_php example detailed explanation

墨辰丷
Release: 2023-03-27 08:50:01
Original
1325 people have browsed it

This article mainly introduces the PHP framework Yaf routing rewriting_php detailed examples. Interested friends can refer to it. I hope it will be helpful to everyone.

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

Use the Yaf framework below Rewrite the URL and do some sorting to facilitate future viewing.

YAF’s URL rewriting methods mainly include the following, which can be used comprehensively

Method 1:In After doing

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

;a regex route match request /list/*/*
routes.route_name1.type="regex"
routes.route_name1.match="#^list/([^/]*)/([^/]*)#"
routes.route_name1.route.controller=Index
routes.route_name1.route.action=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="#!"
Copy after login

in the configuration file, add the initialization function in Bootstrap.php. The function name can be chosen according to your own needs. It must start with _ to be called.

<?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 application.ini
     */
    $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(&#39;dummy&#39;, $route);
  }
?>
Copy after login

Method 2: Directly in the program, configure in array mode

The following functions are placed in Bootstrap.php

public function _initRoute(Ap_Dispatcher $dispatcher) {
  //在这里注册自己的路由协议,默认使用static路由
  $router = Ap_Dispatcher::getInstance()->getRouter();
  $routeConfig = array(
  $router = Ap_Dispatcher::getInstance()->getRouter();
  $routeConfig = array(
    "item" => array(
      "type" => "regex",
      "match" => "#^/(software|game)/(.*).html$#",
      "route" => array(&#39;action&#39; => &#39;item&#39;),
      "map" => array( 1 => &#39;data_type&#39;, 2 => &#39;docid&#39; ),
    ),
    //正则匹配
    "category" => array(
      "type" => "regex",
      "match" => "#^/(software|game|video)/(.*)/(list_(.*).html)?$#",
      "route" => array(&#39;action&#39; => &#39;list&#39; ),
      "map" => array( 1 => &#39;data_type&#39;, 2 => &#39;cid&#39;,4 => &#39;page_num&#39; ),
    ),
    //使用动态结果 :a 表示action
    "name" => array(
      "type" => "rewrite",    //Yaf_Route_Rewrite route
      "match" => "/user-list/:a/:id", //match only /user-list/开头的
      "route" => array(
        &#39;controller&#39; => "user", //route to user controller,
        &#39;action&#39;   => ":a", //使用动态的action
      ),
    ),
  );
  $router->addConfig(new Ap_Config_Simple($routeConfig));
}
Copy after login

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(
        &#39;controller&#39; => "product", //route to product controller,
      ),
    )
  );
Copy after login

Related recommendations:

Installation within PHP7.1yafDetailed explanation of extension steps

Yaf framework installation guide

PHP7.1 installationyafExtension method

The above is the detailed content of PHP framework Yaf route rewriting_php example detailed explanation. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!