composer Archives | phpGrid - PHP Datagrid Create PHP grids in minutes, not hours. Sat, 11 Nov 2023 05:51:51 +0000 en-US hourly 1 CodeProjecthttps://wordpress.org/?v=7.0.3 129966043 composer install common issues https://phpgrid.com/documentation/composer-install-common-issues/ Fri, 15 Sep 2023 14:10:00 +0000 https://phpgrid.com/?p=10138 Since introduction of composer install in version 7.5.3, it has been an excellent way for dependencies management while minimizing file distribution size. Users can keep dependencies updated by running composer update without reaching out to us, a win-win. This post will document composer-related common issues reported by our users and remedies to address those issues. […]

The post composer install common issues appeared first on phpGrid - PHP Datagrid.

]]>
Since introduction of composer install in version 7.5.3, it has been an excellent way for dependencies management while minimizing file distribution size. Users can keep dependencies updated by running composer update without reaching out to us, a win-win.

This post will document composer-related common issues reported by our users and remedies to address those issues.

PHPExcel (deprecated) has now been replaced by PHPSpreadsheet. It requires two PHP extensions, depending versions of PHP, sometime they are not enabled by default. If they are missing, you are like to encounter the following error messages during composer install.

1
2
- Root composer.json requires phpoffice/phpspreadsheet ^1.29 -> satisfiable by phpoffice/phpspreadsheet[1.29.0].
- phpoffice/phpspreadsheet 1.29.0 requires ext-gd * -> it is missing from your system. Install or enable PHP's gd extension.

To enable extensions, verify that the following are enabled in your php.ini files:

– extension=gd
– extension=zip

You can also run `php –ini` in a terminal to see which files are used by PHP in CLI mode. Alternatively, but not recommended, you can run Composer with –ignore-platform-req=ext-gd to temporarily ignore these required extensions if you are sure they are already enabled.

Remember always to restart web server after making any changes in php.ini.

The post composer install common issues appeared first on phpGrid - PHP Datagrid.

]]>
10138
PHP Composer to Autoload a Third Party Library https://phpgrid.com/blog/php-composer-to-autoload-a-third-party-library/ Wed, 18 Jan 2023 22:27:18 +0000 https://phpgrid.com/?p=10058

What is exactly a Composer in PHP? PHP Composer is a dependency management tool for PHP. It allows developers to declare the libraries their project depends on and it will manage (install/update) them for you. Composer uses a file called “composer.json” to manage dependencies. In this file, you list the dependencies your project has, along […]

The post PHP Composer to Autoload a Third Party Library appeared first on phpGrid - PHP Datagrid.

]]>

What is exactly a Composer in PHP?

PHP Composer is a dependency management tool for PHP. It allows developers to declare the libraries their project depends on and it will manage (install/update) them for you.

Composer uses a file called “composer.json” to manage dependencies. In this file, you list the dependencies your project has, along with version constraints. Once you have defined your dependencies, you can use the Composer command line tool to install them.

Adding a package dependence

To add a package dependence manually in Composer, use “require” command.

For example, if you want to add the package “monolog/monolog” as a dependency to your project, you would run the following command in your project’s root directory:

1
composer require monolog/monolog

This will add the package to the require section of your composer.json file and install the package and its dependencies into the “vendor” directory.

You can also specify the version number of the package that you want to install by doing the following:

1
composer require monolog/monolog:1.0.*

The above command will install version 1.0 or any version that starts with 1.0 like 1.0.1, 1.0.2

Once you have added a package as a dependency, you can update or remove it using Composer as well. You can run “composer update” command to update all the dependencies or update only a specific package this way:

1
composer update monolog/monolog

And to remove a package, use remove command.

1
composer remove monolog/monolog"

Once you have all the required dependencies, simply run the following install command

1
composer install

Composer will download the dependencies and their dependencies recursively and install them in a directory called “vendor”. The dependencies can then be autoloaded using the Composer’s autoloader.

What is Composer autoloading?

Now move on to an important Composer feature: autoload. Autoloading is a way to automatically include the necessary files when a class is used in an application, without having to manually include or require each file. This can greatly simplify the organization and maintenance of your codebase.

So instead of having to manually include a large number of files at the top of each script, you can simply use the classes you need and let the autoloader handle the rest. You can organize your code into different namespaces and directories, without having to worry about manually including the correct files.

In other words, Composer autoload replaces the old way of using include and require on top of every PHP script.

More about autoloading: https://getcomposer.org/doc/01-basic-usage.md#autoloading

Autoload a 3rd party library manually

You can also manually add a 3rd party library, such as phpGrid, to the autoload section in Composer. There are a few ways to load a 3rd party class in the Composer autoloader.

1. Using the “classmap” autoloading method

This method allows you to specify a list of specific files that should be included in the autoloader. You can add the classmap entries to the “autoload” section of your composer.json file like this:

1
2
3
4
5
6
7
"autoload": {
    "classmap": [
        "path/to/example/class1.php",
        "path/to/example/class2.php",
        "path/to/example/class3.php"
    ]
}

2. Use “files” autoloading method

This method allows you to specify a list of files that should be included in the autoloader. You can add the files entries to the “autoload” section of your composer.json file like this:

1
2
3
4
5
"autoload": {
    "files": [
        "path/to/phpGrid/conf.php"
    ]
}
Using files autoload is recommended method to add phpGrid in Composer.
X

You can combine different autoload methods in the same composer.json file, with a combination of psr-4, classmap and files.

Don’t forget to run composer dump-autoload

It is important to note that when you use the classmap and files autoloading method, you need to run the composer dump-autoload command after adding or modifying the files/classmap, for the autoloader to be updated.

1
composer dump-autoload

That’s is for all about PHP Composer and its autoloading. Hope you finding it useful in your PHP coding journey, and happy gridding!

The post PHP Composer to Autoload a Third Party Library appeared first on phpGrid - PHP Datagrid.

]]>
10058