
在我的上一篇文章中,我解释了如何在 php 中安装和使用 composer。今天,我们将学习如何在 php 项目中安装 composer 包。我们需要安装的包是collections和 pestphp/pest.
打开浏览器并导航至 ( https://packagist.org/ )
搜索收藏。向下滚动找到照明/收藏包。我们将安装这个包。
打开终端并运行composer命令来检查composer是否已安装。如果是,请运行composer search collections 来搜索集合包,并提供可用的集合包列表。然后,运行composer requireillumination/collections来安装包。
立即学习“PHP免费学习笔记(深入)”;
要使用包,我们必须在public目录中创建一个新文件playground.php,并添加以下代码来过滤小于或等于5的数字:
<?php use illuminatesupportcollection; require __dir__.'/../vendor/autoload.php'; $numbers = new collection([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); $lessthanorequalto5 = $numbers->filter(fn($number) => $number <= 5); var_dump($lessthanorequalto5);
在终端中运行 php public/playground.php 可以看到过滤后数字的输出。
要安装 pestphp/pest 包,请运行 composer search test 屏幕上显示的测试包列表,然后选择其中一个测试包。然后,运行composer require pestphp/pest --dev 将软件包安装为开发依赖项。
要配置 pest 包,请运行 phpvendor/bin/pest --init 命令。这会将composer.json 文件更新为:
{
"name": "admin/demo",
"authors": [
{
"name": "ghulam mujtaba",
"email": "mujtabaofficial247@gmail.com"
}
],
"require": {
"illuminate/collections": "^11.16"
},
"autoload": {
"psr-4": {
"core\": "core/" ,
"http\": "http/" }
},
"require-dev": {
"pestphp/pest": "^2.34"
},
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true
}
}
}
并使用文件exampletest.php创建一个新目录tests。
<?php
test('example', function(){
expect(true)->tobetrue();
});
我们必须在终端中运行 phpvendor/bin/pest 命令才能看到测试文件的输出。如果将 true 替换为 false,您将看到一条错误消息。
让我们对容器进行本地绑定测试,因为问题已经解决了。因此,重命名文件 containertest.php 并将代码替换为:
<?php
use CoreContainer;
test('it can resolve something out of the container', function () {
$container = new Container();
$container->bind('foo', fn()=> 'foo');
$result = $container->resolve('foo');
expect($result)->toEqual('foo');
});
要测试在终端/提示符中再次运行 phpvendor/bin/pest 命令,它会显示输出,因为文件中没有错误
我希望你已经清楚地理解了。
以上就是如何在PHP项目中安装和使用Composer包?的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号