PHP プロジェクトで Composer をインストールして使用する方法は?

WBOY
リリース: 2024-07-19 11:45:50
オリジナル
386 人が閲覧しました

How to lnstall and Use Composer in PHP Project?

Composer Introduction

Composer is a dependency manager for PHP that helps manage libraries and dependencies in your web development projects. It installs and updates dependencies declared in your project's composer.json file, autloads classes from installed dependencies, simplifies project setup and collaboration, and enhances security and performance.

Composer Installation

To install Composer, follow these steps:

  • Open a browser and navigate to (https://getcomposer.org/)
  • Click on the "Download" button it moves you to next page where you see a code snippet/block.
  • Copy the code snippet provided on that page and paste it into your terminal/command prompt
  • Run the command to download and install Composer (note: this method only works on Linux and macOS)
  • For Windows, download the executable file (.exe) and follow the installation prompts
  • Once installed, open a terminal/command prompt and type composer to verify the installation

Composer Initialization

To initialize a new Composer project, run the command composer init in your terminal/command prompt. This will create a composer.json file and prompt you to provide information about your project, such as:

  • Name
  • Description
  • Author
  • License
  • Dependencies (require and require-dev)

Here's an example of the prompts you'll see when running composer init:

Package name (<vendor>/<name>) []: Ghulam Mujtaba <mujtabaofficial247@gmail.com>
Description []: 
Author [n to skip]: 
License []: 
Minimum Stability []: 
Package type (library, project, metapackage, custom) []: 
Define your dependencies: 
Would you like to define your dependencies (require) interactively [yes]? no 
Would you like to define your dev dependencies (require-dev) interactively [yes]? no 
ログイン後にコピー

Autoloading

To autoload classes and functions, we have to add the following code to our composer.json file:

{
    "name": "admin/demo",
    "authors": [
        {
            "name": "Ghulam Mujtaba",
            "email": "mujtabaofficial247@gmail.com"
        }
    ],
    "autoload": {
        "psr-4": {
            "Core\\": "Core/",
            "Http\\": "Http/"
        }
    }
}
ログイン後にコピー

The double backslash (\\) is used to escape the namespace separator, which is a single backslash (\).

Add path

Open Public/Index.php file as entry point for project and add the BASE_PATH at top, after classes import statements, to autoload classes in project:

require BASE_PATH . 'vendor/autoload.php';
ログイン後にコピー

And remove any existing spl_autoload code statements from the file and run the project it shows error as Core/Container is missing.

Updating Autoload

To fix container missing issue we have to update the autoload configuration by running the command composer dump-autoload in terminal. This will update the autoload_psr4.php file in the vendor directory. In psr-4 the namespace must end with namespace separator. The updated autoload_psr4.php file is:

<?php
// autoload_psr4.php @generated by Composer

$vendorDir = dirname(__DIR__);
$baseDir = dirname($vendorDir);

return array(
    'Core\\' => array($baseDir . '/Core'),
    'Http\\' => array($baseDir . '/Http'),
);
ログイン後にコピー

Running the Project

After completing these steps, refresh your browser to see the output. Composer should now autoload the classes and run the project successfully.

I hope that you have clearly understood it.

以上がPHP プロジェクトで Composer をインストールして使用する方法は?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ソース:dev.to
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!