Laravel Development with Vessel (Docker)

Getting started with Vessel

I have started using Vessel for my local development as it makes starting and developing a project so much easier. It seems more lightweight than Homestead and the start-up time is much quicker than Homestead.

No PHP or Composer? No problem

If you don’t have PHP7 or Composer installed on your machine, you can follow these instructions to help get you started. It will install PHP7 and Composer environments with Docker and allowing you to create a Laravel project and install Vessel as a package.

I also add /docker folder into my .gitignore as vessel will create this upon initialisation. You could also ignore the vessel and docker-compose.yml files as well, but I enjoy keeping it within my projects.

Bash Commands

Vessel is a bash file and allows you to run any artisan, yarn or npm command of your choosing which makes it easier rather than ssh’ing into a homestead box or running the exec command.

Only for development environments

It has fantastic documentation and should allow you to get running within 10 minutes. The only downside to Vessel is that it’s only for development work and not production. So don’t expect Lets Encrypt certificates or deployment strategies with this.

Want to learn more about going from Development to Deployment? I will be live streaming a learning event Laravel: from development to deployment

If you enjoyed this article, please follow for more. Alternatively, you can Buy me a coffeeBuy me a coffee

 

Post your RSS feed items to Facebook and Twitter

If you have a Blog in a CMS which isn’t WordPress, Drupal or Joomla, then you will likely not have some automated way to post your new blog posts over to Twitter or Facebook.

