框架类程序如何适配?

常见的有以下几种:

ThinkPHP 示例

// 检测PHP环境
if (version_compare(PHP_VERSION, '5.6.0', '<')) {
    die('require PHP > 5.6.0 !');
}
 
// 定义应用目录
define('APP_PATH', __DIR__ . '/../application/');
 
// 检测是否定义了入口文件的位置
if (!defined('ENTRANCE')) {
    define('ENTRANCE', 'index.php');
}
 
// 加载框架引导文件
require __DIR__ . '/../thinkphp/start.php';

Laravel 示例

    define('LARAVEL_START', microtime(true));
    //注册自动加载,引入了Composer提供的依赖注入,无需手动加载任何类(加载项目依赖)
    require __DIR__.'/../vendor/autoload.php';
    //初始化应用,可以理解为加载框架,准备好应用的环境以及配置,为启动应用准备
    $app = require_once __DIR__.'/../bootstrap/app.php';
    
    //运行应用,从容器中解析kernel对象,初始化,实际解析对象App\Http\Kernel
    $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
    
    //捕获请求,处理请求并返回响应结果
    $response = $kernel->handle(
        $request = Illuminate\Http\Request::capture()
    );
    //发送响应请求
    $response->send();
    //终止Kernel
    $kernel->terminate($request, $response);

Yii 示例

// 定义 debug 的标记
defined('YII_DEBUG'or define('YII_DEBUG', true);
// 定义环境,有 'dev' 和 'prod' 两种
defined('YII_ENV'or define('YII_ENV', 'dev');

// 引入 vendor 中的 autoload.php 文件,会基于 composer 的机制自动加载类
require(__DIR__ . '/../vendor/autoload.php');
// 引入 Yii 框架的文件 Yii.php
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');

// 引入 web 的 config 文件,并将返回值即配置项放入 $config 变量中
$config = require(__DIR__ . '/../config/web.php');

// new 一个 yii\web\Application 的实例,并执行它的 run 方法
// 用 $config 作为 yii\web\Application 初始化的参数
(new yii\web\Application($config))->run();

CakePHP 示例

// 定义应用程序的目录为当前目录
define('APP_DIR', 'App');
define('WEBROOT_DIR', 'webroot');
define('CAKE_CORE_INCLUDE_PATH', ROOT . DS . 'vendor' . DS . 'cakephp' . DS . 'cakephp');
 
// 确保Debug模式开启
define('CONFIG', 'Debug');
 
// 注册自动加载类和函数
require ROOT . '/vendor/autoload.php';
 
// 引入CakePHP的自动加载类
require CORE_PATH . '/config/bootstrap.php';
 
// 引入应用程序的bootstrap文件
require APP_DIR . '/config/bootstrap.php';
 
// 创建并运行应用程序
$cache = null;
if (function_exists('apc_store')) {
    $cache = apc_store(__FILE__, $cache);
}
if (!$cache) {
    $kernel = new Kernel();
    $kernel->load(ROOT . DS . 'config' . DS . 'routes.php');
    $response = $kernel->handle(
        $kernel->getRequest()
    );
    $response->send();
    $kernel->terminate($kernel->getRequest(), $response);
}

Symfony 示例

use  Symfony\Component\HttpFoundation\Request;   // 使用Request命名空间
 
$loader  = require_once  __DIR__. '/../app/bootstrap.php.cache' ;   // bootstrap的自加载文件,包括autoload等
 
require_once  __DIR__. '/../app/AppKernel.php' ;   //bundle的加载
 
$kernel  = new  AppKernel( 'yjf' , true);   // 核心类AppKernel
 
$kernel ->loadClassCache();   // 加载classCache
 
$request  = Request::createFromGlobals();  // 获取$_REQUEST
 
$response  = $kernel ->handle( $request );   // 处理请求,将request转化为response
 
$response ->send();   // 发送response
 
$kernel ->terminate( $request , $response );  // response的后续操作

CodeIgniter 示例

//定义程序运行环境,可选项:development、testing、production
define('ENVIRONMENT', 'development');
 
//根据程序运行环境,设置错误报告级别
if (defined('ENVIRONMENT'))
{
	switch (ENVIRONMENT)
	{
		case 'development':
                        //开发环境,报告全部错误
			error_reporting(E_ALL);
		break;
	
		case 'testing':
		case 'production':
                        //测试、产品,不报告错误
			error_reporting(0);
		break;
 
		default:
			exit('The application environment is not set correctly.');
	}
}
 
//系统文件夹路径
$system_path = 'system';
 
//应用程序文件夹路径
$application_folder = 'application';
 
 
//当一CLI方式运行程序时,设置当前目录
if (defined('STDIN'))
{
	chdir(dirname(__FILE__));
}
 
if (realpath($system_path) !== FALSE)
{
	$system_path = realpath($system_path).'/';
}
 
//确保路径后有'/'
$system_path = rtrim($system_path, '/').'/';
 
//确保系统路径正确
if ( ! is_dir($system_path))
{
	exit("Your system folder path does not appear to be set correctly. Please open the following file and correct this: ".pathinfo(__FILE__, PATHINFO_BASENAME));
}
 
//定义系统路径常量
 
//当前文件的名称,即index.php,即入口文件名称
define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME));
 
//php文件后缀
//此全局常量已经被弃用
define('EXT', '.php');
 
//系统文件夹路径
define('BASEPATH', str_replace("\\", "/", $system_path));
 
//当前文件的路径
define('FCPATH', str_replace(SELF, '', __FILE__));
 
//系统文件夹名称
define('SYSDIR', trim(strrchr(trim(BASEPATH, '/'), '/'), '/'));
 
 
//应用程序文件夹路径
if (is_dir($application_folder))
{
	define('APPPATH', $application_folder.'/');
}
else
{
	if ( ! is_dir(BASEPATH.$application_folder.'/'))
	{
		exit("Your application folder path does not appear to be set correctly. Please open the following file and correct this: ".SELF);
	}
 
	define('APPPATH', BASEPATH.$application_folder.'/');
}
 
//启动程序
require_once BASEPATH.'core/CodeIgniter.php';
 
 

CanPHP 示例

// 定义应用的根目录
define('APP_PATH', __DIR__);
 
// 自动加载类文件
function auto_load_class($class) {
    $class_file = APP_PATH . '/' . str_replace('\\', '/', $class) . '.php';
    if (file_exists($class_file)) {
        require $class_file;
    }
}
 
// 注册自动加载器
spl_autoload_register('auto_load_class');
 
// 启动应用
$app = new App\Core\Application(APP_PATH);
$app->run();

Akelos 示例

// 设置Akelos框架的根目录
define('AKELOS_DIR', realpath(dirname(__FILE__) . '/../akelos'));
 
// 设置应用的根目录
define('APP_DIR', realpath(dirname(__FILE__) . '/..'));
 
// 包含Akelos框架的引导文件
require_once AKELOS_DIR . '/akelos_bootstrap.php';
 
// 启动Akelos应用
Akelos::start();
 
?>

Zend Framework 示例

// 定义应用目录
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
 
// 设置时区
date_default_timezone_set('Asia/Shanghai');
 
// 引入Zend Framework的自动加载类Autoloader
require_once 'Autoload/Loader.php';
 
// 创建自动加载器实例
$loader = new Zend_Application_Autoload_Loader();
 
// 加载应用配置文件
$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);
 
// 运行应用
$application->run();

框架类自研程序,根据框架版本、入口、伪静态规则不同,需要分析在做兼容适配。


评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

zh_CN简体中文