• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • 位置: php 中文手册 -> PECL扩展库

    Yaf(PHP框架)

    Yaf是Yet Another Framework的缩写。Yaf是一个C语言编写的PHP框架。

    Yaf的优点

    • 用C语言开发的PHP框架,相比原生的PHP,几乎不会带来额外的性能开销。
    • 所有的框架类,不需要编译,在PHP启动的时候加载,并常驻内存。
    • 更短的内存周转周期,提高内存利用率,降低内存占用率。
    • 灵巧的自动加载。支持全局和局部两种加载规则,方便类库共享。
    • 高性能的视图引擎。
    • 高度灵活可扩展的框架,支持自定义视图引擎,支持插件,支持自定义路由等等。
    • 内建多种路由, 可以兼容目前常见的各种路由协议.
    • 强大而又高度灵活的配置文件支持. 并支持缓存配置文件, 避免复杂的配置结构带来的性能损失.
    • 在框架本身,对危险的操作习惯做了禁止.
    • 更快的执行速度, 更少的内存占用.

    此 PECL 扩展未与 PHP 捆绑。安装此 PECL 扩展相关的信息可在手册中标题为 PECL 扩展的安装章节中找到。更多信息如新的发行版本、下载、源文件、 维护人员信息及变更日志等,都在此处: https://pecl.php.net/package/yaf.

    PECL 扩展的 DLL 当前不可用。参见 在 Windows 上构建章节。

    Yaf can be installed from source code by:
    cd /path/to/yaf-src/
    phpize
    ./configure
    make
    sudo make install
    

    运行时配置

    这些函数的行为受php.ini中的设置影响。

    Yaf 配置选项
    名字默认可修改范围更新日志
    yaf.libraryits PHP_INI_ALL value
    yaf.action_prefer0its PHP_INI_ALL value
    yaf.lowcase_path0its PHP_INI_ALL value
    yaf.use_spl_autoload0its PHP_INI_ALL value
    yaf.forward_limit5its PHP_INI_ALL value
    yaf.name_suffix1its PHP_INI_ALL value
    yaf.name_separatorits PHP_INI_ALL value
    yaf.cache_config0its PHP_INI_SYSTEM value
    yaf.environproductits PHP_INI_SYSTEM value
    yaf.use_namespace0its PHP_INI_SYSTEM value

    范例

    Example #1 一个典型的应用目录结构

    - index.php 
    - .htaccess 
    + conf
      |- application.ini //application config
    - application/
      - Bootstrap.php   
      + controllers
         - Index.php //default controller
      + views    
         |+ index   
            - index.phtml //view template for default action
      + modules 
      - library
      - models  
      - plugins 
    

    Example #2 入口文件

    顶层目录下的index.php是整个应用的唯一入口,应该把所有请求都重定向到这个文件(在Apache+php_mod模式下可以使用.htaccess)。

    <?php
    define("APPLICATION_PATH",  dirname(__FILE__));
    $app  = new Yaf_Application(APPLICATION_PATH . "/conf/application.ini");
    $app->bootstrap() //call bootstrap methods defined in Bootstrap.php
     ->run();
    ?>
    

    Example #3 重写规则

    #for apache (.htaccess)
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule .* index.php
    #for nginx
    server {
      listen ****;
      server_name  domain.com;
      root   document_root;
      index  index.php index.html index.htm;
      if (!-e $request_filename) {
        rewrite ^/(.*)  /index.php/$1 last;
      }
    }
    #for lighttpd
    $HTTP["host"] =~ "(www.)?domain.com$" {
      url.rewrite = (
         "^/(.+)/?$"  => "/index.php/$1",
      )
    }
    

    Example #4 应用配置文件

    [yaf]
    ;APPLICATION_PATH is the constant defined in index.php
    application.directory=APPLICATION_PATH "/application/" 
    ;product section inherit from yaf section
    [product:yaf]
    foo=bar

    Example #5 默认控制器

    <?php
    class IndexController extends Yaf_Controller_Abstract {
       /* default action */
       public function indexAction() {
           $this->_view->word = "hello world";
           //or
           // $this->getView() >word = "hello world";
       }
    }
    ?>
    

    Example #6 默认视图文件

    <html>
     <head>
       <title>Hello World</title>
     </head>
     <body>
       <?php echo $word;?>
     </body>
    </html>

    Example #7 运行应用

    以上例程的输出类似于:

    <html>
     <head>
       <title>Hello World</title>
     </head>
     <body>
       hello world
     </body>
    </html>
    

    Note:

    在yaf@github上有Yaf代码生成器,你也可以用它来生成上面的例子。

    Alternative
    you can generate the example above by using Yaf Code Generator: https://github.com/laruence/php-yaf/tree/master/tools/cg
    I success in "application" directory set to:
    - application/
     - Bootstrap.php  
     + modules 
      + Index
       + controllers
         - Index.php     //default controller
       + views  
         |+ index
          - index.phtml  //view template for default action
     - library
     - models 
     - plugins 
    And Bootstrap.php should be enter at least 3 line like these:
    <?php
    class Bootstrap extends Yaf_Bootstrap_Abstract {
    }
    YAF based on the actual case:Hooks、Event、Modules、Plugins、Multiple templates、Multiple languages、SQL Centralized Management...
    Support Electric Business Platform、OA、ERP、IaaS、PaaS、SaaS、Blog、Cms...
    Common features required by any platform: User、Acl、Menu...
    https://github.com/letwang/HookPHP
    http://us3.php.net/manual/zh/yaf.tutorials.php
    Lost default Bootstrap.php 
    <?php
      /* bootstrap class should be defined under ./application/Bootstrap.php */
      class Bootstrap extends Yaf_Bootstrap_Abstract {
        public function _initConfig(Yaf_Dispatcher $dispatcher) {
          var_dump(__METHOD__);
        }
        public function _initPlugin(Yaf_Dispatcher $dispatcher) {
          var_dump(__METHOD__);
        }
      }
    use nginx and php-fpm  you can  config:
      location / {
        try_files $uri  $uri/  /index.php$is_args$args;
        }
        location ~ \.php$ {
            fastcgi_pass  127.0.0.1:9000;
            #fastcgi_pass unix:/tmp/php-cgi.sock;
            fastcgi_index index.php;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include    fastcgi_params;
        }
    the nginx rewrite rule should be:
    if (!-e $request_filename) {
        rewrite ^/(.*) /index.php?$1 last;
    }
    When you use nginx & php-fpm config for yaf frame, you must set PATH_INFO support in nginx. Otherwise, it will not work.
    Demo :
    location / {
     try_files $uri $uri/ @path_rw;
    }
    #path_info is needed by some php frameworks like yaf/thinkphp
    location @path_rw {
     rewrite ^/(.*)$ /index.php/$1 last;
    }
    location ~ \.php {
     fastcgi_pass unix:/dev/shm/php-cgi.sock;
     fastcgi_index index.php;
     include fastcgi.conf;
     fastcgi_split_path_info ^(.+?\.php)(/.+)$;
     fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
     fastcgi_param SCRIPT_NAME $fastcgi_script_name;
     fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    应用配置

    你需要传递一个config数组或ini配置文件(参见 Yaf_Config_Ini) 给 Yaf_Application::__construct().

    Yaf的配置文件会和用户的配置文件合并。区别在于,Yaf的配置文件是以"yaf."或"application."开头,如果两项都存在,则"application."生效。

    Example #1 config为数组的例子

    <?php
        $configs = array(
                "application" => array(
                    "directory" => dirname(__FILE__),
                    "dispatcher" => array(
                          "catchException" => 0,
                        ),
                    "view" => array(
                           "ext" => "phtml",
                        ),
                    ),
               );
        $app = new Yaf_Application($config);
    ?>
    

    Example #2 config为ini文件的例子

    [yaf]
    yaf.directory = APPLICATION_PATH "/appliation"
    yaf.dispatcher.catchException = 0
    [product : yaf]
    ; user configuration list here

    Yaf Application Config
    名字默认更新日志
    application.directory
    application.ext"php"
    application.view.ext"phtml"
    application.modules"index"
    application.libraryapplication.directory . "/library"
    application.library.directoryapplication.directory . "/library"
    application.library.namespace""
    application.bootstrapapplication.directory . "/Bootstrap" . application.ext
    application.baseUri""
    application.dispatcher.defaultRoute
    application.dispatcher.throwException1
    application.dispatcher.catchException0
    application.dispatcher.defaultModule"index"
    application.dispatcher.defaultController"index"
    application.dispatcher.defaultAction"index"
    application.system

    这是配置指令的简短说明。

    application.directorystring

    应用程序的目录,包含"controllers", "views", "models", "plugins"等子目录。

    Note:

    这是唯一一个没有默认值的配置项,你需要手动指定它。

    application.extstring

    PHP脚本的扩展名,类的自动加载需要用到它( Yaf_Loader)。

    application.view.extstring

    视图模板扩展名。

    application.modulesstring

    注册的模块列表,以逗号分隔,用于路由处理,特别是当PATH_INFO超过三段的时候,

    Yaf需要用它来判断第一段是否是一个模块。

    application.librarystring

    本地类库的目录,参见Yaf_Loader 和 yaf.library。

    Note:

    Yaf2.1.6以后,该配置项也可以是一个数组,当它是数组的时候,类库的路径将尝试使用application.library.directory的值。

    application.library.directorystring

    Alias of application.library. Introduced in Yaf 2.1.6

    application.library.namespacestring

    逗号分隔的本地类库命名空间前缀。

    Yaf2.1.6以后加入

    application.bootstrapstring

    Bootstrap类脚本文件的绝对路径。

    application.baseUristring

    路由处理中需要忽略的路径前缀。举个例子,请求"/prefix/controller/action"时。如果你将application.baseUri设置为"/prefix",那么只有"/controller/action"会被当做路由路径。

    通常不需要设置此值。

    application.dispatcher.throwExceptionbool

    开启此项,Yaf会在发生错误的地方抛出异常。参见 Yaf_Dispatcher::throwException()。

    application.dispatcher.catchExceptionbool

    开启此项,如果有未捕获的异常,Yaf将会把它定向到Error controller, Error Action。参见 Yaf_Dispatcher::catchException()。

    application.dispatcher.defaultRoutestring

    默认路由,如果未指定,静态路由会被当做是默认路由,参见: Yaf_Router::addRoute()。

    application.dispatcher.defaultModulestring

    默认模块名,参见 Yaf_Dispatcher::setDefaultModule()。

    application.dispatcher.defaultControllerstring

    默认控制器名,参见 Yaf_Dispatcher::setDefaultController()。

    application.dispatcher.defaultActionstring

    默认动作名,参见 Yaf_Dispatcher::setDefaultAction()。

    application.systemstring

    在application.ini中设置Yaf运行时配置,如: application.system.lowcase_path

    Note:

    仅有PHP_INI_ALL配置项能这样设置