zoukankan      html  css  js  c++  java
  • 开始yaf之旅

    目录结构

    + public  //网站根目录
       - index.php //入口文件
       - .htaccess //重写规则    
    + conf
      |- application.ini //配置文件   
    application/
      + controllers
         - Index.php //默认控制器
      + views    
         |+ index   //控制器
            - index.phtml //默认视图
      + modules //其他模块
      - library    //组件目录
      - models  //model目录
      - plugins //插件目录

    入口文件

    入口文件是所有请求的入口, 一般都借助于rewrite规则, 把所有的请求都重定向到这个入口文件.

    一个经典的入口文件public/index.php

    <?php
    define("APP_PATH",  realpath(dirname(__FILE__) . '/../')); /* 指向public的上一级 */
    $app  = new Yaf_Application(APP_PATH . "/conf/application.ini");
    $app->run();

    重写规则

    除非我们使用基于query string的路由协议(Yaf_Route_SimpleYaf_Route_Supervar), 否则我们就需要使用WebServer提供的Rewrite规则, 把所有这个应用的请求, 都定向到上面提到的入口文件.

    修改.htaccess文件

    Nginx的Rewrite (nginx.conf)

    server {
      listen 80;
      server_name  yaf.demo.com;
      root   document_root;
      index  index.php index.html index.htm;
    
      if (!-e $request_filename) {
        rewrite ^/(.*)  /index.php/$1 last;
      }
    }

    配置文件

    在Yaf中, 配置文件支持继承, 支持分节. 并对PHP的常量进行支持. 你不用担心配置文件太大造成解析性能问题, 因为Yaf会在第一个运行的时候载入配置文件, 把格式化后的内容保持在内存中. 直到配置文件有了修改, 才会再次载入.

    一个简单的配置文件application/conf/application.ini

    [common]
    application.directory = APP_PATH  "/application"
    application.dispatcher.catchException = 0
    application.dispatcher.throwException = 0
    application.view.ext = 'phtml'
    [product : common]
    ;enable the error controller
    application.dispatcher.catchException=1

    控制器

    在Yaf中, 默认的模块/控制器/动作, 都是以Index命名的, 当然,这是可通过配置文件修改的.

    对于默认模块, 控制器的目录是在application目录下的controllers目录下, Action的命名规则是"名字+Action"

    默认控制器application/controllers/Index.php

     
    <?php
    class IndexController extends Yaf_Controller_Abstract {
       public function indexAction() {//默认Action
           $this->getView()->assign("content", "Hello World");
       }
    }
    ?>

    视图文件

    Yaf支持简单的视图引擎, 并且支持用户自定义自己的视图引擎, 比如Smarty.

    对于默认模块, 视图文件的路径是在application目录下的views目录中以小写的action名的目录中.

    一个默认Action的视图application/views/index/index.phtml

     
    <html>
     <head>
       <title>Hello yaf</title>
     </head>
     <body>
      <?php echo $content;?>
     </body>
    </html>

    然后在浏览器输入nginx.conf设置的servername,yaf.demo.com。是不是出来了??

    表 4.2. Yaf可选配置项

    名称值类型默认值说明
    application.ext String php PHP脚本的扩展名
    application.bootstrap String Bootstrapplication.php Bootstrap路径(绝对路径)
    application.library String application.directory + "/library" 本地(自身)类库的绝对目录地址
    application.baseUri String NULL 在路由中, 需要忽略的路径前缀, 一般不需要设置, Yaf会自动判断.
    application.dispatcher.defaultModule String index 默认的模块
    application.dispatcher.throwException Bool True 在出错的时候, 是否抛出异常
    application.dispatcher.catchException Bool False 是否使用默认的异常捕获Controller, 如果开启, 在有未捕获的异常的时候, 控制权会交给ErrorController的errorAction方法, 可以通过$request->getException()获得此异常对象
    application.dispatcher.defaultController String index 默认的控制器
    application.dispatcher.defaultAction String index 默认的动作
    application.view.ext String phtml 视图模板扩展名
    application.modules String Index 声明存在的模块名, 请注意, 如果你要定义这个值, 一定要定义Index Module
    application.system.* String * 通过这个属性, 可以修改yaf的runtime configure, 比如application.system.lowcase_path, 但是请注意只有PHP_INI_ALL的配置项才可以在这里被修改, 此选项从2.2.0开始引入



  • 相关阅读:
    ORACLE中order by造成分页不正确原因分析
    各种数据库分页语句整理以及Oracle数据库中的ROWNUM和ORDER BY的区别
    Spring配置文件一直报错的根源所在
    java加密用到了BASE64Decoder时报错信息:Access restriction: The type BASE64Encoder is not accessible due to restrict
    Eclipse报错:An internal error occurred during: "Building workspace". Java heap space),卡死解决办法
    云数据库场景问题经典案例——分页优化
    DDL失败案例
    Java笔试题解答
    模拟购物车表格
    addTodo 模型
  • 原文地址:https://www.cnblogs.com/descusr/p/3210599.html
Copyright © 2011-2022 走看看