发布于 2015-11-29 12:27:56 | 449 次阅读 | 评论: 0 | 来源: PHPERZ

这里有新鲜出炉的精品教程,程序狗速度看过来!

Yaf PHP开发框架

Yaf,全称 Yet Another Framework,是一个C语言编写的PHP框架, 是一个以PHP扩展形式提供的PHP开发框架, 相比于一般的PHP框架, 它更快,更轻便. 它提供了Bootstrap, 路由, 分发, 视图, 插件, 是一个全功能的PHP框架。


yaf中路由有5种方式,这里简单实现正则路由(regex)。

假定场景,访问yafok.com/d,d代表数字,直接到商品详情页面.(Product模块下的product控制器下的show方法),此方法接受一个pid参数。
可以在对应控制器下通过
$paras=$this->getRequest()->getParams();接受pid等参数


方法1:application.ini中添加

[routes]
routes.product.type='regex'
routes.product.match='#^/([0-9]+)[\/]?$#'
routes.product.route.module='Product'
routes.product.route.controller='product'
routes.product.route.action='show'
routes.product.map.1='pid'

bootstrap.php中 ,首先注册初始化配置

public function _initConfig(Yaf_Dispatcher $dispatcher) {
        $this->config = Yaf_Application::app()->getConfig();
        Yaf_Registry::set('config', $this->config);
        }

然后初始化路由

public function _initRoute(Yaf_Dispatcher $dispatcher) {
      
        $router = $dispatcher->getInstance()->getRouter();
        $router->addConfig(Yaf_Registry::get('config')->routes);
    }

PS:你的routes端应该和你当前的环境匹配,比如你设置为dev环境(yaf.environ=dev)
那么需要在application.ini中把routes段加上[dev :common :routes]

参考链接:http://php.net/manual/zh/yaf-router.addconfig.php


方法2:在路由初始化时,用数组添加

$router = $dispatcher->getInstance()->getRouter();
        $route = new Yaf_Route_Regex(
                '/([0-9]+)/',

                array('module'=>'Product',
                    'controller' => 'product',
                    'action' => 'show'
                     ),

                array("1"=>'pid')
   );
            
    $router->addRoute('product', $route);

以上两种方法都行,实现效果如下:

贴一下目录



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务