macOS PHP 使用 Pecl 安装 Swoole 扩展并启用 openssl 作者: Chuwen 时间: 2023-08-26 分类: PHP 评论 ## 安装 PHP 可以参照此教程安装:https://stitcher.io/blog/php-82-upgrade-mac#upgrade-with-shivammathur/homebrew-php 为了节约时间,我摘抄部分 ```shell brew tap shivammathur/php brew install shivammathur/php/php@8.2 # 切换至 PHP 8.2 brew link --overwrite --force php@8.2 ``` ## 安装 openssl & 获取安装路径 如果你没有安装,可以使用此命令进行安装 ```shell brew install openssl ``` ![](https://cdn.nowtime.cc/2023/08/26/4241934229.png) 比如我的就是 `/usr/local/Cellar/openssl@3/3.1.2` 这个路径,请记住这个路径 ## 安装 swoole 扩展 & 启用 openssl 支持 > 确保你的网络环境正常 执行以下命令 ```shell sudo pecl install swoole ``` 当询问你是否启用 openssl 是,你就输入 `yes --with-openssl-dir=/usr/local/Cellar/openssl@3/3.1.2` > 其中 `/usr/local/Cellar/openssl@3/3.1.2` 是 openssl 的安装路径 ![](https://cdn.nowtime.cc/2023/08/26/3703705991.png) 其他的按照自己需求填入 `yes` OR `no` 就好了,不过需要注意可能需要指定相关库的路径,详情请查看[官方文档 -> 编译选项 -> 通用参数](https://wiki.swoole.com/#/environment?id=%e9%80%9a%e7%94%a8%e5%8f%82%e6%95%b0 "官方文档") ## 安装完成 ![](https://cdn.nowtime.cc/2023/08/26/3470978648.png) 检查是否安装成功,执行命令 `php --ri swoole`,如果有内容输出(如图)表示安装成功 ![](https://cdn.nowtime.cc/2023/08/26/3318436440.png)
Laravel `Http::withUrlParameters` 用法 作者: Chuwen 时间: 2023-07-18 分类: Laravel,PHP 评论 ## 使用前 在调用 HTTP API 时会发现有很多 API 都是采用 *REST API* 风格,例如: * /admin/api/2023-07/products/{product_id}.json * /admin/api/2023-01/orders/{order_id}.json * /admin/api/2023-01/orders/{order_id}/fulfillments/{fulfillment_id}.json * ... 不难发现它们都有一个共同点,以花括号(`{var_name}`、`{order_id}`)包裹的变量替换为对应的值,通常我们可能会这么做: ```php $http = \Illuminate\Support\Facades\Http::baseUrl('https://example.com'); $order_id = 123456789; $fulfillment_id = 9889894445; $orderUrl = sprintf('/admin/api/2023-07/orders/%d.json', $product_id); $fulfillmentUrl = sprintf('/admin/api/2023-01/orders/%d/fulfillments/%d.json', $order_id, $fulfillment_id); $order = $http->get($orderUrl); $fulfillment = $http->get($fulfillmentUrl); ``` 但是会发现这样拼接 URI 会很繁琐 ## 使用后 - `Http::withUrlParameters` ```php $http = \Illuminate\Support\Facades\Http::baseUrl('https://example.com') ->withUrlParameters([ 'order_id' => 123456789, 'fulfillment_id' => 9889894445 ]); // 比如你要使用上述定义 URL 参数:order_id // 你在 URL 只需要填 {order_id} // 在请求时会自动替换 $orderUrl = '/admin/api/2023-07/orders/{order_id}.json'; $fulfillmentUrl = '/admin/api/2023-01/orders/{orders_id}/fulfillments/{fulfillment_id}.json'; $order = $http->get($orderUrl); $fulfillment = $http->get($fulfillmentUrl); ``` 会发现使用会很方便,不需要再使用如 `sprintf` 去拼接 URL 了
Laravel Mixin 使用方法 作者: Chuwen 时间: 2023-06-29 分类: PHP 评论 # 目录 * [目录](#目录) * [1. 创建目录](#1-创建目录) * [2. 扩展对应类](#2-扩展对应类) * [3. Mixin macro](#3-mixin-macro) * [3. 使用](#3-使用) ## 1. 创建目录 ```shell mkdir -p app/Macros ``` ## 2. 扩展对应类 比如你要扩展 `Arr`,你可以建立一个形如 `ArrMacros.php` 文件 内容如下: ```php 'nowtime_cc' ]); // 输出结果: /test?ref=nowtime_cc ``` ## 4. 其他 如果你还使用了 https://github.com/barryvdh/laravel-ide-helper 你还可以使用 `php artisan ide-helper:generate` 生成对应的注释,在 PHPStorm 就会有语法提示 ![](https://cdn.nowtime.cc/2023/06/29/1457489303.png)
Hyperf 单元测试运行报错:PHP Warning: pcntl_fork() has been disabled for security reasons in... 作者: Chuwen 时间: 2023-06-12 分类: PHP 评论 ## 背景 因为 Hyperf 使用协程运行,故原本的 PHPUnit 的方式就无法运行,需要使用框架自带的 `co-phpunit` 二进制程序运行 ## 配置 PHPUnit 如果你原本就配置了 PHPUnit,建议删掉重新配置 ![](https://cdn.nowtime.cc/2023/07/09/536344092.png) 然后再按照如下步骤进行配置: 1. **PHPUnit 库**选择 *phpunit.phar* 路径 2. *phpunit.phar* 输入框选择你项目路径下的 `vendor/bin/co-phpunit` 文件 3. **测试运行程序** - *默认配置文件* 勾选,选择项目路径下的 `phpunit.xml` 文件 4. 最后点击“确定”保存即可 ![](https://cdn.nowtime.cc/2023/07/09/2791021112.png) ## 调整 PHPUnit "运行/调试配置模版" ![](https://cdn.nowtime.cc/2023/06/12/1302679487.png) 在第 4 步输入例如:`--prepend /Users/chuwen/wwwroot/xxxxx-hyperf/test/bootstrap.php`,其中 *xxxxx-hyperf* 是项目名,要改成你的项目名 ![](https://cdn.nowtime.cc/2023/06/12/3789934554.png) 移除在配置前的配置 ![](https://cdn.nowtime.cc/2023/06/12/3500733315.png) 然后再运行一下就可以了 ![](https://cdn.nowtime.cc/2023/06/12/2418105964.png)
Laravel 模型(Model) 关联(eloquent-relationships) 查询未找到值返回默认值 作者: Chuwen 时间: 2023-05-29 分类: Laravel 评论 ## Laravel 模型关联查询未找到值返回默认值 在实际业务中,经常会用到模型关联查询,但是有时候关联的那个模型在数据库中不存在,则会返回 null,如果你再去判断是否为 null 逻辑就很复杂,下面给出一个现实中的常见一个例子 假设有 2 个模型,`User::class` 和 `UserConfig::class` **User::class** ```php hasOne(\App\Models\UserConfig::class, 'uid', 'id'); } } ``` **UserConfig::class** ```php $userM->config->locales ]; ``` ## 解决方法 [翻阅文档](https://laravel.com/docs/10.x/eloquent-relationships#default-models)发现有这样一个方法 `withDefault()` 官方文档的解释: > The belongsTo, hasOne, hasOneThrough, and morphOne relationships allow you to define a default model that will be returned if the given relationship is null. This pattern is often referred to as the Null Object pattern and can help remove conditional checks in your code. In the following example, the user relation will return an empty App\Models\User model if no user is attached to the Post model: > > 机器翻译成中文: > >> 如果给定的关系为空,则可以定义默认模型。这种模式通常称为 `空对象模式`,可以帮助删除代码中的条件检查。在以下示例中,如果没有用户附加到Post模型,用户关系将返回一个空的App\Models\User模型: 用法很简单,只需要在 `hasOne` 末尾加上 `->withDefault()` 方法,然后在 `UserConfig::class` 的 `$attributes` 属性写上默认值即可 **User::class** ```php hasOne(\App\Models\UserConfig::class, 'uid', 'id')->withDefault(); } } ``` **UserConfig::class** ```php ['en'] ]; } ``` ```php $uid = 1; $userM = User::findOrFail($uid); return [ // 如果关联的 config 模型查找不到值,就会返回默认值 ['en'] 'locales' => $userM->config->locales ]; ```