Autoload namespaces via Composer. PHP Code example open

Autoload namespaces via Composer. PHP Code example

Composer is a package manager for the PHP programming language written in PHP. The main task of Composer is to manage project dependencies, just like npm\yarn does for node.js, pip for python, etc.

Installation is through the console or by unpacking the files
PHP must be installed on your system for using Composer!

For an example we will show how to install Composer through the console. To do this, go to the folder with our project and run the console from this place. If we want to install Composer not globally in our system.

Enter the following commands in the terminal:

$ composer install - - will start the Composer installation

If Composer was previously installed, update it by entering the command:

$ composer self-update 

After installing Composer will create a “vendor” folder in which we can create a “core” folder of our application. This folder will contain kernel files.
We also add the composer.json file to the root directory of our app.

Composer.json in folder

We add the following code:

{
    "autoload": {
      "psr-4": {//code writing standard
        "core\\": "vendor/core",//when setting the core namespace, the bootloader will search for files in the vendor/core folder
        "app\\": "app"
      }
    },
    "require": {
    }
  }

The autoload.php file will also appear in the vendor folder.
This file must be connected via require_once to our public/index.php file.

If our files specify namespace according to the psr-4 standard, autoload.php will automatically connect the required files.

May be interesed – Create your own php framework. OOP PHP8 code example

0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation