####yaf 学习 C扩展的php框架 Yaf采用自动加载机制,若是你的类库不符合yaf的标准,请将你的php.ini
中的yaf.use_spl_autoload
设置为1,php
使用bootstrap
,在yaf
中提供整个项目的启动配置,能够将不少自定义的方法放到里面bootstrap
使用bootstapapi
####yaf 使用cli来调试程序 [Notice]php框架
yaf 使用php cli
来调试程序,首先确保你的phpinfo()
和php -m
下都能找到yaf
扩展,不然会报Yaf_Application Not Found
错误session
/*加载你的配置文件*/ $app = new Yaf_Application(APP_PATH . "/conf/application.ini",'test'); /*将传统的bootstrap入口文件替换成为路由转发*/ $app->getDispatcher()->dispatch( new Yaf_Request_Simple());
####yaf抛出异常 首先须要在你的入口文件index.php
添加 $app->getDispatcher()->catchException(true);
开启捕获错误app
以后在你的controller控制器下建立Error.php
框架
在文件中写入如下内容ide
class ErrorController extends BaseController{ /*当这些保存$exception->getCode()时候,直接显示404*/ public function errorAction($exception){ switch($exception->getCode()) { case YAF_ERR_LOADFAILD: case YAF_ERR_LOADFAILD_MODULE: case YAF_ERR_LOADFAILD_CONTROLLER: case YAF_ERR_NOTFOUND_CONTROLLER: case YAF_ERR_LOADFAILD_ACTION: case YAF_ERR_NOTFOUND_CONTROLLER: //404 header( "HTTP/1.1 404 Not Found" ); header( "Status: 404 Not Found" ); break; default: echo "No Action"; break; } } }
引伸一些yaf的报错常量学习
YAF_VERSION(Yaf\VERSION) Yaf框架的三位版本信息 YAF_ENVIRON(Yaf\ENVIRON Yaf的环境常量, 指明了要读取的配置的节, 默认的是product YAF_ERR_STARTUP_FAILED(Yaf\ERR\STARTUP_FAILED) Yaf的错误代码常量, 表示启动失败, 值为512 YAF_ERR_ROUTE_FAILED(Yaf\ERR\ROUTE_FAILED) Yaf的错误代码常量, 表示路由失败, 值为513 YAF_ERR_DISPATCH_FAILED(Yaf\ERR\DISPATCH_FAILED) Yaf的错误代码常量, 表示分发失败, 值为514 YAF_ERR_NOTFOUND_MODULE(Yaf\ERR\NOTFOUD\MODULE) Yaf的错误代码常量, 表示找不到指定的模块, 值为515 YAF_ERR_NOTFOUND_CONTROLLER(Yaf\ERR\NOTFOUD\CONTROLLER) Yaf的错误代码常量, 表示找不到指定的Controller, 值为516 YAF_ERR_NOTFOUND_ACTION(Yaf\ERR\NOTFOUD\ACTION) Yaf的错误代码常量, 表示找不到指定的Action, 值为517 YAF_ERR_NOTFOUND_VIEW(Yaf\ERR\NOTFOUD\VIEW) Yaf的错误代码常量, 表示找不到指定的视图文件, 值为518 YAF_ERR_CALL_FAILED(Yaf\ERR\CALL_FAILED) Yaf的错误代码常量, 表示调用失败, 值为519 YAF_ERR_AUTOLOAD_FAILED(Yaf\ERR\AUTOLOAD_FAILED) Yaf的错误代码常量, 表示自动加载类失败, 值为520 YAF_ERR_TYPE_ERROR(Yaf\ERR\TYPE_ERROR) Yaf的错误代码常量, 表示关键逻辑的参数错误, 值为521
####yaf引用smarty来展现输出测试
测试.ini
文件的smarty
配置是否正确加载
$config = new Yaf_Config_Ini(APP_PATH.'/conf/application.ini', 'base'); echo $config->get("smarty")->compile_dir;
在bootstrap.php中加载视图引擎&相关配置文件
public function _initView(Yaf_Dispatcher $dispatcher){ /*加载整合yaf&smarty模板类*/ Yaf_Loader::import(APP_LIBRARY."/Adapter.php"); /*加载application.ini下的smarty的*/ $config = new Yaf_Config_Ini(APP_PATH.'/conf/application.ini', 'smarty'); $smarty = new Smarty_Adapter(null , $config->get("smarty")); Yaf_Dispatcher::getInstance()->setView($smarty); }
而且在bootstrap中添加
/*关闭yaf自动渲染,不然数据会出现两次*/ public function _init(){ Yaf_Dispatcher::getInstance()->disableView(); }
配置文件中具体smarty配置 [smarty] smarty.left_delimiter = "{" smarty.right_delimiter = "}" smarty.template_dir = APP_PATH "/application/views/" smarty.compile_dir = APP_PATH "/application/views/cache/compile" smarty.cache_dir = APP_PATH "/application/views/cache/"
在library
下建立Adapter.php
存入整合yaf&smarty
的语法
<?php /*Yaf 引入smarty*/ /*具体要看smarty在你项目中的位置*/ Yaf_Loader::import( Smarty."/Smarty.class.php"); Yaf_Loader::import( Smarty."/sysplugins/smarty_internal_templatecompilerbase.php"); Yaf_Loader::import( Smarty."/sysplugins/smarty_internal_templatelexer.php"); Yaf_Loader::import( Smarty."/sysplugins/smarty_internal_templateparser.php"); Yaf_Loader::import( Smarty."/sysplugins/smarty_internal_compilebase.php"); Yaf_Loader::import( Smarty."/sysplugins/smarty_internal_write_file.php"); class Smarty_Adapter implements Yaf_View_Interface { /** * Smarty object * @var Smarty */ public $_smarty; /** * 实例化smarty * * @param string $tmplPath * @param array $extraParams * @return void */ public function __construct($tmplPath = null, $extraParams = array()) { $this->_smarty = new Smarty; if (null !== $tmplPath) { $this->setScriptPath($tmplPath); } // var_dump($extraParams); foreach ($extraParams as $key => $value) { $this->_smarty->$key = $value; } } /** * Return the template engine object * * @return Smarty */ public function getEngine() { return $this->_smarty; } /** * Set the path to the templates * * @param string $path The directory to set as the path. * @return void */ public function setScriptPath($path) { if (is_readable($path)) { $this->_smarty->template_dir = $path; return; } throw new Exception('Invalid path provided'); } /** * Retrieve the current template directory * * @return string */ public function getScriptPath() { return $this->_smarty->template_dir; } /** * Alias for setScriptPath * * @param string $path * @param string $prefix Unused * @return void */ public function setBasePath($path, $prefix = 'Zend_View') { return $this->setScriptPath($path); } /** * Alias for setScriptPath * * @param string $path * @param string $prefix Unused * @return void */ public function addBasePath($path, $prefix = 'Zend_View') { return $this->setScriptPath($path); } /** * Assign a variable to the template * * @param string $key The variable name. * @param mixed $val The variable value. * @return void */ public function __set($key, $val) { $this->_smarty->assign($key, $val); } /** * Allows testing with empty() and isset() to work * * @param string $key * @return boolean */ public function __isset($key) { return (null !== $this->_smarty->get_template_vars($key)); } /** * Allows unset() on object properties to work * * @param string $key * @return void */ public function __unset($key) { $this->_smarty->clear_assign($key); } /** * Assign variables to the template * * Allows setting a specific key to the specified value, OR passing * an array of key => value pairs to set en masse. * * @see __set() * @param string|array $spec The assignment strategy to use (key or * array of key => value pairs) * @param mixed $value (Optional) If assigning a named variable, * use this as the value. * @return void */ public function assign($spec, $value = null) { if (is_array($spec)) { $this->_smarty->assign($spec); return; } $this->_smarty->assign($spec, $value); } /** * Clear all assigned variables * * Clears all variables assigned to Zend_View either via * {@link assign()} or property overloading * ({@link __get()}/{@link __set()}). * * @return void */ public function clearVars() { $this->_smarty->clear_all_assign(); } /** * Processes a template and returns the output. * * @param string $name The template to process. * @return string The output. */ public function render($name, $value = NULL) { return $this->_smarty->fetch($name); } public function display($name, $value = NULL) { echo $this->_smarty->fetch($name); } }
在你的Action
方法里写入
$this->getView()->assign('content', 'hello World!!!!!!!!!!!'); $this->getView()->display('index/index.tpl');
以后在你的.tpl
写入
{$content}
注意模板的路径,就能够在页面中看到输出的效果了
####yaf使用session 获取session
实例$session = Yaf_Session::getInstance()
, 设置一个username
$session->username='test'
获取username
$session->username
删除session
unset($session->username)
yaf没法在本身的controller
使用__construct
方法,所以Yaf_Controller_Abstract
提供了一个魔术方法Yaf_Controller_Abstract::init()
当controller
被实例化的时候被调用
####yaf使用多模块的使用
在application\conntrollers\
下定义的控制器都属于Index
模块
那么项目中不可能只有一个模块,那么多模块咱们如何处理呢?
在目录application\
下新建目录modules
。除了默认模块,其余模块都放在application\modules\
下。
新建一个模块,模块名自定义。假设个人新模块叫Api吧。 建立目录application\modules\Api
。
修改项目配置文件conf\application.ini:
application.modules = "Index,Api"
在新模块下建立控制器 在目录application\modules\Api\
下建立控制器目录controllers
,用于存放模块Api下的控制器文件。
新建文件application\modules\Api\controllers\Passport.php:
<?php class PassportController extends Yaf_Controller_Abstract { public function loginAction() { echo '我是登陆接口'; return false; } }
http://127.0.0.1/api/passport/login