Introduction

PHP is an ever-evolving language, and as a developer, you might find yourself working on projects that require different PHP versions. Laravel, a popular PHP framework, provides a powerful tool called Laravel Herd to manage PHP versions seamlessly. In this article, we will explore the basics of Laravel Herd and how it can simplify the process of switching between PHP versions within a Laravel project.

Understanding Laravel Herd

Laravel Herd is a tool designed to simplify the management of PHP versions in Laravel projects. It allows developers to define the required PHP version for a specific project, making it easy to maintain consistency across different environments. Herd leverages Docker to create isolated development environments, ensuring that the specified PHP version is used without affecting the global PHP installation on your machine.

Installation

To get started with Laravel Herd, you need to install it as a development dependency in your Laravel project. Open your terminal and run the following command:

bash
composer require --dev nunomaduro/laravel-herd

Once the installation is complete, you can use the herd:install Artisan command to set up the necessary configuration files.

bash
php artisan herd:install

This command will create a herd.php configuration file in your project’s config directory.

Configuring Laravel Herd

Open the herd.php configuration file, and you’ll find an array where you can define the PHP versions required for your project. For example:

php
return [
'versions' => [
'7.4',
'8.0',
],
];

In this example, we have specified PHP 7.4 and 8.0 as the required versions. You can customize this array based on the PHP versions your project supports.

Switching PHP Versions

Once you have configured Laravel Herd, you can easily switch between PHP versions using the herd:switch Artisan command. For instance, to switch to PHP 8.0, run:

bash
php artisan herd:switch 8.0

Laravel Herd will handle the PHP version switching seamlessly, ensuring that your project uses the specified version.

Integrating Laravel Herd with Docker

Laravel Herd utilizes Docker containers to manage PHP versions. Therefore, you need to have Docker installed on your machine. If you don’t have Docker installed, you can download it from the official Docker website.

After installing Docker, you can start using Laravel Herd by running your Laravel development server within a Docker container. The herd:run Artisan command makes this process straightforward:

bash
php artisan herd:run

This command will launch your Laravel development server within a Docker container, using the PHP version specified in your herd.php configuration.

Automating PHP Version Switching

To streamline your development workflow, you can integrate Laravel Herd with your project’s scripts in the composer.json file. Add the following lines to the "scripts" section:

json
"scripts": {
"herd:switch": "php artisan herd:switch",
"herd:run": "php artisan herd:run",
"post-install-cmd": [
"@herd:switch 8.0",
"@herd:run"
],
"post-update-cmd": [
"@herd:switch 8.0",
"@herd:run"
]
}

In this example, the post-install-cmd and post-update-cmd scripts ensure that whenever you install or update your project dependencies, Laravel Herd switches to PHP 8.0 and runs the development server within the Docker container.

Conclusion

Managing PHP versions within a Laravel project can be challenging, especially when dealing with multiple projects that require different PHP versions. Laravel Herd simplifies this process by providing a convenient way to switch between PHP versions and ensuring consistency across development environments.

By following the installation steps, configuring the herd.php file, and integrating Laravel Herd with Docker and Composer scripts, you can enhance your Laravel development workflow. Take advantage of Laravel Herd to streamline your PHP version management and focus on building robust and efficient Laravel applications.