因爲某種緣由,公司總體框架由python的flask框架,轉換爲php的laravel。在斷斷續續幾個月的時間裏,邊繼續寫着flask框架,邊學着laravel。說下本身如今的狀態吧。前段時間差很少都在個1-2點睡覺,大概四月份有段時間居然到了3-4點才睡的地步。php
路漫漫其修遠兮,總感受時間不夠用的。大概是本身以前浪費的時間太多了,是時候還上了。python
laravel文檔中文版的,大概看到過三個。隨便找個看看就能夠了。http://laravel-china.org/docs/5.1 laravel
# 輸入 php artisan 便可看到所有可用命令 down Put the application into maintenance mode up Bring the application out of maintenance mode
總體流程大致(由於詳細的我也不是很清楚╮(╯_╰)╭)說下吧。flask
// config/app.php 中。 'providers' => [ // 這個即是 laravel自帶的 artisan 命令提供者 Illuminate\Foundation\Providers\ArtisanServiceProvider::class, ]
/** * Register the command. * * @return void */ protected function registerUpCommand() { $this->app->singleton('command.up', function () { return new UpCommand; }); } /** * Register the command. * * @return void */ protected function registerDownCommand() { $this->app->singleton('command.down', function () { return new DownCommand; }); }
class DownCommand extends Command { /** * The console command name. * * @var string */ protected $name = 'down'; /** * The console command description. * * @var string */ protected $description = 'Put the application into maintenance mode'; /** * Execute the console command. * * @return void */ public function fire() { // 關鍵點: 在當前存儲目錄/framework 下面建立一個 down文件 touch($this->laravel->storagePath().'/framework/down'); $this->comment('Application is now in maintenance mode.'); } } // touch() 函數php文檔解釋 /** * Sets access and modification time of file * @link http://php.net/manual/en/function.touch.php * @param string $filename <p> * The name of the file being touched. * </p> * @param int $time [optional] <p> * The touch time. If time is not supplied, * the current system time is used. * </p> * @param int $atime [optional] <p> * If present, the access time of the given filename is set to * the value of atime. Otherwise, it is set to * time. * </p> * @return bool true on success or false on failure. * @since 4.0 * @since 5.0 */ function touch ($filename, $time = null, $atime = null) {}
class UpCommand extends Command { /** * The console command name. * * @var string */ protected $name = 'up'; /** * The console command description. * * @var string */ protected $description = 'Bring the application out of maintenance mode'; /** * Execute the console command. * * @return void */ public function fire() { // 關鍵:刪除laravel存儲目錄/fromework 下面的 down 文件 @unlink($this->laravel->storagePath().'/framework/down'); $this->info('Application is now live.'); } } // @unlink() php文檔解釋 /** * Deletes a file * @link http://php.net/manual/en/function.unlink.php * @param string $filename <p> * Path to the file. * </p> * @param resource $context [optional] ¬e.context-support; * @return bool true on success or false on failure. * @since 4.0 * @since 5.0 */ function unlink ($filename, $context = null) {}
// Http/Kernel.php 文件裏面 class Kernel extends HttpKernel { /** * The application's global HTTP middleware stack. * * @var array */ protected $middleware = [ // 就是這個東西了 \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, ]; }
class CheckForMaintenanceMode { /** * The application implementation. * * @var \Illuminate\Contracts\Foundation\Application */ protected $app; /** * Create a new middleware instance. * * @param \Illuminate\Contracts\Foundation\Application $app * @return void */ public function __construct(Application $app) { $this->app = $app; } /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed * * @throws \Symfony\Component\HttpKernel\Exception\HttpException */ public function handle($request, Closure $next) { // 當這個條件成立時,直接拋出 HttpException(503) 異常。 // 默認狀況下,該請求會直接顯示 resources/views/errors/503.blade.php 頁面 if ($this->app->isDownForMaintenance()) { throw new HttpException(503); } return $next($request); } } // 再看 isDownForMaintenance() 函數 /** * Determine if the application is currently down for maintenance. * * @return bool */ public function isDownForMaintenance() { // 重點:判斷一下 laravel的storagePath/framework 下面是否存在 down 文件 return file_exists($this->storagePath().'/framework/down'); }
其實呢,這些只是一個拋磚引玉的過程。只是拿框架的一個小東西來扯扯而已。仍是那句話:路漫漫其修遠兮。加油吧,少年~bash
1. php artisan down => 在storagePath/framework 下面建立 down文件; php artisan up => 刪除 down 建立 down文件app
2. laravel 默認中間件,檢查storagePath/framework 下面是否存在down文件,若存在則拋出503異常框架