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.

If you enjoyed this article, please follow for more.

5 of the best Laravel books

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

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

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

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

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

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

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:

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).

If you would like to contribute then I would wholeheartedly appreciate any contributions. Pull requests or feedback on the project are more than welcome. I must warn you guys that it has literally just started. 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. You can see what I would like to be done, what needs testing and what has been completed.

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

…Continued

This Blogpost 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. 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.

Laracon US – Laravel 5.3 overview

The last two days I have been keeping an eye on the Laracon US live stream shown on StreamACon. While the first day of the stream didn’t go too well, the second days stream has been phenomenal.

The reason for this post however, is to talk about the new features in Laravel 5.3. Oh boy, has he put a lot in?

First off, he has removed a lot of unneeded folders. However, these can be placed back in by running the simple artisan commands for when they are actually needed by the user. This makes the framework look a whole lot cleaner and less scary for newcomers. I have seen time and time again where a newcomer will come into Reddit. They want to know all about every folder as soon as they can when it shouldn’t be like that. They need to learn to start small and grow the framework which is exactly what 5.3 has tackled.

Laravel Passport

Laravel Passport adds oAuth 2 and JWT tokens. A lot of people are already jumping on the “goodbye Dingo, hello Passport” bandwagon. However, I don’t think people should be too quick to say this. It looks to me as Passport could be the perfect companion for Dingo. Dingo was never meant as a authentication layer and is in fact a JSON API layer to help with JSON requests and responses. No where has Taylor said that Passport will be doing the same, he just said it adds oAuth 2 and JWT.

Laravel Mailable

This is a nice option for sending mail, it makes for a more expressive API design choice and helps tidy up the sending of mail. If you send a lot of Mail and have used the less expressive syntax, you can move to something like this…

Mail::to('laravel@example.com')->send(new ContactForm)

Within your ContactForm class, you can build up the request and then return a Blade view on the response.

Laravel Scout

This is a pretty exciting new feature which allows full text searching using Algolia (Taylor currently only has the Algolio drivers, but asks that the community adds more drivers).

php artisan scout:import App\Post

Running the above command will sync your Post model to Alogolia for indexing, you can then run searches like…

Post::search('Laravel')->get();

You can even paginate the responses…

Post::search('Laravel')->paginate();

Laravel Notifications

Laravel notifications enable you to notify you or a customer if something happens using Slack, SMS or email.

$this->line('Thank you for joining') ->action('Button Text', 'https://url.com') ->line('If you have any question, please reply to this email.') ->success();

Or if you have an error…

$this->line('We are sorry, but something went wrong...') ->action('Button Text', 'https://url.com') ->error();

VueJS

VueJS has been added as the default JS framework into the boilerplate of Laravel 5.3. This is easy enough to take out. Although it is very helpful if you want to use VueJS and even helps if you couldn’t decide between VueJS, Angular or ReactJS.

Laravel Summary

As you can see, Taylor had a lot to talk about and there’s bound to be something which has been missed. Taylor tried to squeeze as much in as possible, but I don’t think he had enough time to discuss everything in detail. We will have to wait a couple of weeks to see what the official release brings.

Social Networking site – Starting point

In my last post, I said how I would create a new open-sourced social networking website. I would to start out on the API.

To get us started, I am using the latest Lumen build. I will extend it with Dingo and JWT Tokens for authentication.

First and foremost, I feel that the database and API endpoints are the most important parts of our application and is what I will begin to build out.

I will start with the basics, user profile, comments, friendship etc and then build out a nice messaging system. I will be looking to implement something like Pusher for a messaging system. It looks like a nice API, and with the updates on Laravel 5.3, it seems like it could be a nice system.

After building out the API, I will then open-source it for others to contribute to. That will help me out with getting the actual site implemented and interacting with the API.

Again, the basics will be implementation first. When I am happy with the results, I will push it out into the open for others to view and contribute to.

If you would like to show some interest, then please comment below and even share on Facebook and/or Twitter and get the word out. I really hope we can build out something amazing.

The beginning of our Laravel social network

A long while back, I said I would love to create and document making a social networking website using the Laravel framework, well that hasn’t happened, like at all.

Instead, I have started to build an API based in Lumen which I hope to open-source once the main functionality has been built into it.

Once the API starts to take shape, I would then like to build an open-source website which interacts with the API and then also open source the website.

