yaf框架学习之集成pdo操作类,路由的定义以及自动加载

yaf性能高是其优点,但它没有集成mysql等类文件调用,需要自己手动封装,下面分享一下集成的pdo扩展

推荐参考下这位仁兄的:http://blog.csdn.net/doomsday0417/article/details/70810366

配置和引用文中都已经很详细的介绍了,这里不多做赘述

路由的定义

conf/application.ini里添加如下代码

 1 ;自定义路由
 2 ;顺序很重要
 3 routes.regex.type="regex"
 4 routes.regex.match="#^/list/([^/]*)/([^/]*)#"
 5 routes.regex.route.controller=Index
 6 routes.regex.route.action=action
 7 routes.regex.map.1=name
 8 routes.regex.map.2=value
 9 ;添加一个名为simple的路由协议
10 routes.simple.type="simple"
11 routes.simple.controller=c
12 routes.simple.module=m
13 routes.simple.action=a
14 ;添加一个名为supervar的路由协议
15 routes.supervar.type="supervar"
16 routes.supervar.varname=r
17 ;product节是Yaf默认关心的节, 添加一个名为rewrite的路由协议
18 routes.rewrite.type="rewrite"
19 routes.rewrite.match="/product/:name/:value"

创建一个application/Bootstrap.php文件,bootstrap.php是一个初始化文件,里面的_init的方法都会依次执行

 1 use Yaf\Bootstrap_Abstract;
 2 use Yaf\Dispatcher;
 3 use Yaf\Registry;
 4 use Yaf\Application;
 5 use Yaf\Loader;
 6 class Bootstrap extends Bootstrap_Abstract{
 7     public function _initConfig(Dispatcher $dispatcher){
 8         //配置文件添加
 9         Registry::set('config', Application::app()->getConfig());
10     }
11     //配置路由
12     public function _initRoute(Dispatcher $dispatcher){
13         $router = Dispatcher::getInstance()->getRouter();
14         @$router->addConfig(Registry::get("config")->routes);
15     }
16    /* public function _initCommonFunctions(){
17         Loader::import(Application::app()->getConfig()->application->directory . '/test.php');
18     }
19 */
20     public function _initLoader($dispatcher){
21         Loader::getInstance()->registerLocalNameSpace(array("Foo", "Local"));
22      }
23 }

注意,在public/index.php里面

创建hello.php控制器

use Yaf\Controller_Abstract;
class HelloController extends Controller_Abstract
{
    public function indexAction()
    {
        echo 'hello world';
        die;
    }
}

 

 

自动加载

第三方类文件放在application/library/目录里,公共的类文件,创建一个common目录,里面的类文件让所有的控制器都继承自这个类文件

 1 use Yaf\Controller_Abstract ;//此处与官方文档不一致,之后有详细的解释。
 2 use Yaf\Loader;
 3 use Yaf\Application;
 4 class common_Base extends Controller_Abstract
 5 {
 6     public $config;
 7     public $db;
 8     public function init()
 9     {
10         $config = Application::app()->getConfig()->db;
11         $this->db = Db_Mysql::getInstance($config);
12     }
13 }
 1 use common\Base;
 2 use Yaf\Application;
 3 class IndexController extends common_Base
 4 {
 5     public function init()
 6     {
 7         parent::init();
 8     }
 9 }
10 public function dbAction()
11     {
12         //查询
13         //$row = $this->db->fetchAll('select * from `user` where id=1');
14         //添加
15         //$result = $this->db->insert('user',['username'=>'comeon']);
16         //修改
17         //$res = $this->db->update('user',['username'=>'lelle'],'id=3');
18         //$result = $this->db->delete('user','id=4');
19         // $id = $this->db->lastInsertId();
20         //连表查询
21         $result = $this->db->fetchAll('select a.*,b.content from user as a left join comment as b on b.uid = a.id where a.id=1');
22         var_dump($result);
23         die;
24         if($result){
25             echo $result;
26         }else{
27             echo 'fail';
28         }
29         die;
30 
31     }

 

转载于:https://www.cnblogs.com/hello-php/p/7856091.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值