如何在PHP项目中安装和使用Composer包?
来源:dev.to
时间:2024-07-19 08:21:47 367浏览 收藏
来到golang学习网的大家,相信都是编程学习爱好者,希望在这里学习文章相关编程知识。下面本篇文章就来带大家聊聊《如何在PHP项目中安装和使用Composer包?》,介绍一下,希望对大家的知识积累有所帮助,助力实战开发!
在我的上一篇文章中,我解释了如何在 php 中安装和使用 composer。今天,我们将学习如何在 php 项目中安装 composer 包。我们需要安装的包是collections和 pestphp/pest.
搜索包
打开浏览器并导航至 ( https://packagist.org/ )
搜索收藏。向下滚动找到照明/收藏包。我们将安装这个包。
安装包
打开终端并运行composer命令来检查composer是否已安装。如果是,请运行composer search collections 来搜索集合包,并提供可用的集合包列表。然后,运行composer requireillumination/collections来安装包。
使用包
要使用包,我们必须在public目录中创建一个新文件playground.php,并添加以下代码来过滤小于或等于5的数字:
<?php use illuminate\support\collection; 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 包
要配置 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 Core\Container; 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包?》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布文章相关知识,快来关注吧!
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
192 收藏
-
107 收藏
-
279 收藏
-
158 收藏
-
252 收藏
-
120 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 541次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 506次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习