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 }