Categories
Laravel PHP Programming

5 of the best programming books

Advertisements

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:

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

Categories
PHP

Local OAUTH – Curl: (60) SSL certificate : Unable to get local issuer certificate

Advertisements

I’ve seen a couple of questions regarding the error “Curl: (60) SSL certificate : Unable to get local issuer certificate” and thought I would post some steps on the best way of setting it up locally.

I use XAMPP as my local development environment, but this will work with WAMP or LAMP enviroments too.

(I know I should use Vagrant as my local environment, but I haven’t got round to getting it up and running just yet).

Firstly, we need to download an SSL certificate to use for our local set up, to do this, head over to Github and save the file. The best place to save it is in C:/xampp/apache/conf/ssl.crt. There is already a crt file here, but I have test that one and it doesn’t work. You can either overwrite that one, or keep the name “ca-bundle.crt”, I generally keep to the same name that way the other file is there in case you need it in the future.

Now you need to open up your php.ini file find the line “curl.cainfo” (if it isn’t there) add/change it to… curl.cainfo = “C:xamppapacheconfssl.crtca-bundle.crt”. Restart your Apache server and test it out using any Oauth API you are using or going to use for your projects.

This is the best and most permenant way to handle SSL within your local environment and I highly recommend it.

If you feel there is a better way or you need help, then please feel free to comment below.

Categories
Laravel PHP

Unique slug for news articles using Laravel’s unique valitation

Advertisements

Just recently at work, I had to change the way my news articles worked. We have a client who has Holiday Homes, and they have news articles per park. When I redeveloped the website in Laravel, I created a one to many relationship based on… “One park has many news articles”.

I really should have created a many to many relationship. There could be the same articles per park, but I was young and naive. Because I worked in this way, I needed to figure out a way where I could have the same or similar articles with the same article slug, but unique only to that park.

So say if I had a park in Cornwall, I could create an article which has the slug of “owners-update”. This slug could be used on another park which is in Devon, but the slug couldn’t then be used again in Cornwall as it has been done.

Add a similar rule to the one below in your create method. You will get the results which I was looking for…

slug' => 'required|unique:news,slug,NULL,id,park_id,' . $input['park_id'],

This rule will then ONLY apply to an article with the same park id.

Ref: Laravel Unique Rule