当前位置: 首页 > 编程文章 >

Laravel 5.1 在 SAE 环境下的移植与适配教程

Laravel 5.1 在 SAE 环境下的移植与适配教程

作者:沈知时间:2025-08-19 16:00:34浏览:651

本文介绍将 Laravel 5.1 移植到新浪云 SAE 环境的具体方法。针对 SAE 禁用 putenv()、禁止本地文件写入等限制,通过修改配置文件、重写内核类及使用 Storage Wrapper 解决模板编译、缓存、日志和 Session 等问题,实现本地与 SAE 环境的优雅切换。

本文目录展开

Laravel 5.1 SAE环境移植概述

本实例详细解析了 Laravel 5.1 在 SAE 环境中的开发实践,旨在为开发者提供参考。

Laravel 是一款简洁优雅的 PHP Web 开发框架,现已成功适配至 SAE 平台。

相较于 Laravel 4,版本 5.1 目录结构更清晰且功能增强。尽管官方未提供原生 SAE 支持,但通过定制移植版可实现本地与 SAE 环境的无缝切换。

鉴于 SAE 平台的特殊运行机制,需重点解决以下核心兼容性问题。

主要挑战包括:#1 putenv()函数被禁用;#2 模板编译路径限制;#3 缓存驱动适配;#4 日志处理机制变更;#5 Session 存储方式;#6 服务提供者缓存生成。

#1 putenv()函数禁用

Laravel 5.1 依赖 putenv() 动态加载环境变量以读取 .env 配置。因 SAE PHPRuntime 禁用了该函数,此处采取折中方案:注释掉 vendor/vlucas/phpdotenv/src/Dotenv.php 第 86 行左右的代码,并将所有配置项迁移至 config 文件夹下的对应文件中。虽解决了禁用问题,但实现略显粗糙,期待更优雅的替代方案。

#2 模板编译

由于 SAE 禁止本地文件写入,原生的模板编译机制失效。解决方案是利用 Wrapper 将编译后的视图文件写入 Storage 存储桶。此前尝试写入 KVDB 但因未知错误放弃。

需在 config\view.php 中进行如下配置调整:


$compiled = [
 'paths' => [
 realpath(base_path('resources/views')),
 ],
 'compiled' => realpath(storage_path('framework/views')),
];
if(SAE){
 $compiled['compiled'] = 'saestor://'.SAE_STORAGE.'/compiled';
}
return $compiled;

注意确保目标 Storage 中存在 compiled 文件夹。

#3 缓存类

Laravel 5.1 默认未包含 SAE 可用的 Memcache 驱动。解决方法是编写自定义服务提供者并在 app.php 中注册,同时在 config\cache.php 中关联,具体逻辑见项目源码。

#4 日志处理

这也是一个比较棘手的问题,由于Laravel 5.1的日志处理已经不是和4一样使用服务提供者,而且直接注入到启动器中,这就使得我们只能覆写原生ConfigureLogging启动类,而官方也没有给出如何覆写和在哪里覆写,所以我这边的解决方案是判断当前环境为SAE后直接重写Http内核中的一个启动器属性,核心代码:


namespace Illuminate\Cloud\SAE;
use App\Http\Kernel as DefaultKernel;
class Kernel extends DefaultKernel{
 /**
 * The bootstrap classes for the application.
 *
 * @var array
 */
 protected $bootstrappers = [
 'Illuminate\Foundation\Bootstrap\DetectEnvironment',
 'Illuminate\Foundation\Bootstrap\LoadConfiguration',
 'Illuminate\Cloud\SAE\Log\ConfigureLogging',
 'Illuminate\Foundation\Bootstrap\HandleExceptions',
 'Illuminate\Foundation\Bootstrap\RegisterFacades',
 'Illuminate\Foundation\Bootstrap\RegisterProviders',
 'Illuminate\Foundation\Bootstrap\BootProviders',
 ];
}

此外,还需对日志的部分底层实现进行重写。


