laravel中踩过的坑

config函数读取不到配置

config文件

// api地址
    'api'=>[
        'user.create'=>[
            'method'=>'POST',
            'api'=>'https://api.netease.im/nimserver/user/create.action'
        ],

读取配置的代码

$method = config('netease.api.user.create.method');
$api = config('netease.api.user.create.api');

最后怎么也读取不到,也试了清除缓存,最后还是 null

artisan cache:clear
artisan config:clear

问题所在

因为云信那边的api是create.action这种形式,中间是 点 连接的,我就把create.action作为key,存配置了

结果 laravel中config create.action 表示create的下一级action,这样自然是读取不到了

解决办法

'user_create'=>[
            'method'=>'POST',
            'api'=>'https://api.netease.im/nimserver/user/create.action'
        ],
$method = config('netease.api.user_create.method');
$api = config('netease.api.user_create.api');

config中使用route('xxxx')出错

config/setting.php

//内部使用的api
'inside'=>[
    'check-shake-hands'=>route('inside.game.check-shake-hands'),
]

然后使用config('setting.inside.check-shake-hands')读取,然后出现以下错误,其实路由都是正确的,,只要不在config中使用,就正确

file

p.s 未解决..待解决

"Impossible to create the root directory "xxxxx"."

在存储文件的时候碰到的

请检查xxxx目录的权限

laravel使用队列无限循环

队列分发代码

因为这个入队列是在Command中使用的,所以不能$this->dispatch(),只能用dispatch()

dispatch((new UserActivityIncome(true,$userId,$amount,$activityId,$activityConfId))->onQueue('user_activity_income'));

这个时候队列一直无限的循环,但是我确实只触发了一次

[2018-03-22 18:03:37] Processing: App\Jobs\UserActivityIncome
[2018-03-22 18:03:37] Processing: App\Jobs\UserActivityIncome
[2018-03-22 18:03:37] Processing: App\Jobs\UserActivityIncome
[2018-03-22 18:03:37] Processing: App\Jobs\UserActivityIncome
[2018-03-22 18:03:37] Processing: App\Jobs\UserActivityIncome
[2018-03-22 18:03:37] Processing: App\Jobs\UserActivityIncome
[2018-03-22 18:03:37] Processing: App\Jobs\UserActivityIncome
...........

后来查手册才知道,是因为队列错误了,,然后我又没有指定重试的次数,所以就无限循环了 于是在UserActivityIncome类中加了这两个属性,来表明最大重试次数和超时时间

//最大重试次数
public $tries = 5;
//队列超时时间
public $timeout = 60;

但是此时队列却停在了第五次的地方,因为我设置了最大重试次数

[2018-03-22 18:03:37] Processing: App\Jobs\UserActivityIncome
[2018-03-22 18:03:37] Processing: App\Jobs\UserActivityIncome
[2018-03-22 18:03:37] Processing: App\Jobs\UserActivityIncome
[2018-03-22 18:03:37] Processing: App\Jobs\UserActivityIncome
[2018-03-22 18:03:37] Processing: App\Jobs\UserActivityIncome
[2018-03-22 18:03:37] Failed:     App\Jobs\UserActivityIncome

最终解决方案

最后才发现是我的UserActivityIncome类里面的代码有问题,才导致了这个队列失败

问题1

$userIncomeData = $activityUserIncomeRep->where('user_id', $this->userId)->where('activity_conf_id', $this->activityConfId)->first();

改为

$userIncomeData = $activityUserIncomeRep->m()->where('user_id', $this->userId)->where('activity_conf_id', $this->activityConfId)->first();

问题2

我的$userIncomeData里面并没有activity_id这个属性,然而我却用了,所以报错了

$incomeData = [
    'user_id'     => $this->userId,
    'activity_id' => $userIncomeData->activity_id,
    'amount'      => $this->amount,
    'add_time'    => time(),
];

queue:listen 和 queue:work --daemon 的区别

  • queue:work 默认只执行一次队列请求, 当请求执行完成后就终止;
  • queue:listen 监听队列请求, 只要运行着, 就能一直接受请求, 除非手动终止;
  • queue:work --daemon 同 listen 一样, 只要运行着, 就能一直接受请求, 不一样的地方是在这个运行模式下, 当新的请求到来的时候, 不重新加载整个框架, 而是直接 fire 动作

能看出来, queue:work --daemon 是最高级的, 一般推荐使用这个来处理队列监听.

注意: 使用 queue:work --daemon , 当更新代码的时候, 需要停止, 然后重新启动, 这样才能把修改的代码应用上.

至于存在的必要 首先, --daemon 是在 Laravel 4.2 以后才加入的. 其次, 从上面的分析来看, queue:listen 和 queue:work --daemon 还是有区别的.

启动指定队列

我再这里把一个任务晒到了use_activity_prop名字的队列里

$job = (new UseActivityProp(array_first($revivalList), $activityId, time()))->onQueue('use_activity_prop');
$this->dispatch($job);

所以我启动队列,就必须--queue=use_activity_prop,如果没有onQueue的话,那么不带这个参数是可以得

php artisan queue:work redis --queue=use_activity_prop --daemon

tinker中create()数据 Allowed memory size of 134217728 bytes exhausted

$ php artisan tinker
Psy Shell v0.8.17 (PHP 7.2.4 — cli) by Justin Hileman
>>> namespace App\Models;
>>> factory(User::class,1000)->create();
PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) in /Users/ailuoy/code/learning/mysql/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 332
PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 65536 bytes) in /Users/ailuoy/code/learning/mysql/vendor/laravel/framework/src/Illuminate/Contracts/Logging/Log.php on line 59

Laravel Error: Please provide a valid cache path

第一步赋予权限

sudo chmod 777 bootstrap/cache/ storage/

可能没有缓存文件夹

如果还是不行,那有可能就是你再storage下没有缓存文件夹

继续在 storage/framework 下面创建 sessions, views, cache 文件夹, 搞定。。

mkidr -p storage/framework/sessions
mkidr -p storage/framework/views
mkidr -p storage/framework/cache

laravel/passport 安装失败

Using version ^6.0 for laravel/passport
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Conclusion: don't install laravel/passport v6.0.6
    - Conclusion: don't install laravel/passport v6.0.5
    - Conclusion: don't install laravel/passport v6.0.4
    - Conclusion: don't install laravel/passport v6.0.3
    - Conclusion: don't install laravel/passport v6.0.2
    - Conclusion: don't install laravel/passport v6.0.1
    - Conclusion: don't install laravel/passport v6.0.0
    - Conclusion: don't install laravel/passport 6.0.x-dev
    - Conclusion: remove paragonie/random_compat v9.99.99
    - Installation request for laravel/passport ^6.0 -> satisfiable by laravel/passport[6.0.x-dev, v6.0.0, v6.0.1, v6.0.2, v6.0.3, v6.0.4, v6.0.5, v6.0.6].
    - Conclusion: don't install paragonie/random_compat v9.99.99
    - laravel/passport 6.0.x-dev requires league/oauth2-server ^7.0 -> satisfiable by league/oauth2-server[7.0.0, 7.1.0, 7.1.1, 7.2.0].

解决方案

composer require paragonie/random_compat "2.*"
composer require laravel/passport

产考文献