PHP 프로젝트에서 Composer 패키지를 설치하고 사용하는 방법은 무엇입니까?

PHPz
풀어 주다: 2024-07-19 18:36:27
원래의
1012명이 탐색했습니다.

How to Install and Use Composer Packages in PHP Project?

As in my last post, I explained how to install and use Composer in PHP. Today, we will learn how to install Composer packages in a PHP project. The packages we need to install are collections and pestphp/pest.

Searching for Packages

  • Open your browser and navigate to ( https://packagist.org/ )

  • Search for collections. Scroll down to find the illuminate/collections package. We will install this package.

Installing Package

Open your terminal and run the composer command to check if Composer is installed. If it is, run composer search collections to search for the collections package a list of collection packages is available. Then, run composer require illuminate/collections to install the package.

Using Package

To use package we have to create a new file playground.php in the public directory and add the following code to filter the numbers less then or equal to 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);
로그인 후 복사

Run php public/playground.php in the terminal to see the output of filtered numbers.

Installing Pest Package

To install the pestphp/pest package, run composer search test a list of test packages show on screen and select one of the test packages. Then, run composer require pestphp/pest --dev to install the package as a development dependency.

Configuring Pest Package

For configuration of pest package run php vendor/bin/pest --init command. This will update the composer.json file as:

{
    "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
        }
    }
}
로그인 후 복사

and create a new directory tests with a file ExampleTest.php.

<?php

test('example', function(){

    expect(true)->toBeTrue();
});
로그인 후 복사

Running Tests

We have to run php vendor/bin/pest command in the terminal to see the output of the test file. If you replace true with false, you will see an error message.

Binding Test

Let's take a local binding test for container as something is resolved from this. So, Rename the file ContainerTest.php and replace the code with:

<?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');
});
로그인 후 복사

To test run php vendor/bin/pest command again in terminal/prompt it shows output as there is no error in file

I hope that you have clearly understood it.

위 내용은 PHP 프로젝트에서 Composer 패키지를 설치하고 사용하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:dev.to
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!