PHP Destruct Magic Method

What does it do? The destruct function is called when the following occurrs; Whenever a class is deleted When the object falls out of scope When the script ends Why would you use it? If you are removing a class from code and you no longer require its functionality it would be best to tie up all your loose ends. For example you may have a class that opens a stream, if there is life yet to to be lived in the script, make it more efficient by closing the file stream in the destruct method.
Read more

PHP Get Magic Method

What is does The get magic method is called if the property that you are requesting does not exist on the class. Why would you use it Similar to call() and callStatic() if the property you are looking for does not exist this is called instead, in here you can add logic such as; $someClass = new SomeClass(); $someClass->aProperty; public function __get( $key ) { return 'no property found for ' .
Read more

PHP To String Magic Method

What does it do? The to string magic method is used to specify how you would like the object to react when used as a string. Why would you use it? If you want to log or print out information on the class in a quick and easy way or avoid errors incase the object is used as a string. class User { protected $name; protected $age; protected $location; public function __construct($name, $age, $location) { $this->name = $name; $this->age = $nage; $this->location = $location; } public function __toString() { return 'User Information: name: ' .
Read more

Redirecting with Nginx

Some Common Usages Here are some common redirects that you may need to implement with Nginx Redirect From www.domain1.com to www.domain2.com server { server_name www.domain1.com; rewrite ^/$ http://www.domain2.com redirect; // redirect = temporary rewrite ^/$ http://www.domain2.com permanent; // redirect = permanent } Redirect url on old domain to new domain server { server_name www.olddomain.com; rewrite ^/oldlocation$ http://www.newdomain.com/newlocation redirect; // redirect = temp rewrite ^/another-location$ http://www.
Read more

Sending Laravel Internal Requests

Use Case Whilst writing a unit test I came across the need to send an internal Laravel request. I needed to do this for two reasons, one to test the full life cycle of the request, instead of the individual classes that the request would go through, to make sure they all worked together. Two, i needed the test to be fast so I wanted to avoid booting up an entirely new application instance.
Read more

The Nginx Map Function

What is the nginx map function? Nginx Map map allows you to mass assign a input variable to a certain outcome variable. The below runs through all the redirects and checks if the current url is among the ones to redirect, if it is a variable called $new_uri is defined, further down we check if the variable is defined, and redirect if so. map $uri $new_uri { /old.
Read more

The Open Closed Principle - SOLID

A Brief Explanation Today we are looking at the open closed principle. This principle attempt to make you write code which is easier changed and modified at a later date by touching as little as possible. You may know the common scenario where during a client meeting you will hear the dreaded sentence “I want it to work like this now”, and in these situation you may here the common developer excuse “It wasn’t built like that” or “Thats not the way its being developed”.
Read more

The Single Responsibility Principle - SOLID

A Brief Explanation The Single Responsibility Principle will help you create DRY, modular and easier to maintain code. In short the principle states that each class should have a single responsibility, one job. With that in mind lets get started! In Practice Please study the controller class below; class UserController { public function allUsers() { $allUsers = User::all(); return view('users.index')->with(['users' => $allUsers]); } public function notifyUser ($userId) { $user = User::find($userId); Mail::send(['text'=>'mail'], $data, function($message) { $message->to($user->email, 'New Notification') ->subject('You have a new notification'); $message->from('info@someservice.
Read more

Understanding Laravel's High Order Collections

A Brief Explanation As of 5.4 Laravel comes with High Order collections, a short, more convenient way to filter through a collection. Lets say i have a collection of Users, I want to loop through all of them and display the users full name ( first_name last_name ). Considering I have the method class User extends Eloquent { public function fullName() { return $this->firstname .
Read more

Validate Laravel Requests

In Laravel there are many different ways to validate user input, and one of these is Request validation. This method is best for when you are validating something that isnt a model, like for example a contact form. Validating requests this way will allow you to move all your validation logic out of your controllers and into its own Request Validation class. Remember Slim Controllers/ Fat Models. See below. Below is an example of what we want to avoid, a bloated controller!
Read more