Home >Backend Development >PHP Tutorial >The mystery of ThinkPHP application mode extension_PHP tutorial

The mystery of ThinkPHP application mode extension_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:22:54895browse

The mystery of ThinkPHP application mode extension

ThinkPHP application mode provides the opportunity to transform the core framework, allowing your application to adapt to more environments and different needs. Each application mode has its own mode definition file. Compared with the ThinkPHP3.1 version, the ThinkPHP3.2 version has a clearer and clearer extension of the application mode. In the ThinkPHP3.1 version, Cli, Lite, Thin, AMF, and PHPRPC are defined. , REST mode, its definition method is similar to that of ThinkPHP version 3.2. If necessary, you can refer to the modification. The Cli mode is built into the ThinkPHP framework. It can be used normally without defining the Cli mode separately. If you need more detailed adjustments, please refer to 3.1 Cli run mode extension written in . ThinkPHP also provides a convenient mode switching method between development environment and formal environment. Let us follow the running process of ThinkPHP to analyze the mystery of its application mode expansion.

1. Use of application mode

Before exploring application mode extensions, let’s take a look at how to use application mode. Generally, the constant APP_MODE is defined as the application mode name in the entry file. However, when analyzing the ThinkPHP framework entry file, we learned that the default mode of the framework is common mode (common) and can automatically identify the sae environment. Of course, the premise is that the APP_MODE constant is not defined. Of course, ThinkPHP can automatically identify CLI and CGI modes, and when running in CLI and CGI environments, the ThinkPHP framework will automatically make subtle adjustments to these two environments in the default mode. Of course, you can also expand these two application modes yourself.

if(function_exists('saeAutoLoader')){// 自动识别SAE环境
    defined('APP_MODE')     or define('APP_MODE',      'sae');
    defined('STORAGE_TYPE') or define('STORAGE_TYPE',  'Sae');
}else{
    defined('APP_MODE')     or define('APP_MODE',       'common'); // 应用模式 默认为普通模式   
    defined('STORAGE_TYPE') or define('STORAGE_TYPE',   'File'); // 存储类型 默认为File   
}


2. Application mode definition

In the ThinkPHP framework, except for the ThinkPHP framework entry and framework boot classes, basically all other functions can be changed and extended through the application mode. If we want to add an application mode, we only need to define a mode definition file under the ThinkPHPMode directory. That’s it, we can learn by analyzing the common mode.

//文件路径:ThinkPHP/Mode/common.php
/**
 * ThinkPHP 普通模式定义
 * 定义一个模式文件,只需要返回一个模式包含文件的数组即可
 * 在数组中主要包含4种扩展文件列表:
 *     config 为默认加载配置文件列表
 *     alias  为核心类库别名配置列表
 *     core 需要加载的核心函数和类文件列表
 *     tags 行为配置列表
 *
 * 如果在应用模式定义中加载一个自定类,那个自定义类的命名空间必须是Think
 */
return array(
    // 配置文件
    'config'    =>  array(
        THINK_PATH.'Conf/convention.php',   // 系统惯例配置
        CONF_PATH.'config.php',      // 应用公共配置
    ),
 
    // 别名定义
    'alias'     =>  array(
        'Think\Log'               => CORE_PATH . 'Log'.EXT,
        'Think\Log\Driver\File'   => CORE_PATH . 'Log/Driver/File'.EXT,
        'Think\Exception'         => CORE_PATH . 'Exception'.EXT,
        'Think\Model'             => CORE_PATH . 'Model'.EXT,
        'Think\Db'                => CORE_PATH . 'Db'.EXT,
        'Think\Template'          => CORE_PATH . 'Template'.EXT,
        'Think\Cache'             => CORE_PATH . 'Cache'.EXT,
        'Think\Cache\Driver\File' => CORE_PATH . 'Cache/Driver/File'.EXT,
        'Think\Storage'           => CORE_PATH . 'Storage'.EXT,
    ),
 
    // 函数和类文件
    'core'      =>  array(
        THINK_PATH.'Common/functions.php',
        COMMON_PATH.'Common/function.php',
        CORE_PATH . 'Hook'.EXT,
        CORE_PATH . 'App'.EXT,
        CORE_PATH . 'Dispatcher'.EXT,
        //CORE_PATH . 'Log'.EXT,
        CORE_PATH . 'Route'.EXT,
        CORE_PATH . 'Controller'.EXT,
        CORE_PATH . 'View'.EXT,
        BEHAVIOR_PATH . 'BuildLiteBehavior'.EXT,
        BEHAVIOR_PATH . 'ParseTemplateBehavior'.EXT,
        BEHAVIOR_PATH . 'ContentReplaceBehavior'.EXT,
    ),
    // 行为扩展定义
    'tags'  =>  array(
        'app_init'     =>  array(
            'Behavior\BuildLiteBehavior', // 生成运行Lite文件
        ),       
        'app_begin'     =>  array(
            'Behavior\ReadHtmlCacheBehavior', // 读取静态缓存
        ),
        'app_end'       =>  array(
            'Behavior\ShowPageTraceBehavior', // 页面Trace显示
        ),
        'view_parse'    =>  array(
            'Behavior\ParseTemplateBehavior', // 模板解析 支持PHP、内置模板引擎和第三方模板引擎
        ),
        'template_filter'=> array(
            'Behavior\ContentReplaceBehavior', // 模板输出替换
        ),
        'view_filter'   =>  array(
            'Behavior\WriteHtmlCacheBehavior', // 写入静态缓存
        ),
    ),
);


After we saw this common application mode code, we kind of understood what ThinkPHP's application mode extension was about, but we still knew it but didn't know why. How does defining a loaded file list and configuration change the core of the framework? The secret lies in the ThinkPHPK boot class, let’s review the following!

