本文實(shí)例講述了Laravel 5.1 on SAE環(huán)境開發(fā)方法。分享給大家供大家參考,具體如下:
Laravel-簡(jiǎn)潔、優(yōu)雅的PHP開發(fā)框架,為 WEB 藝術(shù)家創(chuàng)造的 PHP 框架,如今正式移植到SAE環(huán)境。
由于Laravel 5.1相比于Laravel 4有很多的改動(dòng),不僅以目錄結(jié)構(gòu)更加清晰,而且功能也更豐富。但是Laravel官方還是沒有原生支持SAE環(huán)境(估計(jì)永遠(yuǎn)不會(huì)支持),所以我就做了一個(gè)移植版本,可以很優(yōu)雅的切換本地和SAE環(huán)境。
由于SAE的特殊性,那么這幾個(gè)核心問題就必須要解決
#1 putenv()函數(shù)禁用
#2 模板編譯
#3 緩存類
#4 日志處理
#5 Session類
#6 服務(wù)提供者緩存
#1 putenv()函數(shù)禁用
Laravel 5.1使用了這個(gè)putenv()函數(shù)來向當(dāng)前的環(huán)境中動(dòng)態(tài)添加變量,但是很遺憾的是SAE的PHPRuntime禁用了該函數(shù),所以只能使用折中的方法來實(shí)現(xiàn)。當(dāng)初本來想Hook掉該實(shí)現(xiàn),后來覺得沒必要,這個(gè)函數(shù)在Laravel 5.1中主要是為了使用.env配置文件來統(tǒng)一團(tuán)隊(duì)的配置。所以我是直接禁用了該功能,在vendor/vlucas/phpdotenv/src/Dotenv.php的86行左右,直接注釋掉該函數(shù),然后把所有的配置信息都寫到config文件夾的相應(yīng)配置文件中。雖然解決了該函數(shù)被禁用的問題,但是實(shí)現(xiàn)的不夠優(yōu)雅,希望有大神可以給出更加優(yōu)雅的實(shí)現(xiàn)。
#2 模板編譯
該問題主要還是因?yàn)镾AE的本地環(huán)境寫入被禁止,所以我使用了Wrapper來把編譯后的模板文件寫入到Storage。本來是打算寫到KVDB中,但是會(huì)出現(xiàn)一些奇奇怪怪問題,原因不明。
在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;
注意要在相應(yīng)的Storage中建立compiled文件夾。
#3 緩存類
Laravel 5.1沒有直接提供SAE可用的Memcache緩存驅(qū)動(dòng),這個(gè)解決比較簡(jiǎn)單,直接寫一個(gè)服務(wù)提供者注冊(cè)到app.php即可,然后在config\cache.php中注冊(cè),具體實(shí)現(xiàn)看項(xiàng)目源碼
#4 日志處理
這也是一個(gè)比較棘手的問題,由于Laravel 5.1的日志處理已經(jīng)不是和4一樣使用服務(wù)提供者,而且直接注入到啟動(dòng)器中,這就使得我們只能覆寫原生ConfigureLogging啟動(dòng)類,而官方也沒有給出如何覆寫和在哪里覆寫,所以我這邊的解決方案是判斷當(dāng)前環(huán)境為SAE后直接重寫Http內(nèi)核中的一個(gè)啟動(dòng)器屬性,核心代碼:
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', ]; }
這樣還不行,還必須重寫日志的部分實(shí)現(xiàn)
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類
Laravel5.1的session依舊是本地寫的問題,參考了Laravel4的移植,使用了memcache作為session的實(shí)現(xiàn),具體可以結(jié)合緩存部分來處理
#6 服務(wù)提供者緩存
在應(yīng)用程序的啟動(dòng)過程中,laravel會(huì)在bootstrap/cache/services.json生成服務(wù)提供者的緩存,為了加快下次訪問的速度,依舊是本地寫的問題,解決方案很簡(jiǎn)單,使用Storage的Wrapper即可
以上這些問題解決后,差不多就算成功了。最后修改下bootstrap\app.php來實(shí)現(xiàn)本地與SAE環(huán)境的優(yōu)雅切換,主要是判斷環(huán)境然后生成SAE專有應(yīng)用實(shí)例和注入相應(yīng)的Http內(nèi)核。
/* |-------------------------------------------------------------------------- | 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環(huán)境,原因很明確了,就是要注入相應(yīng)的應(yīng)用程序?qū)嵗虷ttp實(shí)例,然后再這里也定義一下Storage
然后就是config\app.php的相關(guān)配置,根據(jù)環(huán)境判斷來注入相應(yīng)的服務(wù)提供者
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專有應(yīng)用程序?qū)嵗虷ttp實(shí)例與原生的差別,主要還是本地寫的問題。原生的會(huì)在應(yīng)用程序啟動(dòng)時(shí)候生成路由、配置、服務(wù)提供者、模板編譯的相關(guān)文件,以此來提升加載速度。但是到了SAE就不行了,所以重寫了Application類的部分與路徑相關(guān)的方法,來把這些文件生成到Storage中,而Http專有內(nèi)核則是處理啟動(dòng)器中的日志類。具體代碼就不貼出來,可以看看項(xiàng)目。
再給一個(gè)SAE可以使用的rewrite
handle: - rewrite: if (path ~ "^/$") goto "public/index.php" - rewrite: if(!is_dir() && !is_file() && path~"^(.*)$") goto "public/index.php/$1"
總結(jié)
整個(gè)移植過程還算是很順利,得益于Laravel的拓展性與SAE的便利.不過在對(duì)于putenv()函數(shù)和日志處理的解決方法上,還是實(shí)現(xiàn)的不夠優(yōu)雅,希望能有人給出更有優(yōu)雅的實(shí)現(xiàn)方案。然后其他的SAE服務(wù)比如分詞、郵件、隊(duì)列等,則可以使用服務(wù)提供者自動(dòng)加載,這個(gè)就不多說了。
希望本文所述對(duì)大家基于Laravel框架的PHP程序設(shè)計(jì)有所幫助。
更多Laravel 5.1 on SAE環(huán)境開發(fā)教程相關(guān)文章請(qǐng)關(guān)注PHP中文網(wǎng)!

ホットAIツール

Undress AI Tool
脫衣畫像を無料で

Undresser.AI Undress
リアルなヌード寫真を作成する AI 搭載アプリ

AI Clothes Remover
寫真から衣服を削除するオンライン AI ツール。

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡(jiǎn)単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中國(guó)語(yǔ)版
中國(guó)語(yǔ)版、とても使いやすい

ゼンドスタジオ 13.0.1
強(qiáng)力な PHP 統(tǒng)合開発環(huán)境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)