laravel/lumen 的构造函数需要注意的地方
比如 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 的构造函数需要注意的地方的更多相关文章
- LaravelS - 基于Swoole加速Laravel/Lumen
LaravelS LaravelS是一个胶水项目,用于快速集成Swoole到Laravel或Lumen,然后赋予它们更好的性能.更多可能性.Github 特性 内置Http/WebSocket服务器 ...
- 解决 Laravel/Lumen 出现 "Please provide a valid cache path" 问题
解决 Laravel/Lumen 出现 "Please provide a valid cache path" 问题 解决 Laravel/Lumen 出现 "Pleas ...
- 移动构造函数应用最多的地方就是STL中(原文详解移动构造函数)
移动构造函数应用最多的地方就是STL中 给出一个代码,大家自行验证使用move和不适用move的区别吧 #include <iostream> #include <cstring&g ...
- Laravel / Lumen 框架修改 创建时间 和 更新时间 对应字段
为避免浪费时间--先上解决方案 在Model中重写 CREATED_AT 和 UPDATED_AT 两个类常量就可以了,这两个常量分别是创建时间和更新时间的字段名. ================= ...
- Laravel Lumen 数组操作
php原生:http://www.w3school.com.cn/php/php_ref_array.asp Lumen方法:https://laravel.com/docs/5.6/helpers ...
- laravel/lumen 单元测试
Testing Introduction Application Testing Interacting With Your Application Testing JSON APIs Session ...
- laravel Lumen邮箱发送配置
Lumen 中配置邮件 https://blog.csdn.net/glovenone/article/details/54344013 Lareval 比 Lumen 多了一个步骤 https:// ...
- Laravel/Lumen 分组求和问题 where groupBy sum
在Laravel中使用分组求和,如果直接使用Laravel各数据库操作方法,应该会得出来如下代码式: DB::table('table_a') ->where('a','=',1) ->g ...
- laravel(lumen)配置读写分离后,强制读主(写)库数据库,解决主从延迟问题
在Model里面加上下面这句,强制读主(写)库数据库,解决主从延迟问题. public static function boot() { //清空从连接,会自动使用主连接 DB::connection ...
随机推荐
- C_数据结构_循环实现求阶乘
# include <stdio.h> int main(void) { int val; printf("请输入一个数字:"); printf("val = ...
- Python-dict-12
字典 Why:咱们目前已经学习到的容器型数据类型只有list,那么list够用?他有什么缺点呢? 1. 列表可以存储大量的数据类型,但是如果数据量大的话,他的查询速度比较慢. 2. 列表只能按照顺序存 ...
- 实践简单的项目WC
#include<iostream> #include<fstream> #include<string> #include<Windows.h> us ...
- linux 第七周 总结及实验
姬梦馨 原创作品 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 第七周 Linux内核如何装载和启动一 ...
- 基于SSH框架的在线考勤系统开发的质量属性
我要开发的是一个基于SSH框架的在线考勤系统. 质量属性是指影响质量的相关因素,下面我将分别从6个系统质量属性(可用性,易用性,可修改性,性能,安全性,可测试性)来分析我的系统,以及如何实现这些质量属 ...
- myeclipse从SVN检出项目报错
今天是在公司实习的第一天,从SVN服务器检出项目后发现报错. 解决方法: 1. 右键项目,选择属性properties-->选择resource-->将others选中并换为UTF-8 2 ...
- Redis持久化的两种方式和区别
该文转载自:http://www.cnblogs.com/swyi/p/6093763.html Redis持久化的两种方式和区别 Redis是一种高级key-value数据库.它跟memcached ...
- 4-Python3从入门到实战—基础之数据类型(字符串-String)
Python从入门到实战系列--目录 字符串表示 在 Python 3版本中,字符串是以 Unicode 编码的:Python 中使用 ' '或者" "表示字符串 msg = 'H ...
- Oracle 数据库启动过程
一 启动数据库 Oracle启动过程涉及几种模式,这些模式涉及不同的文件,每个状态下数据库做不同的事情,同时这些模式适用于不同的维护需求,主要的模式有三种:NOMOUNT.MOUNT.OPEN. 1 ...
- Windows系统下PHP使用Redis
参考链接:https://www.cnblogs.com/lhat/p/6402472.html 环境:windows 10 64位操作系统 php 5.4 redis 3.0 1.redis ...