laravel在debug模式下抛出异常时,会把当前的环境变量等打印出来,即使是开发机、测试机,其实有时候也不希望将一些关键的变量公开给所有人,比如密码、APPKEY(主要是不少人在上传的时候APPKEY本地和服务器一致),还有各类OSS、微信的appId,secret,token等,所以就需要在抛出异常时隐藏掉。
网上查到的是在config/app.php中增加:debug_hide 的数组下标,对应的数据是_ENV,_SERVER,其实就是把遍历这些全局变量,对指定的KEY进行隐藏。但偶尔也查到有debug_blacklist。于是全文搜索了一下,发现框架里是:foreach([config('app.debug_hide'),config('app.debug_blacklist')]),看来这是一个历史 遗留问题。
知道怎么写之后就方便了。例如:
'debug_hide'=>[
'_ENV' => ['APP_KEY']
]
,随便抛一个异常可以看到 ,输出的APP_KEY的地方,已经不再是标准值了,而是一堆***,关键变量其实有很多,如果一个个写,感觉有点累,再加上,它不支持 * 的模拟判断,比如*KEY*,则完全无效。于是偷了个懒:
'debug_hide'=>[
'_ENV' => array_filter(array_keys($_ENV),fn($v)=> Str::contains(strtolower($v), ['oss_', 'db_', 'cookie', 'key','secret', 'password', 'aliyun']) ),
]
既然都在debug_mode下了。也不在乎这点性能了。。
tips:
1、不能用str($v)->contains,因为str这个helper函数,居然还调用了env的class,而在刚加载config文件时。env的类还没有加载进来。
2、strtolower,是因为Str::contains不能忽略大小写。虽然.env都是标准的大写,但你不能保证其他人有没有写小写(又不能抽他们)。所以先tolower一下
解决!
发现从PHP7.4升到PHP8后,就一个地方,那就是 get_magic_quotes_gpc 这个函数。报错信息是:PHP message: PHP Fatal error: Uncaught Error: Call to undefined function get_magic_quotes_gpc()。
方法未定义?看了下文档,原来从7.4.1开始就已经不再支持了,而且永远返回False,在PHP8中彻底移除,仅此而已,于是乎。。。。
在 include/common.php 文件中,在调用 get_magic_quotes_gpc上面加了一段 :
if(!function_exists('get_magic_quotes_gpc')){
function get_magic_quotes_gpc(){
return false;
}
}
然后,一切正常,所以我就不再上传新包了。(页面详情可能有不正常的情况,是因为我在上一页下一页的时候。$article[articleid]上没有加单引号,进入后台在模板管理 default/show.php 搜到 57行,下一篇的链接就是 ,调整一下即可)
----
改完后才发现,问题很多啊。。。比如 global.php的296行,$article[image] ,这种代码太多了。。。。估计得一点点改了,php4的后遗症啊。
----
只能说解决了95%的问题,其它的只能遇到后慢慢改了。。。我也不可能一个个的去定义:define('image','image');。当然 这样就彻底解决了,只是有点2
在使用Ziggy的时候,偶尔会报类似的错误:
Error in render: "Error: Ziggy error: 'project' parameter is required for route 'projectPage'."。
仔细看出错的路由,往往都是有必要参数的,比如Route::any('/test/{user:uuid}',[UserController::class,'test'])->name('test')
象这种路由,如果在JS中直接获取route().current()来获取当前路径并显示,那肯定就会报错。因为current()方法是将参数和路由一并显示出来。
route(route().current())这样的报错怎么处理呢?
其实,只要加上params参数就行了,例如:route(route().current(),route().params).toString(),为什么会有这么妖的写法?是因为在公众号的网页里,如果你要用jssdk,就必须是当前URL,如果不正确,获取的签名也就可能是失败的,因此,才想到用这个恶心的方法。当然你也可以用location.href...
好几次遇到阿里云的镜像更新有问题了,明明某个库有了最新版,但阿里云就是报版本不存在。今天又出现了这样的情况,安装laravel-zero的时候,require-dev中有个laravel/pint ^1.2,直接就报了
Problem 1
- Root composer.json requires laravel/pint ^1.2, found laravel/pint[dev-main, v0.1.0, ..., v0.2.4, v1.0.0, ..., v1.1.3] but it does not match the constraint.
由于之前也遇到过这个问题,当时是unset掉 aliyun镜像,换成了官方镜像,但确实是慢,所以我就看了一眼其他镜像,比如以前常用的https://packagist.phpcomposer.com,在国内没有其他镜像的时候,它做了很大的贡献,然而也太不稳定了。直接报:
Problem 1 - Root composer.json requires laravel-zero/framework, it could not be found in any version, there may be a typo in the package name. Problem 2 - Root composer.json requires nunomaduro/termwind, it could not be found in any version, there may be a typo in the package name. Problem 3 - Root composer.json requires laravel/pint, it could not be found in any version, there may be a typo in the package name. Problem 4 - Root composer.json requires mockery/mockery, it could not be found in any version, there may be a typo in the package name. Problem 5 - Root composer.json requires pestphp/pest, it could not be found in any version, there may be a typo in the package name. Potential causes: - A typo in the package name - The package is not available in a stable-enough version according to your minimum-stability setting see <https://getcomposer.org/doc/04-schema.md#minimum-stability> for more details. - It's a private package and you forgot to add a custom repository to find it Read <https://getcomposer.org/doc/articles/troubleshooting.md> for further common problems.
看来,已经多年没有更新过了,真可惜了。
配好之后,composer u,完成。记录一下
1、阿里:composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
2、腾讯:composer config -g repos.packagist composer https://mirrors.tencent.com/composer/
3、清除:composer config -g --unset repos.packagist
带 -g 的是全局的,不带 -g,就是当前项目了。
PHP有不少Redis库,比如pecl的Redis库(phpredis),就是直接自带了数组的存取和读出,因为他在存储和读出的时候自动序列化了。象是象predis就不行,如果你直接存一个数组去,那么它就会报warning,同时存一个Array到指定的KEY上。
但是predis允许你封装自定义的redis方法。比如jsonset/jsonget,这时候你利用这些自定义的方法来获取或写入数组即可。(基于Laravel,其他的也一样,其他参考:Setting arrays · Issue #136 · predis/predis (github.com))
只是官方的Issue中 【Predis\Profile\ServerProfile:】已经不存在了,要换成【Predis\Profile\Factory】,其余可以复制
----
基于Laravel的话,上述的数组可以放到config里,就啥出不用配置了~~
PHP代码
- if (!function_exists('yredis')) {
- class StringSetJson extends Predis\Command\StringSet
- {
- protected function filterArguments(array $arguments)
- {
- $arguments[1] = json_encode($arguments[1]);
- return $arguments;
- }
- }
-
- class StringGetJson extends Predis\Command\StringGet
- {
- public function parseResponse($data)
- {
- return json_decode($data, true);
- }
- }
-
- class StringSetPhp extends Predis\Command\StringSet
- {
- protected function filterArguments(array $arguments)
- {
- $arguments[1] = serialize($arguments[1]);
- return $arguments;
- }
- }
-
- class StringGetPhp extends Predis\Command\StringGet
- {
- public function parseResponse($data)
- {
- return unserialize($data);
- }
- }
-
-
-
-
-
-
- function yredis()
- {
- try {
- $redis = app('yredis');
- } catch (Exception $e) {
- app()->singleton('yredis', function ($app) {
- $config = $app->make('config')->get('database.redis', []);
- unset($config['options']['prefix']);
- if(env('REDIS_CLIENT') == 'predis'){
- $config['options']['profile'] = function ($options, $option) {
- $profile = \Predis\Profile\Factory::getDefault();
- $profile->defineCommand('jsonset', 'StringSetJson');
- $profile->defineCommand('jsonget', 'StringGetJson');
- $profile->defineCommand('phpset', 'StringSetPhp');
- $profile->defineCommand('phpget', 'StringGetPhp');
- return $profile;
- };
- }
- return new \Illuminate\Redis\RedisManager($app, \Illuminate\Support\Arr::pull($config, 'client', 'phpredis'), $config);
- });
- app()->bind('yredis.connection', function ($app) {
- return $app['yredis']->connection();
- });
- $redis = app('yredis');
- }
- return $redis;
- }
- }
因为laravel的默认config会带 prefix,所以写一个简单的,其实就是为了在写数据的时候不加 prefix 。。。