zoukankan      html  css  js  c++  java
  • yaf 整理札记

        由于yaf只是一个web框架,只负责处理web请求之类的基本功能,相当简洁,连db库都没有。于是试着把zend 2.2的db库,form库,validator库与yaf结合,写了一个demo。因为zend 2.2框架的命名空间跟yaf一样,所以用起来相当方便。

        下面是demo的文件架构,参照yaf手册建立的,是一个标准的架构:

    ├── application
    │   ├── Bootstrap.php
    │   ├── controllers
    │   │   ├── Album.php
    │   │   ├── Application.php
    │   │   ├── Blogs.php
    │   │   ├── Device.php
    │   │   ├── Error.php
    │   │   ├── Index.php
    │   │   └── Product.php
    │   ├── library
    │   │   ├── Album
    │   │   │   ├── Filter
    │   │   │   │   └── Album.php
    │   │   │   └── Form
    │   │   │       └── AlbumForm.php
    │   │   ├── lYaf
    │   │   │   └── Layout.php
    │   │   └── Zend
    │   │       ├── Db(下面的都是文件夹)
    │   │       ├── Form
    │   │       ├── InputFilter
    │   │       ├── ServiceManager
    │   │       ├── Stdlib
    │   │       └── Validator
    │    ── models
    │   │   └── Device.php
    │   ├── modules
    │   │   └── Mod
    │   │       ├── controllers
    │   │       │   ├── Ctrl.php
    │   │       │   └── Index.php
    │   │       └── views
    │   │           ├── ctrl
    │   │           │   └── index.phtml
    │   │           ├── index
    │   │           │   └── index.phtml
    │   │           └── index.phtml
    │   └── views
    │       ├── album
    │       │   ├── add.phtml
    │       │   └── index.phtml
    │       ├── blogs
    │       │   └── index.phtml
    │       ├── device
    │       │   ├── index.phtml
    │       │   └── list.phtml
    │       ├── error
    │       │   └── error.phtml
    │       ├── index
    │       │   ├── form.phtml
    │       │   ├── index.phtml
    │       │   └── list.phtml
    │       ├── layouts
    │       │   └── frontend.phtml
    │       ├── product
    │       │   └── info.html
    │       └── report
    │           └── index.phtml
    ├── conf
    │   ├── application.ini
    ├─  public
        ├── css
        ├── font
        ├── img
        ├── index.php
        └── js
    View Code

        接下来看看配置文件

    [common]
    application.directory = APP_PATH  "/application"
    application.dispatcher.catchException = 1
    application.dispatcher.throwException = 1
    application.view.ext = 'phtml'
    application.layout.directory=APP_PATH "/application" "/views" "/layouts"
    ;application.layoutpath = APP_PATH "/application/views"
    application.document = "layout.phtml"
    
    ;app
    application.baseUri = '' ;not used
    application.dispatcher.defaultModule = index
    application.dispatcher.defaultController = index
    application.dispatcher.defaultAction = index
    application.modules = Index,Mod
    [product : common]
    
    
    ;database 数据库链接设置
    database.params.driver   = "pdo_mysql"
    database.params.database = "data"
    database.params.username = "username"
    database.params.password = "pwd"
    database.params.hostname = "127.0.0.1"
    database.params.port     = 3306
    database.params.charset  = "UTF8"
    database.params.driver_options.1002 = "SET NAMES utf8"
    
    routes.user.type             = "regex"
    routes.user.match            = "#^/$#"
    routes.user.route.module     = Mod
    routes.user.route.controller = Ctrl
    routes.user.route.action     = index
    routes.user.map.1            = name
    routes.user.map.2            = value
    View Code

       yaf不可缺少的配置项:application.directory = APP_PATH  "/application" 建议把

    application.dispatcher.catchException = 1
    application.dispatcher.throwException = 1

    放在[product : common]外面,因为[product : common]里面的是生产环境。注意这一句:

    application.modules = Index,Mod

    modules要注意顺序,而且名字要与modules下面的文件夹名字一致,包括大小写。在数据库配置方面,有一个很重要的设置项:

    database.params.charset  = "UTF8"
    database.params.driver_options.1002 = "SET NAMES utf8"

    如果不设置database.params.driver_options.1002 = "SET NAMES utf8",从数据库取出来的中文会变成乱码。

    配置文件与Bootstrap.php文件紧密结合,那么再来看Bootstrap.php文件

    <?php
    use lYafLayout;
    /**
     * 所有在Bootstrap类中, 以_init开头的方法, 都会被Yaf调用,
     * 这些方法, 都接受一个参数:Yaf_Dispatcher $dispatcher
     * 调用的次序, 和申明的次序相同
     */
    class Bootstrap extends YafBootstrap_Abstract {
    
        private $_config;
    
        public function _initConfig() {
            $this->_config = YafApplication::app()->getConfig();
            YafRegistry::set("config", $this->_config);
        }
    
        public function _initDefaultName(YafDispatcher $dispatcher) {
            $dispatcher->setDefaultModule("Index")->setDefaultController("Index")->setDefaultAction("index");
    
        }
    
        /**
         * 设置页面layout
        */
        public function _initLayout(YafDispatcher $dispatcher){
    
            $layout = new Layout($this->_config->application->layout->directory);
            $dispatcher->setView($layout);
        }
    
        public function _initNamespaces(){
            //申明, 凡是以Zend,Local开头的类, 都是本地类
            YafLoader::getInstance()->registerLocalNameSpace(array("Zend", "Local"));
        }
    
        public function _initRoute(YafDispatcher $dispatcher) {
            //在这里注册自己的路由协议,默认使用简单路由  通过派遣器获取默认的路由器
            $router = YafDispatcher::getInstance()->getRouter();//获取路由器
            $router->addConfig($this->_config->routes);//加载路由协议
        }
        /**
         * 连接数据库,设置数据库适配器
         */
        public function _initDefaultDbAdapter(){
            $dbAdapter = new ZendDbAdapterAdapter(
                $this->_config->database->params->toArray()
            );
    
            YafRegistry::set("adapter", $dbAdapter);
        }
    
    }
    View Code

    我已经在这个文件中写了注释,在这里就不一一解释了。因为我开启了命名空间,所以整个demo都充满着反斜杠,虽然看起来别扭,但使用起来很方便。在这个Bootstrap.php文件中,使用了一个老外写的Layout库,写得很全面。

    从zend 2.2引进来的库都比较简单,比1.2的版本小多了,很容易使用。这些引用来的模块和自定义的模块(其实引入来的也就是自定义的)都要根据配置,放在相应的目录下面。在写这些自定义模块类时,建议使用yaf的命名空间,提高效率的同时也使代码简洁了很多。

    默认的module,它的controllers和views就是/application/controllers/和/application/views,而其他module,它们的controllers和views就在它们目录下面:/modules/Mod/controllers/,/modules/Mod/views/。

  • 相关阅读:
    android 源码
    android 保护
    电池信息 显示
    RGB、HSB、HSL 互相转换算法
    网页美工
    css 设计标准
    js 封闭 小结
    格式转换工具
    网页设计规范
    瀑布流分析
  • 原文地址:https://www.cnblogs.com/descusr/p/3225827.html
Copyright © 2011-2022 走看看