If you’ve found an error, spotted something missing, or feel a section could be clearer or better explained, we’d love to hear from you. Your feedback helps keep the documentation accurate and useful for everyone.
Please report issues or suggest improvements on GitHub. Community input is invaluable in making the docs stronger.
Not comfortable with GitHub? No problem — you can also get in touch with us directly via our contact form. We welcome all feedback.
Using Packagist With Trongate
This guide outlines the process for integrating third-party PHP libraries into Trongate applications using Packagist, the primary repository for Composer packages. For demonstration purposes, the instructions below specifically focus on downloading and utilizing Guzzle, a third-party PHP library that facilitates the sending and receiving of HTTP requests and responses.
System Requirements
Ensure that the system meets the following requirements:
- PHP: PHP 8 or newer is required to use the latest version of Trongate with Packagist.
- Composer: Composer must be installed on the system for managing library dependencies. If Composer is not installed, visit getcomposer.org to download and install it.
- Internet Connection: An active internet connection is necessary to download packages from Packagist.
Step 1: Setting Up the Project with Composer
-
Initialize Composer: If a
composer.json
file is not already present in the project, create one by running the following command in the project directory: Follow the prompts to configure the project. This setup helps manage project dependencies. - Install Guzzle: Add Guzzle to the project by running: This command instructs Composer to find Guzzle on Packagist and install it along with any necessary dependencies.
-
Verify Installation: Ensure that the Guzzle library has been added to the project by checking
that the
composer.json
file includes Guzzle and that thevendor
directory has been created with Guzzle files in it.
Step 2: Using Guzzle in a Trongate Application
- Autoload Dependencies: To use Guzzle, include Composer's autoloader in the PHP script. Add the following line at the top of the PHP file: This inclusion ensures that all dependencies, including Guzzle, are loaded and available for use.
- Create an HTTP Client: Instantiate a new Guzzle client to make HTTP requests:
- Make an HTTP Request: Use the client to send a request and retrieve the response:
Complete Example
Below is an example of using Guzzle within a Trongate controller file to fetch data from GitHub's API:
In the example above, the APPPATH
constant is used to specify the location of the
autoload.php
file. All Trongate controllers, view files, and templates have access to the
APPPATH
constant, which references the application's root directory. This provides a reliable
method for specifying file paths.
The example also utilizes PHP's require_once
function, ensuring that the file is included only
once, safeguarding the application's stability.
Conclusion
Although Trongate has its own code-sharing platform, there may be instances where downloading third-party code via Packagist is desirable. Packagist offers a vast repository of PHP libraries and packages that can significantly accelerate development and bring advanced functionality to projects with minimal effort.
However, the use of Packagist is not without challenges. Relying on Packagist can lead to potential issues with stability, versioning, and dependencies. Moreover, developers considering using code from Packagist should be aware of potential security vulnerabilities associated with Packagist.
The Trongate framework enables developers to selectively use Packagist for specific modules, providing precise integration without compromising the overall performance of the application. This targeted approach ensures that enhancements can be made where necessary without affecting the efficiency of the entire system.