Last time I got in touch with PHP development was 2004. There seems to change a lot of things in the past 17 years. Today I’m going to set up PHP and Laravel on my Mac. I know nothing about it and start with the very beginning…
So, why Laravel? I have to implement a small backend for my formapps. There are few requirements that should be met:
- REST interface to access backend from my apps
- user management and user roles
- PHP language, because I have a cheap PHP hosting
I’m aware of that it is very much work, if I start from scratch. In my case I’ll have to rely on some CMS system or PHP framework. After a short research I first looked at drupal. It’s a very popular open source CMS. I had a little bit experience with it in year 2004. Drupal has a lot of features but is too much of goodness for my requirements 🙂 I need something that is more lightweight. Then I talked to a friend of mine who is backend developer. He works with Laravel. He says, it’s a very cool modern framework to get quickly started with backend development. Well, why not give it a try?
Setting up environment
According to Laravel documentation, there are several ways how to start working with Laravel. The doku states: “…Sail provides a great starting point for building a Laravel application using PHP, MySQL, and Redis without requiring prior Docker experience…”. Ok, sounds well. What do I need for this approach?
- Install Docker desktop for Mac. Done!
- Install PHP for MAC. I must install PHP in version 7.4 because of dependencies..
brew install email@example.com
and no, it didn’t work immediatelly for me. I had to upgrade brew, export some brew stuff, then xcode-select –install and … I finally got it working, but I hate this command line stuff! Done!
- Install PHP composer for MAC. It’s like CocoaPods but for PHP. For installation just run the commands at https://getcomposer.org/download/ Done!
- additionally I moved the installed composer to the path, to install it globally and call it on console just as “composer” and not as “php composer.phar”
sudo mv composer.phar /usr/local/bin/composer
Starting with existing project
After everything is installed, we can start with the PHP-project itself. In my case I had already a laravel project from the friend of mine I mentioned above, however without Sail dependency…So I just opened the project directory and called two commands:
composer require laravel/sail –dev
php artisan sail:install
The latest command failed, because .env file could not be found… hm, in project directory I found .env.example file. So just renamed it to .env, assigned proper parameters and tried installation again. wow, It works!!! It turned out, that .env is the main configuration file in laravel. It should not be pushed to git, only the example version of it.
So, what next?
We have installed sail into existing laravel project. Great. My next questions are:
- how to use sail stuff?
- and how can I develop? I mean, which IDE? How can I debug in PHP? And how will code updates get into docker container?
Let’s investigate. Starting and stopping of the docker container with sail is easy. First create an alias:
alias sail='[ -f sail ] && bash sail || bash vendor/bin/sail’
… and then just:
“sail up” in your Terminal => now you can go to Safari and call localhost. Yeeeeh, it works! That means, my local project is running in docker… a kind of magic. Was my project, i.e. php code copied to docker container? I hope, we’ll find it out later.
As for IDE, I decided to try VisualStudioCode for MAC. It makes a good impression and is for free. I found a way how to use Xdebug with Laravel Sail:
Now I’m best equipped and am ready for Laravel.