比如 lumen,ConsoleServiceProvider 里面的 register 做了下面的处理:

\Laravel\Lumen\Console\ConsoleServiceProvider::register

/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->registerCommands(array_merge(
$this->commands, $this->devCommands
));
} /**
* Register the given commands.
*
* @param array $commands
* @return void
*/
protected function registerCommands(array $commands)
{
foreach (array_keys($commands) as $command) {
call_user_func_array([$this, "register{$command}Command"], []);
} $this->commands(array_values($commands));
} /**
* Register the package's custom Artisan commands.
*
* @param array|mixed $commands
* @return void
*/
public function commands($commands)
{
$commands = is_array($commands) ? $commands : func_get_args(); Artisan::starting(function ($artisan) use ($commands) {
$artisan->resolveCommands($commands);
});
} // \Illuminate\Console\Application::resolveCommands /**
* Resolve an array of commands through the application.
*
* @param array|mixed $commands
* @return $this
*/
public function resolveCommands($commands)
{
$commands = is_array($commands) ? $commands : func_get_args(); foreach ($commands as $command) {
$this->resolve($command);
} return $this;
} /**
* Add a command, resolving through the application.
*
* @param string $command
* @return \Symfony\Component\Console\Command\Command
*/
public function resolve($command)
{
return $this->add($this->laravel->make($command));
}

  注意看上面的最后一个方法,这是所有命令 register 的时候都要做的一件事,也就是实例化,也就是说,在我们命令行任何的 handle 方法运行之前,就需要实例化所有的 Command 了。因为 Command 里面的 signature、description 都是实例属性,需要实例化才能获取,要不然我们运行 php artisan 的时候就获取不到命令列表了。

  因此,如果我们在 Command 的 __construct 里面做了一些影响全局的操作,有可能我们不会察觉得到,比如:

\Cache::setPrefix('abc');

  虽然你是在一个 Command 里面设置了 Cache 的 prefix,但实际上你在其他 Command 里面的缓存前缀也会是你设置的这个。

 

  简单地说,我们运行 php artisan 的时候,会把所有定义的 Command 实例化,如果我们需要做一些命令行初始化操作,最好放在 handle() 里面

laravel/lumen 的构造函数需要注意的地方的更多相关文章

  1. LaravelS - 基于Swoole加速Laravel/Lumen

    LaravelS LaravelS是一个胶水项目,用于快速集成Swoole到Laravel或Lumen,然后赋予它们更好的性能.更多可能性.Github 特性 内置Http/WebSocket服务器 ...

  2. 解决 Laravel/Lumen 出现 "Please provide a valid cache path" 问题

    解决 Laravel/Lumen 出现 "Please provide a valid cache path" 问题 解决 Laravel/Lumen 出现 "Pleas ...

  3. 移动构造函数应用最多的地方就是STL中(原文详解移动构造函数)

    移动构造函数应用最多的地方就是STL中 给出一个代码,大家自行验证使用move和不适用move的区别吧 #include <iostream> #include <cstring&g ...

  4. Laravel / Lumen 框架修改 创建时间 和 更新时间 对应字段

    为避免浪费时间--先上解决方案 在Model中重写 CREATED_AT 和 UPDATED_AT 两个类常量就可以了,这两个常量分别是创建时间和更新时间的字段名. ================= ...

  5. Laravel Lumen 数组操作

    php原生:http://www.w3school.com.cn/php/php_ref_array.asp Lumen方法:https://laravel.com/docs/5.6/helpers ...

  6. laravel/lumen 单元测试

    Testing Introduction Application Testing Interacting With Your Application Testing JSON APIs Session ...

  7. laravel Lumen邮箱发送配置

    Lumen 中配置邮件 https://blog.csdn.net/glovenone/article/details/54344013 Lareval 比 Lumen 多了一个步骤 https:// ...

  8. Laravel/Lumen 分组求和问题 where groupBy sum

    在Laravel中使用分组求和,如果直接使用Laravel各数据库操作方法,应该会得出来如下代码式: DB::table('table_a') ->where('a','=',1) ->g ...

  9. laravel(lumen)配置读写分离后,强制读主(写)库数据库,解决主从延迟问题

    在Model里面加上下面这句,强制读主(写)库数据库,解决主从延迟问题. public static function boot() { //清空从连接,会自动使用主连接 DB::connection ...

随机推荐

  1. [LeetCode] 307. Range Sum Query - Mutable 解题思路

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  2. 第十次Scrum meeting

    第十次Scrum  meeting 任务及完成度: 成员 1.2 1.3 陈谋 任务1040:完成stackoverflow的数据处理后的json处理(100%) 任务1114-2:完成对pdf.pp ...

  3. 《Linux内核设计与实现》读书笔记 3

    第三章 进程管理 3.1进程 概念: 进程:处于执行期的程序.但不仅局限于程序,还包含其他资源(打开的文件,挂起的信号,内核内部数据,处理器状态,一个或多个具有内催音社的内存地址空间及一个或多个执行线 ...

  4. 过滤器Filter的使用(以登录为例子)

    使用过滤器步骤: (1)在web.xml文件中添加过滤器(以下例子是过滤多个请求) <!-- 用户登录过滤 --> <filter> <filter-name>lo ...

  5. 用户模拟+spec

    用户模拟:用户模拟的对象分别为小学二年级学生,学生父亲以及小学教师. 名字 孔小颖 性别 男 职业 小学生 收入 来自父母给予 知识层次和能力 小学二年级 生活/工作情况 在学校上课,数学成绩较差,放 ...

  6. vue element-ui upload 实现带token上传

    <el-upload class="upload-demo" :data ="uploadData" :headers = "headers&q ...

  7. Delphi的idhttp报IOHandler value is not valid错误的原因[转]

    出现这种问题的原因是由于访问的 URL地址为https或存在其跳转地址为https. 首先单纯使用idhttp是只能访问http,而https则需要搭配IdSSLIOHandlerSocketOpen ...

  8. [转载]Linux目录说明

    原作者博客: http://blog.51cto.com/yangrong/1288072 将文字部分转移到自己的目录下便于学习记录 感谢~ 2./目录 目录 描述 / 第一层次结构的根.整个文件系统 ...

  9. datatime

    /把一个日期字符串如“2007-2-28 10:18:30”转换为Date对象 var   strArray=str.split("   "); var   strDate=str ...

  10. Highcharts之折线图

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...