//判断是否存在core.php配置文件(这是开发环境临时定义的运行模式,我是这么理解的)
          //否者加载APP_MODE定义的模式文件
          $mode   =   include is_file(CONF_PATH.'core.php')?CONF_PATH.'core.php':MODE_PATH.APP_MODE.'.php';
          //加载模式中core定义的核心文件列表
          foreach ($mode['core'] as $file){
              if(is_file($file)) {
                include $file;
                if(!APP_DEBUG) $content   .= compile($file);
              }
          }
 
          //加载模式中定义的config配置文件列表
          foreach ($mode['config'] as $key=>$file){
              is_numeric($key)?C(include $file):C($key,include $file);
          }
 
          // 读取当前应用模式对应的配置文件
          if('common' != APP_MODE && is_file(CONF_PATH.'config_'.APP_MODE.'.php'))
              C(include CONF_PATH.'config_'.APP_MODE.'.php'); 
 
          // 加载模式中alias别名列表定义
          if(isset($mode['alias'])){
              self::addMap(is_array($mode['alias'])?$mode['alias']:include $mode['alias']);
          }
 
          // 加载应用别名定义文件
          if(is_file(CONF_PATH.'alias.php'))
              self::addMap(include CONF_PATH.'alias.php');
 
          // 加载模式中tags行为定义
          if(isset($mode['tags'])) {
              Hook::import(is_array($mode['tags'])?$mode['tags']:include $mode['tags']);
          }
 
          // 加载应用行为定义
          if(is_file(CONF_PATH.'tags.php'))
              // 允许应用增加开发模式配置定义
              Hook::import(include CONF_PATH.'tags.php');  
 
          // 加载框架底层语言包
          L(include THINK_PATH.'Lang/'.strtolower(C('DEFAULT_LANG')).'.php');


Through this code in ThinkPHP::start(), the meaning and implementation method of the pattern definition file are perfectly and seamlessly related.

3. Define a simple operating mode

There is an example of mode extension in the manual. You can take it here to analyze and define a lite simple running mode. First, create a new lite.php file in the ThinkPHP/Mode directory and define the content as follows:

return array(   
// 配置文件  
'config'    =>  array(
               THINK_PATH.'Conf/convention.php',     // 系统惯例配置
               CONF_PATH.'config.php',      // 应用公共配置  
 ),
 
 // 别名定义   
 'alias'     =>  array(
              'Think\Exception'         => CORE_PATH . 'Exception'.EXT,
              'Think\Model'             => CORE_PATH . 'Model'.EXT, 
              'Think\Db'                => CORE_PATH . 'Db'.EXT,
              'Think\Cache'             => CORE_PATH . 'Cache'.EXT,
              'Think\Cache\Driver\File' => CORE_PATH . 'Cache/Driver/File'.EXT,
              'Think\Storage'           => CORE_PATH . 'Storage'.EXT,
 ),
 
 // 函数和类文件  
  'core'      =>  array(
             MODE_PATH.'Lite/functions.php',  
             COMMON_PATH.'Common/function.php',
             MODE_PATH . 'Lite/App'.EXT, 
             MODE_PATH . 'Lite/Dispatcher'.EXT, 
             MODE_PATH . 'Lite/Controller'.EXT,
             MODE_PATH . 'Lite/View'.EXT,
             CORE_PATH . 'Behavior'.EXT,
 ),
 
 // 行为扩展定义   
 'tags'  =>  array(
              'view_parse'    =>  array(
                     'Behavior\ParseTemplate', // 模板解析 支持PHP、内置模板引擎和第三方模板引擎
               ),
               'template_filter'=> array(
                     'Behavior\ContentReplace', // 模板输出替换
               ),
 ),
);


From the above configuration, we found that most of the core files in the core have been replaced. Of course, these program functions that need to be replaced need to be implemented by ourselves. However, it is recommended that you directly copy the core files defined in the normal mode and modify them. . Next, we will implement the following core class library extension file App.class.php

in ThinkPHP application development

Create a Lite directory in the ThinkPHP/Mode directory and create the App.class.php file in the lite directory. The following is the implementation of the program file:

//模式扩展类库必须是Think命名空间
namespace Think;
 
/**
 * ThinkPHP 应用程序类 执行应用过程管理 Lite模式扩展类
 * 实现ThinkPHP核心类库扩展时,尽可能仿造原有类库实现(除非对ThinkPHP框架源码特别了解)
 * 因为在其他没有扩展的核心文件中可能会调用扩展的核心类文件中的某个方法,除非你打算全部扩展
 */
class App{
/**
 * 应用程序初始化
 * @access public
 * @return void
 */
static public function init() {
        //具体现实
}
 
/**
 * 执行应用程序
 * @access public
 * @return void
 */
static public function exec() {
        //具体实现
}
 
/**
 * 运行应用实例 入口文件使用的快捷方法
 * @access public
 * @return void
 */
static public function run() {
        //具体实现
}
 
static public function logo(){
        //具体实现
}
}

After all extension files of the file are implemented, the APP_MODE constant can be defined as lite in the framework entry file.

I am here to complain about the following official: In this section of the manual, the MODE_NAME constant must be defined to change the operating mode. For accuracy, I had no choice but to search for MODE_NAME in all the files of the ThinkPHP framework. I was relieved when the results were not found. This is the method of defining the operating mode in the previous version 3.1. The manual is updated with details (please state that the manual I am using now is version 3.2.1 on February 14, 2014).


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/845433.htmlTechArticleThe mystery of ThinkPHP application pattern extension ThinkPHP application pattern provides the opportunity to transform the core framework to make your application Adapt to more environments and different needs. Each application mode...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn