yaf框架學習之集成pdo操做類,路由的定義以及自動加載

yaf性能高是其優勢,但它沒有集成mysql等類文件調用,須要本身手動封裝,下面分享一下集成的pdo擴展php

推薦參考下這位仁兄的:http://blog.csdn.net/doomsday0417/article/details/70810366mysql

配置和引用文中都已經很詳細的介紹了,這裏很少作贅述sql

路由的定義bootstrap

conf/application.ini裏添加以下代碼app

 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裏面fetch

建立hello.php控制器ui

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

 

 

自動加載this

第三方類文件放在application/library/目錄裏,公共的類文件,建立一個common目錄,裏面的類文件讓全部的控制器都繼承自這個類文件spa

 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     }
相關文章
相關標籤/搜索