Developers will then have a choice, they can either connect to whatever data I have on my server with the website repo. Or they can clone both the API and website and build out whatever it is they need for their own website.

Hopefully, this will be made clear when it becomes open-sourced and more people contribute to it. As I develop things further, I will throw everything up on my Blog to keep everyone who wants to follow it up to date.

Using View Composers in Laravel 5.2

I have started using Laravel’s View Composers which come in handy when you have a navigation bar that constantly needs certain data. For example, you have an e-commerce store and you need to poll how many items a user has in their basket on pretty much every page they move onto. Or, you need a login/register button when a user isn’t logged in and then a profile options button when they are.

You could be really lazy and add the same code/query into every single controller, which yes, that will work, but then that’s not DRY (Don’t Repeat Yourself). To fix this, we can complete a couple of simple steps and have one bit of code which will constantly be in use every time a certain partial is called.

If you currently have a layout/template view which has the navigation menu already written in, then you will need to move this out into a partial. Within your “resources > views” directory, you will need to create a new directory called “partials”. Once done, you will need to create a “nav.blade.php” file and paste in your navigation code. It should look something like this…

<nav class="navbar navbar-default">
  <div class="container-fluid navbar-header">
  <button class="navbar-toggle collapsed" type="button" data-toggle="collapse"
  data-target="#bs-example-navbar-collapse-1" aria-expanded="false" class="sr-only">
    Toggle navigation
    <br />
    <br />
    <br />
    <br />
    </button><br />
    <a class="navbar-brand" href="#">Brand</a><br />
  </div>
  <p><!-- Collect the nav links, forms, and other content for toggling --><br /></p>
  <div id="bs-example-navbar-collapse-1" class="collapse navbar-collapse">
    <ul class="nav navbar-nav">
      <li style="list-style-type: none;">
        <ul class="nav navbar-nav">
          <li class="active">
            <a href="#">Link <span class="sr-only">(current)</span></a>
          </li>
        </ul>
      </li>
    </ul>
    <ul class="nav navbar-nav">
      <li style="list-style-type: none;">
        <ul class="nav navbar-nav">
          <li><a href="#">Link</a></li>
        </ul>
      </li>
    </ul>
    <ul class="nav navbar-nav">
      <li style="list-style-type: none;">
        <ul class="nav navbar-nav">
          <li class="dropdown">dropdown
            <ul class="dropdown-menu">
              <li style="list-style-type: none;">
                <ul class="dropdown-menu">o</ul>
              </li>
            </ul>
            <p> </p>
            <ul class="dropdown-menu">
              <li style="list-style-type: none;">
                <ul class="dropdown-menu">
                  <li><a href="#">Action</a></li>
                </ul>
              </li>
            </ul>
            <ul class="dropdown-menu">
              <li style="list-style-type: none;">
                <ul class="dropdown-menu">
                  <li><a href="#">Another action</a></li>
                </ul>
              </li>
            </ul>
            <ul class="dropdown-menu">
              <li style="list-style-type: none;">
                <ul class="dropdown-menu">
                  <li><a href="#">Something else here</a></li>
                </ul>
              </li>
            </ul>
            <ul class="dropdown-menu">
              <li style="list-style-type: none;">
                <ul class="dropdown-menu">
                  <li class="divider" role="separator"> </li>
                </ul>
              </li>
            </ul>
            <p></p>
            <ul class="dropdown-menu">
              <li style="list-style-type: none;">
                <ul class="dropdown-menu">
                  <li><a href="#">Separated link</a></li>
                </ul>
              </li>
            </ul>
            <p></p>
            <ul class="dropdown-menu">
              <li style="list-style-type: none;">
                <ul class="dropdown-menu">
                  <li class="divider" role="separator"> </li>
                </ul>
              </li>
            </ul>
            <p></p>
            <ul class="dropdown-menu">
              <li style="list-style-type: none;">
                <ul class="dropdown-menu">
                  <li><a href="#">One more separated link</a></li>
                </ul>
              </li>
            </ul>
            <p></p>
            <p></p>
          </li>
        </ul>
      </li>
    </ul>
    <p></p>
    <p></p>
    <form class="navbar-form navbar-left" role="search">
      <div class="form-group">
        <input class="form-control" type="text" placeholder="Search" />
        <br />
      </div>
      <p>
        <button class="btn btn-default" type="submit">Submit</button>
        <br />
      </p>
    </form>
    <p></p>
    <ul class="nav navbar-nav navbar-right">
      <li style="list-style-type: none;">
        <ul class="nav navbar-nav navbar-right">
          <li>
            <button class="btn btn-primary navbar-btn" type="button">
              Basket
              <span class="badge"></span>
            </button>
        </li>
        </ul>
      </li>
    </ul>
    <p></p>
    <p></p>
  </div>
  <p><!-- /.navbar-collapse --><br /></p>
  <p><!-- /.container-fluid --></p>
