网站首页php

Yaf路由表设置

发布时间:2020-04-28 10:13:29编辑:slayer.hover阅读(4463)

    一、创建路由文件,放于 APP_PATH/conf/route.php

    <?php
    return [
        ["/product/view/:id", "goods/view"],
        ["/product/list/*", "/index/goods/list"],
        ["/product/get", "goods/get"],
        ["/product/update", "goods/update"],
        ["/product/delete", "goods/delete"],
    ];


    二、在Bootstrap.php里引入路由表


    class Bootstrap extends Yaf_Bootstrap_Abstract
    {
        public function _initRoute(Yaf_Dispatcher $dispatcher)
        {        
                $routes = include(APP_PATH . '/conf/route.php');
                if (!empty($routes)) {
                	$router = $dispatcher->getRouter();
                	foreach ($routes as $v) {
                		$router->addRoute($v[0], new Yaf_Route_Rewrite($v[0], (function ($oneRoute) {
                			if ($oneRoute[0] == '/') {
                				$route = explode('/', $oneRoute);
                				return [
                					"module"     => $route[1] ?? 'index',
                					"controller" => $route[2] ?? 'index',
                					"action"     => $route[3] ?? 'index',
                				];
                			} else {
                				$route = explode('/', $oneRoute);
                				return [
                					"module"     => 'index',
                					"controller" => $route[0] ?? 'index',
                					"action"     => $route[1] ?? 'index',
                				];
                			}
                		})($v[1])));
                	}
                }
        }
        
        ....
    }


    三、使用说明


    a.> 路由表使用 Yaf_Route_Rewrite 规则,  冒号(:)设置获取变量, 星号(*)可以匹配的(键/值)方式对数据访问。

    b.>路由表明细中的第一项参数"/product/list", 代表匹配上的url路由。

    c.>路由表明细中的第二项参数"/index/goods/list", 

          若以”/“开头,则代表(去除开头/后)以/分隔后的第一段为module值,第二段为controller,第三段为action。
                 若非”/“开头,则代表module为默认值index,以/分隔后的第一段为controller,第二段为action。

    d.>路由表可以和配置文件中的正则路由一起使用。 



评论