因为某种缘由,公司总体框架由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异常框架