</nav>

Including it in your Blade template

Next, in your layout or template blade, you can add…

<?php
require('partials.nav');

This will include your navigation file.

Now for setting up the View Composer, firstly we need to set up a ServiceProvider (for the sake of this tutorial I will show an example for counting a basket)…

<?php
...
public function boot() {
    view->composer(
      'partials.nav',
      'App\Http\ViewComposers\BasketComposer'
    );
}

The service provider will run whenever you use the nav partial and in the next attribute, it will use your BasketComposer which should look something like this…

<?php
class BasketComposer
{
  public function __construct()
  {
    $cart = Cart::Contents();
  }

  /**
   * Bind data to the view.
   *
   * @param View $view
   * @return void
   */
  public function compose(View $view)
  {
    $view->with('cart', $this->cart['result']['total_items']);
  }
}

Obviously, depending on your API or personal use, your code may vary here, but you should get an idea of how this is working. Every time you need the nav partial, the view will render with your given items. You can also pass this to many views at once which makes it work really well. I also believe you can pass many different constant data sets into a single view, so you could in theory also add a user constant for login and edit profile data.

For more information, feel free to visit Laravel’s documentation on View Composers and this nice Laracasts video which helped me figure out how to use it correctly.

If you have a unique use case, please let me know and we can discuss as a community just how useful these View Composers are.

Using Vagrant and Homestead In as Little Steps as Possible (Per Project Install)

I want to show you guys just how quick and easy it easy to get a Laravel project up and running alongside Vagrant/Homestead.

1. Head over to your chosen directory and run something like…

composer create-project laravel/laravel PROJECTNAME

or if you have the Laravel installer…

laravel new PROJECTNAME

This will create a Laravel project directory with everything installed inside the given PROJECTNAME folder.

2. When you cd into your PROJECTNAME directory, you can run the following…

composer require laravel/homestead --dev

This will add Homestead into your Composers require attribute under the dev tag, which means it will only be used for development purposes.

3. You then need to run…

vendor/bin/homestead make

This will create the VagrantFile and Homestead.yaml file to be used for Vagrant.

The Homestead.yaml file is used to configure any settings to be used in your virtual machine. If you need more advanced settings, then check out the Homestead docs, which shows how to add Blackfire and Crons etc.

4. Run vagrant up

This will get your virtual machine up and running, once running, you can visit the site through 192.168.10.10, it should now be ready for dev work.


If you’re like me and don’t like working on the IP address, you have a couple of options. Firstly, you can add a hostname manually in your computers hosts file. Something like…

192.168.10.10 homestead.app

You can change “homestead.app” to whatever host name you prefer. The better and more elegant way is to add a Vagrant plugin called “Vagrant Hosts Updater”…

vagrant plugin install vagrant-hostsupdater

This will automatically edit your hosts file using the settings inside your Homestead.yaml file. You can change the hostname by editing the following attributes; hostname, name and sites: -map to your chosen domain name e.g. example.app or example.dev etc.

Mac and Linux users will also be asked for their admin passwords, whilst in Windows, you will need to run the Command Line as an administrator.


Here are some fantastic commands you should learn as well as documentation from other resources to learn more about Vagrant and Homestead environments.

upBoots up the machine and fires provision
reloadReboots the machine
provisionRuns only the provisioner(s)
initInitializes a new Vagrantfile based on specified box url
haltTurns off the machine
destroyDestroys the virtual machine
suspendSuspends execution
resumeResumes execution
sshLogs in via ssh (no password is required)
statusShows info about the current Vagrant environment
global-statusShows info about all Vagrant’s environments

Homestead Documentation

Vagrant Documentation

Awesome book about Vagrant written by Erika Heidi