I have started using [IFTTT](https://ifttt.com) to automate a lot of my workflow, but I couldn’t find any applets which would automatically post my feed items to Facebook or Twitter. I then went and created my own RSS applets, but hard coded the URL as it was just for my personal use. I then thought to myself “I bet other people may find this useful”. So, I decided to create similar applets, but you can set your own RSS feed URL and have it post to your Twitter page, or you can post it to a Facebook business page.

Check out the [link here](https://ifttt.com/makers/checkeredmichael) and let me know what you guys think.

Of course, it doesn’t have to be just your blog items, it could be anyones blog items. If you would like them posted elsewhere, then please let me know and I can create the applet for you.

5 of the best Laravel books

Cross development problems

Laravel – Up and Running: A Framework for Building Modern PHP Apps
Amazon.com | Amazon.co.uk

Matt Stauffer

What sets Laravel apart from other PHP web frameworks?

Speed and simplicity, for starters. This rapid application development framework and its vast ecosystem of tools let you quickly build new sites and applications with clean, readable code. With this practical guide, Matt Stauffer-a leading teacher and developer in the Laravel community-provides the definitive introduction to one of today’s most popular web frameworks. The book’s high-level overview and concrete examples will help experienced PHP web developers get started with Laravel right away. By the time you reach the last page, you should feel comfortable writing an entire application in Laravel from scratch.

Dive into several features of this framework, including: Blade, Laravel’s powerful, custom templating tool Tools for gathering, validating, normalizing, and filtering user-provided data Laravel’s Eloquent ORM for working with the application’s databases The Illuminate request object, and its role in the application lifecycle PHPUnit, Mockery, and PHPSpec for testing your PHP code Laravel’s tools for writing JSON and RESTful APIs Interfaces for file system access, sessions, cookies, caches, and search Tools for implementing queues, jobs, events, and WebSocket event publishing Laravel’s specialty packages: Scout, Passport, Cashier, Echo, Elixir, Valet, and Socialite

Laravel 5.1 Beauty: Creating Beautiful Web Apps in Laravel 5.1
Amazon.com | Amazon.co.uk

Chuck Heintzelman

Leverage the power of Laravel 5.1 to create a simple, clean and beautiful blogging application and the administration area required to maintain the blog. This book goes through the process of designing, creating, and coding a real-world application using Laravel. You’ll learn about: * Installing Laravel 5.1 * Using Homestead * Using Elixir * Database Migrations and Seeding * Form Requests * Views (Blade templates) * Authentication * And much, much more!

Build APIs You Won’t Hate: Everyone and their dog wants an API, so you should probably learn how to build them
Amazon.com | Amazon.co.uk

Phil Sturgeon (Author), Laura Bohill (Designer)

API development is becoming increasingly common for server-side developers thanks to the rise of front-end JavaScript frameworks, iPhone applications, and API-centric architectures. It might seem like grabbing stuff from a data source and shoving it out as JSON would be easy, but surviving changes in business logic, database schema updates, new features, or deprecated endpoints can be a nightmare.

After finding many of the existing resources for API development to be lacking, Phil learned a lot of things the hard way through years of trial and error. This book aims to condense that experience, taking examples and explanations further than the trivial apples and pears nonsense tutorials often provide.

By passing on some best practices and general good advice you can hit the ground running with API development, combined with some horror stories and how they were overcome/avoided/averted. This book will discuss the theory of designing and building APIs in any language or framework, with this theory applied in PHP-based examples.

Learning Laravel’s Eloquent
Amazon.com | Amazon.co.uk

Francesco Malatesta

Learning Laravel’s Eloquent starts off by taking you through setting up your first project and guiding you in creating a perfect Laravel environment. You will learn how to build the right database structure with the Migrations system and the Schema Builder class. Next, you will be introduced to the main element of Eloquent: the model. After treating the model as a single, isolated entity, you will learn how to create relations between them. You will be taken through organizing, filtering, and sorting your data with collections. You will then learn to enhance an application with new features using events and by creating new observers. Towards the end of the book, you will discover how to install, configure, and use the Eloquent ORM without Laravel. The book concludes by walking you through how to deal with complex problems and build advanced and flexible systems.

Design Patterns in PHP and Laravel
Amazon.com | Amazon.co.uk

Kelt Dockins

Learn each of the original gang of four design patterns, and how they are relevant to modern PHP and Laravel development. Written by a working developer who uses these patterns every day, you will easily be able to implement each pattern into your workflow and improve your development. Each pattern is covered with full examples of how it can be used.

Too often design patterns are explained using tricky concepts, when in fact they are easy to use and can enrich your everyday development. Design Patterns in PHP and Laravel aims to break down tricky concepts into humorous and easy-to-recall details, so that you can begin using design patterns easily in your everyday work with PHP and Laravel.

This book teaches you design patterns in PHP and Laravel using real-world examples and plenty of humor.

 

5 of the best programming books

Cross development problems

## Working Effectively with Legacy Code
Amazon.com | Amazon.co.uk

![Working Effectively with Legacy Code](/storage/app/media/0131177052.jpg)

Michael C. Feathers

The average book on Agile software development describes a fairyland of greenfield projects, with wall-to-wall tests that run after every few edits, and clean & simple source code.

The average software project, in our industry, was written under some aspect of code-and-fix, and without automated unit tests. And we can’t just throw this code away; it represents a significant effort debugging and maintaining. It contains many latent requirements decisions. Just as Agile processes are incremental, Agile adoption must be incremental too. No more throwing away code just because it looked at us funny.

Mike begins his book with a very diplomatic definition of “Legacy”. I’l skip ahead to the undiplomatic version: Legacy code is code without unit tests.

Before cleaning that code up, and before adding new features and removing bugs, such code must be de-legacified. It needs unit tests.

To add unit tests, you must change the code. To change the code, you need unit tests to show how safe your change was.

The core of the book is a cookbook of recipes to conduct various careful attacks. Each presents a particular problem, and a relatively safe way to migrate the code towards tests.

Code undergoing this migration will begin to experience the benefits of unit tests, and these benefits will incrementally make new tests easier to write. These efforts will make aspects of a legacy codebase easy to change.

It’s an unfortunate commentary on the state of our programming industry how much we need this book.

Amazon.com | Amazon.co.uk

## Clean Code
Amazon.com | Amazon.co.uk

![Clean Code](/storage/app/media/0132350882.jpg)

Robert C. Martin

An extremely pragmatic method for writing better code from the start, and ultimately producing more robust applications.

Amazon.com | Amazon.co.uk

## Refactoring – Improving the Design of Existing Code
Amazon.com | Amazon.co.uk

![Refactoring – Improving the Design of Existing Code](/storage/app/media/0201485672.jpg)

Martin Fowler, Kent Beck

Users can dramatically improve the design, performance, and manageability of object-oriented code without altering its interfaces or behavior. “Refactoring” shows users exactly how to spot the best opportunities for refactoring and exactly how to do it, step by step.

Amazon.com | Amazon.co.uk

## Design Patterns
Amazon.com | Amazon.co.uk

![Design Patterns](/storage/app/media/0201633612.jpg)

Ralph Johnson, Erich Gamma, John Vlissides, Richard Helm

Capturing a wealth of experience about the design of object-oriented software, four top-notch designers present a catalog of simple and succinct solutions to commonly occurring design problems. Previously undocumented, these 23 patterns allow designers to create more flexible, elegant, and ultimately reusable designs without having to rediscover the design solutions themselves. The authors begin by describing what patterns are and how they can help you design object-oriented software. They then go on to systematically name, explain, evaluate, and catalog recurring designs in object-oriented systems. With Design Patterns as your guide, you will learn how these important patterns fit into the software development process, and how you can leverage them to solve your own design problems most efficiently. Each pattern describes the circumstances in which it is applicable, when it can be applied in view of other design constraints, and the consequences and trade-offs of using the pattern within a larger design. All patterns are compiled from real systems and are based on real-world examples. Each pattern also includes code that demonstrates how it may be implemented in object-oriented programming languages like C++ or Smalltalk. 0201633612B07092001

Amazon.com | Amazon.co.uk

## Patterns of Enterprise Application Architecture
Amazon.com | Amazon.co.uk

![Patterns of Enterprise Application Architecture](/storage/app/media/0321127420.jpg)

Martin Fowler

This volume is a handbook for enterprise system developers, guiding them through the intricacies and lessons learned in enterprise application development. It provides proven solutions to the everyday problems facing information systems developers.

Amazon.com | Amazon.co.uk

Related:

* [Top 5 Laravel Books](/post/top-5-laravel-books)

Most helpful GIT command…

This little command is a lifesaver and will save you so much time when switching and merging previous branches in your GIT workflow.

`git checkout -`

`git merge -`

See the little minus symbol at the end? That basically tells GIT you want to checkout the previous branch, or merge the previous branch. This will also work with rebasing and any other command which uses branches.

Say you were in master branch and wanted to switch to that new PR request your colleague has submitted, you would do something like this…

`git checkout new-feature`

You look at the code and test and all looks good, so you want to switch back to master and then merge in the new feature, this will be super easy and you won’t need to remember the branch names…

Switch back to master branch `git checkout -` then merge that new feature in `git merge -`. How much easier was that? You guys can thank me later when you have finished merging in all those PRs. 😉

Have any other commands you find helpful? Let us know in the comments below.

Social Network API update

Last time I updated you guys on this, I only had a list of ideas and how it would initially pan out. I have now started the project and published the repo onto GitHub (which can be [found here](https://github.com/Michael-Brooks/social-api)).

If you would like to contribute then I would whole heartedly appreciate any contributions, pull requests or feedback on the project. I must warn you guys that it is literally just started, but so far I have implemented Dingo and JWT tokens as part of the API.

You can also find my todo list publicly available through [Trello](https://trello.com/b/gGSRijjW/social-app-api). You can see what I would like to be done, what needs testing and what has been completed. If you think you could add to the list, then please send a request to my email [email protected] and I will add you.

I’m pretty excited about this project and I’m really keeping my fingers crossed that we can create something truly amazing here especially with such an amazing Laravel/PHP community.

## …Contininued ##

This Blog post was supposed to be out a while ago, and I have made a lot of progress on this project. I found a great starting project which has Dingo API, JWT tokens and CORS all setup out of the box with Lumen. This project can be [found here](https://github.com/0plus1/lumendingojwtapi). I now have JWT setup with user registration/login.

I also have status updates and friend request approval/ignore. There is still a lot to do and so much I need to figure out on this API, but if you would like to contribute, then please feel free to take a look and fork the project [here](https://github.com/Michael-Brooks/social-api).

Create a Raspberry Pi 3 Bluetooth tag

I found this [MagPi Tutorial](https://www.raspberrypi.org/magpi/create-a-raspberry-pi-3-bluetooth-tag/) to be a really nice starting project for my newly purchased Pi 3. However, I did run into a few problems which I will address in this Blog post. You can pretty much follow either this post, or the MagPi one, but my post will address any issues I had when following their tutorial.

So to start, they requested I find the Mac address for both my phone and PC. I followed their advice for looking on my phone, but the Mac address was long and full of dashes which looking at the Python script is not what they want. Instead, I used the Pi to search for my phone on Bluetooth and find the Mac address using the following in Terminal…

“`
sudo bluetoothctl
agent on
scan on
“`

This will use the Bluetooth module, turn Bluetooth on and then search for Bluetooth devices. Next, connect your phone to your Pi and copy the Mac address then paste into a text editor for later.

To pair your selected device type…

“`
pair XX:XX:XX:XX:XX:XX
“`

The x’s will be your phone’s mac address.

Now we need to grab your PC’s mac address, you can do this through your router. My router’s IP is 192.168.0.1, but your’s might be different, log into your router and view your connected devices where you should find your PC’s chosen Mac address. Keep this safe for later on.

Now we have our Mac addresses, we need to install all relevant software. I had trouble trying to install ‘pybluez’, so I will now tell you how I overcame this issue.

Firstly, we need to install Python dev and a Bluetooth library by typing the following into Terminal…

“`
sudo apt-get install python-dev
sudo apt-get install libbluetooth-dev
“`

Then we can install the packages from MagPi…

“`
sudo pip install pybluez
sudo pip install wakeonlan
“`

Now for the Python script, if you check out the original post, you will see this differs slightly as this code works perfect for me…

“`
#!/usr/bin/python

import time
import bluetooth
from wakeonlan import wol

phone = “ff:ff:ff:ff:ff:ff”

def search():
devices = bluetooth.discover_devices(duration = 5, lookup_names = True)
return devices

while True:
results = search()

for addr, name in results:
if addr == phone:
wol.send_magic_packet(‘ff:ff:ff:ff:ff:ff’)

time.sleep(20)
“`

To get this working automatically on reboot, we need to add the following to /usr/profile, to edit /usr/profile type…

“`
sudo nano /usr/profile
“`

Then right at the very bottom of this script we need to add the following…

“`
sudo python /home/py/bluetooth_tag.py &
“`

That is everything, be sure to run python on the script beforehand to ensure your phone can wake up your PC, when it does, the script should carry on working after a reboot.

If you have any questions regarding this tutorial, or think it could be improved, then please let me know.

ReactPHP and Symfony

I started listening to a [great Podcast series](https://phptownhall.com/episode-49-async/) which talks a lot about different PHP and server technologies and on one of the later episodes, they talk about [ReactPHP](https://reactphp.org/).

[ReactPHP](https://reactphp.org/) is a great asynchronous solution for PHP and it offers non-blocking async which is fantastic and can be easily compared to NodeJS (although we won’t be doing that here).

I have now started exploring the technology and what it is capable of, one of my great project ideas is to have it listen on [Twitter’s Streaming API](https://dev.twitter.com/streaming/overview) and post coordinates of a certain search query. As it picks up new posts, it will cache them on a database server for historical reasons. This would be a great way to visualise certain stories or World events as they happen.

To start this project off, I decided to not only use [ReactPHP](https://reactphp.org/), but also to merge it in with the [Symfony framework](https://symfony.com/). This means, I can use ReactPHP within an MVC framework and have my code better organised.

A quick Google pointed me to this article [Super Speed Symfony](https://gnugat.github.io/2016/04/13/super-speed-sf-react-php.html). I followed his quick tutorial on getting these technologies working alongside one another (he had a mistake in his code which I quickly fixed) and now I was ready to get to work.

The barebones framework can be found on my Github for your personal use and exploration [here](https://github.com/Michael-Brooks/react-symfony) as the original poster had not thought of doing this himself. I thought I would take the lead so others can use it and help in the future.

If you clone my repo, you will be able to run in the project root…

“`
php web/app.php
“`

This will start up a web server running [ReactPHP](https://reactphp.org) on the web address 127.0.0.1:1337. You can see this code within app/AppKernal.php where it converts React’s requests into a Symfony request so you can carry on using Symfony as needed. You should then be able to constantly listen in on requests and create some pretty nice async apps.

Some of you may be wondering why I didn’t try going down the Laravel route as I’m a huge fan of that framework. However, I felt like it wouldn’t fit in with how Laravel deals with Http requests, and it would be a lot more straight forward to implement with Symfony and their Http requests instead. Although I could be wrong and would love for someone to prove it if I am.

This is it for my post on this subject for now, but when I have a more solid app, I will be posting it here along with any tutorials to help others out. I hope this has helped others and I would love it if you shared your ReactPHP projects.

How to containerize your PHP/NGINX docker project properly

This evening, I had myself a little challenge. I would turn my two containers (MariaDB and my own built container) into more containers, but using the power of Docker’s official container repos. E.g. I would have 3 containers in total PHP7-FPM, NGINX and my already created MariaDB.

If you don’t know my steps I previously created, you can check it out [here](https://michaelbrooks.co.uk/post/hosting-your-site-digital-ocean-and-docker) and skip to MariaDB for your database container and settings.

First off, I used the PHP container and followed instructions to have a PHP7-FPM container. You can see below for the Docker file, just copy it and run the command below…

“`
docker build -t php7 .
docker run -d –name php7 php7
“`

This will build a php7 Image based on the PHP7-FPM container and add the relevant system, PHP and PECL extensions needed. Please bear in mind that this is for my OctoberCMS app and you may need different extensions. If this is the case, you will need to research and find the correct extensions for your project.

Now we have PHP7-FPM, we also need our trusty old proxy server NGINX.

“`
docker run -p 80:80 -p 443:443 –name nginx -v “$PWD”:/PATH/TO/PROJECT:ro -d nginx
“`

To follow the next steps, we will need to temporarily stop our nginx server and start it up again once we’re finished…

“`
docker stop nginx
“`

This will create our NGINX container using the ports usual ports including 443 for our secure port and share our project to the container as read-only (:ro).

Now for our SSL certificates. Run the following code and follow the steps which pop up.

“`
sudo docker run -it –rm -p 443:443 -p 80:80 –name letsencrypt
-v “/etc/letsencrypt:/etc/letsencrypt”
-v “/var/lib/letsencrypt:/var/lib/letsencrypt”
quay.io/letsencrypt/letsencrypt:latest auth
docker start nginx
“`

If you have followed along so far, then we have almost finished, we now need to copy our SSL directory to NGINX which will share the SSL directory as read-only.

“`
docker cp /etc/letsencrypt/live/DOMAINNAME/ nginx:/etc/letsencrypt/live/DOMAINNAME:ro
“`

Finally, we can create a default.conf file and copy it into the correct nginx directory.

From my GIST above, I redirect all non secure requests over to a secure connection and as I have 0 subdomains, I redirect them all to my main domain name. You can edit as you see fit, but it should give you a good idea.

Please note the fastcgi_pass PHP_FPM_IP:9000; section, in order to get your PHP_FPM_IP you will need to run the following…

“`
docker network inspect bridge
“`

Looking for the php7 container, you should see something similar to…

“`
“Containers”: {
“5bdf47e8153e713281ef59defe394f32b6172ba80ea626898d1566daf3541322”: {
“Name”: “nginx”,
“EndpointID”: “61de2b82a696984286a0b0e912e2c869bf755afa4e74e246182eeca1ace8412c”,
“MacAddress”: “02:42:ac:11:00:02”,
“IPv4Address”: “172.17.0.2/16”,
“IPv6Address”: “”
},
“a814706d03e4583bf061229bbb8c872d5ccd70d1e8b38a28a8dd80b984192027”: {
“Name”: “mariadb”,
“EndpointID”: “1d33b48beed9b7a43ea48d8d0b89811d0978f4af4a840b1d60ce2b4125bc6fb4”,
“MacAddress”: “02:42:ac:11:00:03”,
“IPv4Address”: “172.17.0.3/16”,
“IPv6Address”: “”
},
“fe46b9bcc689ba7fce47abe73f870408876b1b9c84094d3763fbe9ff8b601f4a”: {
“Name”: “php7”,
“EndpointID”: “c35329419ed8652ac23cfba7f9808a27d248237f14cd1d14d5668e9c2069e310”,
“MacAddress”: “02:42:ac:11:00:04”,
“IPv4Address”: “172.17.0.4/16”,
“IPv6Address”: “”
}
},
“`

As you can see from the code above, my IP is… 172.17.0.4 so I would place that into my default.conf like…

“`
fastcgi_pass 172.17.0.4:9000;
“`

Now for copying the default.conf file…

“`
docker cp default.conf nginx:/etc/nginx/conf.d/default.conf
docker restart nginx
“`

This will copy your file over to the correct location and a quick restart will ensure the new configuration has been loaded in.

If you are seeing a “502 Bad Gateway” then you will need to go into your php container and edit the file pemissions as follows…

UPDATE: If you pull down my latest Dockerfile, you will no longer need to perform these chown and chmod steps as it now does it inside of the Dockerfile.

“`
docker exec -it php7 bash
chown -R www-data public
cd public
sudo chmod -R gu+w storage
sudo chmod -R guo+w storage
“`

This should sort out your folder permissions and now you can drink a cup of tea and enjoy all of your hard work.

If you have any problems or would like to suggest anything, please feel free to comment below.

Hosting your site With Digital Ocean and Docker

I want to share with you how I am hosting my website with [Digital Ocean](https://m.do.co/c/d5aca73b366f) (hosting company) and [Docker](https://docker.com) (places your app in containers).

First off, I created my droplet on DO and used the docker image from the list of “One-click Apps”. The droplet was the $10 a month size as I don’t currently need anything more. (If you want to try Docker for a month or two then [click here](https://m.do.co/c/d5aca73b366f)).

After going through the setup process, I accessed my server through SSH and downloaded the Ubuntu image.

“`
docker pull ubuntu
“`

Once I have downloaded the Ubuntu image, I created my own container. I know I could have pulled in the [php-fpm](https://hub.docker.com/_/php/) container (and I did), I couldn’t quite work out how I was supposed to get it working properly. So while this next part was really tedius, it has taught me a valuable lesson.

Onto the command for creating my container…

“`
docker run -tid -p 80:80 -p 443:443 -v /home/project/public:/home/project/public –name CONTAINERNAME
“`

Now my container is up and running and sharing my data with the server. I then mount the container by running…

“`
docker attach CONTAINERNAME
“`

Once in, I begin to install PHP 7, NGINX, PHP-FPM and everything else I need for hosting my site (the list is long as the Ubuntu image is barebones). I then add my server block info for http and https rules as well as subdomain redirects.

Once finished, I press…

“`
ctrl+p ctrl+q
“`

This will detach from your continer, but won’t stop any services. I would recommend you never type “`exit“` within the container if you want to keep it running along with it’s services.

Now we need a database container, I user MariaDB and followed the documentation from their [Docker Hub](https://hub.docker.com/_/mariadb/) page.

“`
docker run -p 3306 –name some-mariadb -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mariadb:tag –character-set-server=utf8mb4 –collation-server=utf8m4bd
“`

This will create a container for your MariaDB database, you can then attach yourself to the container and create your personal database using the usual MySQL commands.

Now quit your database and detach from the container with…

“`
ctrl+p ctrl+q
“`

The run…

“`
docker network inspect bridge
“`

It will list your containers and display your IP address, you need to look out for your MariaDB container and take not of your IP address. You can then edit your database configuration file and where it says DB_HOST, place your IP here and along with your username/password details, it should connect.

Make sure to either run any migrations or export/import your database and your website is now fully up and running on Docker instances.

As I become more acquainted with the behemouth that is Docker, I will create more tutorials with regards to what it is is I wanted to accomplish and how I did it.