You want to get your hands dirty quickly with PHP (Laravel or Symfony) without having to install or configure it locally? I invite you to continue reading ...
Instead of trying to run things locally polluting our machine, I always prefer running things via isolated using docker if possible. That is also how I do it in this post.
Prerequisites
- A Shell (Linux / MacOS / WSL2 / VM)
- Docker
Initialization
PHP is used for developing web applications in the vast majority of cases. In order to avoid inventing the wheel, PHP frameworks are used. Probably the most popular one is Laravel. The second one is Symfony. Some prefer Laravel and others Symfony. In order to avoid making some people sad, we want to provide the ability to use either.
PHP Package Manager
With any, somewhat matured, language, we have a package manager to install, update and remove packages. The package manager for PHP is Composer.
Since we don't want to be dealing with installing things on our system and just use them, we need a way to run composer with docker.
Here is a shell function that can be used to run composer:
#!/bin/sh
composer() {
docker run -it --rm \
-u $(id -u ${USER}):$(id -g ${USER}) \
-v $(pwd):/workspace \
-w "/workspace" \
"composer:${COMPOSER_VERSION:-"2.2.6"}" \
$@
}
How To Install Symfony?
Now that we have the package manager, we can start with the symfony installation.
Both of the mentioned frameworks have "lighter" and "heavier" versions. This is how to install symfony in the lightest form:
_> composer create-project --prefer-dist symfony/skeleton "directory-name"
Installing the "full" version requires a couple more lines:
_> composer create-project --prefer-dist symfony/skeleton "app"
_> cd app
_> composer --no-interaction require webapp
How To Install Laravel?
With Laravel we have two size choices, Laravel (Full) or Lumen (Lite).
This is how we can install Lumen:
_> composer create-project --prefer-dist laravel/lumen "app"
Or to install the main Laravel framework we can run this:
composer create-project --prefer-dist laravel/laravel "app"
How To Make It Easy?
Now we don't want to be looking for these options every time we want to start a new project. Something that would be more user friendly is a command that does it for us while we just answer a few questions i.e. a "wizard".
Instead of posting a whole bunch of code in a blog post I prefer to share a link to the place most code resides at.
Please feel free to review the script.
The Rest Of The Story
- Before running the PHP application in a container environment, we first need an image that fulfills the requirements of both frameworks.
- After having a container image, we can use it in a docker-compose definition.
- Then we need a way to easily build, start, restart and stop the project.
- While running the application, we also need to be able to see the logs so we can debug problems when they occur (i know, it rarely happens 🤡).
- And if we want to develop anything remotely serious, we need an easy way to run tests.
Everything Put Together
If you want to use or review everything mentioned, you are invited to refer to this repository.
Hope this helps to get started quickly and easily with PHP.