Laravel框架学习 -- php artisan down/up

    由于某种原因,公司整体框架由python的flask框架,转换为php的laravel。在断断续续几个月的时间里,边继续写着flask框架,边学着laravel。说下自己现在的状态吧。前段时间差不多都在个1-2点睡觉,大概四月份有段时间竟然到了3-4点才睡的地步。

    路漫漫其修远兮,总感觉时间不够用的。大概是自己之前浪费的时间太多了,是时候还上了。


    laravel文档中文版的,大概看到过三个。随便找个看看就可以了。http://laravel-china.org/docs/5.1 

# 输入 php artisan 即可看到全部可用命令

  down                 Put the application into maintenance mode
  up                   Bring the application out of maintenance mode

    整体流程大体(因为详细的我也不是很清楚╮(╯_╰)╭)说下吧。

一、命令的实现

    1. 作为服务提供者,加载到程序中。
// config/app.php 中。
'providers' => [
    // 这个便是 laravel自带的 artisan 命令提供者
    Illuminate\Foundation\Providers\ArtisanServiceProvider::class,
]
    2.然后找到 Up/Down 命令入口
/**
 * 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;
    });
}
    3.1 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) {}
    3.2 UpCommand 实现
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] &note.context-support;
 * @return bool true on success or false on failure.
 * @since 4.0
 * @since 5.0
 */
function unlink ($filename, $context = null) {}

 

二、如何工作的

    1. 当然是使用中间件了
// Http/Kernel.php 文件里面
class Kernel extends HttpKernel
{
    /**
     * The application's global HTTP middleware stack.
     *
     * @var array
     */
    protected $middleware = [
        
        // 就是这个东西了
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    ];
}
    2. 继续看中间件的实现
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');
}


总结:

其实呢,这些只是一个抛砖引玉的过程。只是拿框架的一个小东西来扯扯而已。还是那句话:路漫漫其修远兮。加油吧,少年~

1.  php artisan down => 在storagePath/framework 下面创建 down文件; php artisan up => 删除 down 创建 down文件

2.  laravel 默认中间件,检查storagePath/framework 下面是否存在down文件,若存在则抛出503异常

转载于:https://my.oschina.net/lpe234/blog/689059

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值