Categories
Laravel

Laravel Development with Vessel (Docker)

Advertisements

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.

Categories
Laravel

5 of the best Laravel books

Advertisements

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.

Categories
Laravel

Social Network API update

Advertisements

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.

Categories
Laravel

Laracon US – Laravel 5.3 overview

Advertisements

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.

Categories
Laravel

Social Networking site – Starting point

Advertisements

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.

Categories
General Laravel

The beginning of our Laravel social network

Advertisements

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.

Categories
PHP Symfony

ReactPHP and Symfony

Advertisements

I started listening to a great Podcast series which talks a lot about different PHP and server technologies. Where on one of the later episodes, they talk about ReactPHP.

ReactPHP is a great asynchronous solution for PHP. 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 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 but also to merge it in with the Symfony framework. 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. I followed his quick tutorial on getting these 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 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 on the web address 127.0.0.1:1337. You can see this code within app/AppKernal.php. 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. Also, it would be a lot more straight forward to implement with Symfony and their HTTP requests instead. However, 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. However, 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.

Enjoy reading this blog? Please consider subscribing using the form below.

Categories
General

Moltin API

Advertisements

I want to introduce you guys to an awesome e-commerce API called Moltin. It takes out so much hard work when it comes to creating an e-commerce platform and I have been working with the API to try and create an open source Laravel e-commerce platform which utilises the Moltin API as the core part of the app.

I won’t go into details of my app just yet as it isn’t released yet, but I just wanted my readers to get excited about the potential of using Moltin as their e-commerce backbone.

Because the API is in JSON format, you can use it in any programming language which includes; PHP, Javascript, C and many others. They even have a nice tutorial on creating an iPhone app and utilising Apple pay within the app.

I think you guys would love it and think you should check it out. It also gives you a really good insight as to what I have in store for you guys.

Categories
Docker

How to containerize your PHP/NGINX docker project properly

Advertisements

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. You can skip to MariaDB for your database container and settings.

Dockerfile

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

NGINX Configuration

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

Categories
Laravel PHP

Using View Composers in Laravel 5.2

Advertisements

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.