class Writer extends IlluminateLogWriter {
 protected function useSaeLog($level = 'debug'){
 $level = $this->parseLevel($level);
 $this->monolog->pushHandler($handler = new SaeLogHandler($level));
 $handler->setFormatter($this->getDefaultFormatter());
 }
 public function useFiles($path, $level = 'debug'){
 if (SAE) {
 return $this->useSaeLog($level);
 }
 parent::useFiles($path, $level);
 }
 public function useDailyFiles($path, $days = 0, $level = 'debug'){
 if (SAE) {
 return $this->useSaeLog($level);
 }
 parent::useDailyFiles($path, $days, $level);
 }
}

#5 Session类

Laravel 5.1 的 Session 同样面临本地写入限制。参考 Laravel 4 的移植经验,改用 Memcache 作为 Session 存储后端,具体实现可结合前述缓存部分的处理方式。

#6 服务提供者缓存

应用启动时,Laravel 会在 bootstrap/cache/services.json 生成服务提供者缓存以提升性能。为解决本地写入问题,同样采用 Storage 的 Wrapper 机制。上述问题解决后,基本完成适配。最后需修改 bootstrap\app.php 以实现本地与 SAE 环境的智能切换,即根据环境判断生成 SAE 专用应用实例并注入对应的 Http 内核。


/*
|--------------------------------------------------------------------------
| Create The Application
|--------------------------------------------------------------------------
|
| The first thing we will do is create a new Laravel application instance
| which serves as the "glue" for all the components of Laravel, and is
| the IoC container for the system binding all of the various parts.
|
*/
define('SAE',true);
define('SAE_STORAGE', 'laravel');
if(SAE){
 $app = new Illuminate\Cloud\SAE\Application(
 realpath(__DIR__.'/../')
 );
 $app->singleton(
 Illuminate\Contracts\Http\Kernel::class,
 Illuminate\Cloud\SAE\Kernel::class
 );
}else{
 $app = new Illuminate\Foundation\Application(
 realpath(__DIR__.'/../')
 ); 
 $app->singleton(
 Illuminate\Contracts\Http\Kernel::class,
 App\Http\Kernel::class
 );
}
/*
|--------------------------------------------------------------------------
| Bind Important Interfaces
|--------------------------------------------------------------------------
|
| Next, we need to bind some important interfaces into the container so
| we will be able to resolve them when needed. The kernels serve the
| incoming requests to this application from both the web and CLI.
|
*/
$app->singleton(
 Illuminate\Contracts\Console\Kernel::class,
 App\Console\Kernel::class
);
$app->singleton(
 Illuminate\Contracts\Debug\ExceptionHandler::class,
 App\Exceptions\Handler::class
);
/*
|--------------------------------------------------------------------------
| Return The Application
|--------------------------------------------------------------------------
|
| This script returns the application instance. The instance is given to
| the calling script so we can separate the building of the instances
| from the actual running of the application and sending responses.
|
*/
return $app;

在 bootstrap\app.php 中定义 SAE 环境标识的原因在于:需要在此处注入特定的应用程序实例和 Http 实例,同时初始化 Storage 配置。

接着配置 config\app.php,根据环境判断结果动态注入相应的服务提供者。


if(SAE){
 $removeProviders = [
 Illuminate\Cache\CacheServiceProvider::class,
 Illuminate\Session\SessionServiceProvider::class,
 ]; 
 for($i = 0; $i < count($app['providers']); $i++){
 if (in_array($app['providers'][$i], $removeProviders)) {
 unset($app['providers'][$i]);
 }
 }
 $app['providers'] = array_merge($app['providers'],[
 Illuminate\Cloud\SAE\Cache\SaeCacheServiceProvider::class,
 Illuminate\Cloud\SAE\Session\SessionServiceProvider::class,
 Illuminate\Cloud\SAE\Storage\StorageServiceProvider::class,
 Illuminate\Cloud\SAE\Segment\SegmentServiceProvider::class,
 ]);
 $app['aliases']['Storage'] = Illuminate\Cloud\SAE\Storage\Storage::class;
 $app['aliases']['Segment'] = Illuminate\Cloud\SAE\Segment\Segment::class;
}

