yaf 学习

####yaf 学习 C扩展的php框架 Yaf采用自动加载机制,如果你的类库不符合yaf的标准,请将你的php.ini中的yaf.use_spl_autoload设置为1,

使用bootstrap,在yaf中提供整个项目的启动配置,可以将很多自定义的方法放到里面

使用bootstap

####yaf 使用cli来调试程序 [Notice]

yaf 使用php cli来调试程序,首先确保你的phpinfo()php -m下都能找到yaf扩展,否则会报Yaf_Application Not Found错误

/*加载你的配置文件*/
$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); 开启捕获错误

之后在你的controller控制器下创建Error.php

在文件中写入以下内容

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

转载于:https://my.oschina.net/kakoi/blog/727254

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值