SAE 专有应用实例和 Http 实例与原生的主要区别在于处理本地写入限制。原生应用在启动时会生成路由、配置、服务提供者及模板编译文件以加速加载,而在 SAE 中需重写 Application 类中与路径相关的方法,将这些文件生成至 Storage。Http 专有内核则负责处理启动器中的日志类。具体代码详见项目仓库。

此外,提供一个适用于 SAE 的 rewrite 规则供参考。


handle:
 - rewrite: if (path ~ "^/$") goto "public/index.php"
 - rewrite: if(!is_dir() && !is_file() && path~"^(.*)$") goto "public/index.php/$1"

总结

得益于 Laravel 的高扩展性和 SAE 的便利性,整体移植过程较为顺利。但在 putenv() 禁用和日志处理方面,现有实现仍不够优雅,欢迎提出改进建议。其他 SAE 服务如分词、邮件、队列等可通过服务提供者自动加载,此处不再赘述。

项目 GitHub 地址: https://github.com/wh469012917/laravel5-on-sae

软件点击此处本站下载。

希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。

上一篇
Yii控制器中filter过滤器用法分析

在Yii框架的控制器中,可以通过重写filters()方法来定义和注册过滤器。过滤器方法需以filter为前缀,并在filters()中通过动作名或组合规则进行绑定。例如,使用projectContext +create index admin可指定特定动作应用自定义逻辑。过滤器常用于权限控制、数据加载等前置处理场景。

下一篇
Zend Framework 注册表的使用方式

注册表通过约定的键存取应用共享的对象或配置。使用时可以先建立明确的登记和读取顺序,并检查缺少键时的行为;将键名集中管理,有助于避免模块之间无意覆盖同一个对象。

相关文章

更多
PHP基于单例模式的日志处理类库实现

本文介绍了一个基于单例模式实现的PHP日志处理类,旨在节省资源并规范日志记录。该类支持自动判断文件大小,当超出指定阈值时按序创建新文件(如log_0、log_1)。文章提供了完整的类源码,包括构造函数、实例获取及日志写入逻辑,并展示了如何调用该类进行事件或错误记录的示例代码。

2025.02.19 127

专题推荐

更多
PHP 类与对象定义及继承

先看类声明、对象实例与属性访问。理解对象的创建和销毁后,再读继承、方法重载和关键字对照。已有代码遇到继承行为差异时,可进一步查看 final 方法与反射实例。

PHP 定时任务配置与运行

先确定任务运行间隔以及是否允许重叠。固定时间触发先看 Cron 案例,框架内和常驻服务再读对应示例。

Yii2 队列配置与常驻运行

先用一个最小任务跑通入队和消费,再检查 Web 与 Console 是否加载了相同的队列配置。确认命令能手动运行后再配置 Supervisor,最后验证进程重启时的处理情况。

PHP Session 过期时间设置方法

浏览器关闭、服务端回收和业务规定的闲置超时,可能在不同时间发生。对照配置项与应用层时间戳的做法,检查会话何时仍可读、何时应要求重新登录。

PHP URL 编码和解码方法

URL 路径片段和查询参数对空格的处理方式不同。先比较 urlencode 与 rawurlencode,再沿解码过程核对中文、加号和百分号,检查地址是否被重复编码或提前解码。

在线工具箱

更多
键码查询
键码查询

按下键盘按键时查看浏览器记录的键名和事件信息。

.env 配置检查
.env 配置检查

本地检查 .env 键值语法、引号、重复键及空值,预览解析结果,辅助排查 PHP 和 Laravel 环境配置。

进制转换
进制转换

转换二进制、十进制、十六进制等数值表示,辅助程序调试与进制学习。

HTML 标签安全清理
HTML 标签安全清理

清理 HTML 片段中的标签或危险内容,辅助整理待展示的文本。

文件 Base64 转换
文件 Base64 转换

把文件转为 Base64 文本,也可将编码内容还原为文件,方便接口传输与调试。