Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Alison Gianotto:
Demystifying Custom Auth in Laravel 5
Nov 21, 2016 @ 11:49:17

Alison Gianotto (a.k.a. Snipe) has a new post on her site talking about custom authentication in Laravel-based applications including built-in functionality and how you can override it to your needs.

I’m a big fan of Laravel. I use it in most of my personal and professional projects, and for the most part it really does make coding fun for me again. One of the things Laravel tries to do (similar to Rails) is to build in the most repetitive things a developer would have to do, for example a user registration/login/forgotten password system.

[...] In each of my current Laravel apps, auth works just a tiny bit differently. Add to that the fact that a few of them were pulled forward from Laravel version 4.2, and things can get confusing and messy. [...] Laravel makes this really, really easy – they just don’t document how to do it very well.

She starts by mentioning the "fresh" install version of building out the auth pieces (php artisan make:auth) but points out that, if a more "hybrid" system is needed, a bit more work is required. She shows you the routes that are created in the "make:auth" process and how/where you need to modify things to customize it to your system. She illustrates with some of her own changes including code examples.

tagged: laravel tutorial custom authentication framework

Link: http://snipe.net/2016/11/demystifying-custom-auth-in-laravel-5/

INANI El Houssain:
Build your OWN switch statment using Laravel’s custom blade directives
Nov 03, 2016 @ 10:26:01

In this post on his Medium blog INANI El Houssain shows you how to create a custom directive for use with Laravel's Blade templating language. In this example he shows how to make a custom switch statement, something commonly used on the PHP side to select an action based on a value.

One of the good points of Laravel’s framework is that it allows you to make your own components, macros and directives. so today we will make use of Laravel’s Custom Blade directives and make something good.

He starts with a simple "hello world" example to show where the pieces all live, outputting a simple "Hello $name" string. He then moves into the creation of the "@switch" directive having it write out the PHP code required for the switch to start and end. He adds in two more tags to start and end the different cases: @case and @endcase. The post wraps up with an example of all of these tags in use and how to catch when the value under evaluation might be empty.

tagged: laravel blade directive custom output switch tutorial case

Link: https://medium.com/@InaniT0/build-your-own-switch-statment-using-laravels-custom-blade-directives-218244e41a7c#.dtkbzif3j

Freek Van der Herten:
Method overloading is possible in PHP (sort of)
Oct 21, 2016 @ 09:33:41

Freek Van der Herten has a post to his site showing how PHP functions can (sort of) be overloaded with the help of a trait from Adam Wathan.

PHP does not support method overloading. In case you’ve never heard of method overloading, it means that the language can pick a method based on which parameters you’re using to call it. This is possible in many other programming languages like Java, C++.

However, with some clever coding, Adam Wathan made a trait, aptly called Overloadable, that makes method overloading possible. It works by just accepting any parameters using the splat operator and then determining which of the given functions must be called according to the given parameters.

He shows how to use the trait in a simple example, defining a single "bar" function and using the "Overloadable" trait to handle the switching between the methods based on the input variables. You can find more information about the trait and the source for it in this gist over on GitHub.

tagged: method overload trait custom splat operator variable

Link: https://murze.be/2016/10/method-overloading-possible-php-sort/

Stovepipe Systems:
Symfony Security Roles vs. Voters
Aug 22, 2016 @ 10:08:28

On the Stovepipe Systems blog author Iltar van der Berg has retuned with a continuation of his series on Symfony security basics with this new post covering voters and roles.

In my previous blog post I've explained the basics of authentication, authorization and how this is dealt with in Symfony. Due to the size of the post, I've left out several important topics such as roles and voters; Both an equally important part of authentication and authorization. A common misconception is that roles should be used to check permissions. In fact, they should definitely not be used to check permissions!

He goes on to explain where "roles" come into the process of authentication (not authorization) and how they describe something about the user of the system. With that defined he moves on to the "voters": functionality that "vote" on attributes related to the user/request/resource/etc. and return a pass or fail decision based on their logic. He explains why voters are probably more what most developers are looking for and some reasons to use them over roles. He then ends the post showing how to create your own custom voter and configure it into your application.

tagged: tutorial symfony authorization voter role introduction custom

Link: https://stovepipe.systems/post/symfony-security-roles-vs-voters

SitePoint PHP Blog:
Your Own Custom Annotations – More than Just Comments!
Jun 21, 2016 @ 11:04:14

The SitePoint PHP blog has posted a new tutorial from author Daniel Sipos showing you how you can use custom annotations in your Symfony-based application. You can also do annotation parsing outside of Symfony but that requires other external libraries to accomplish.

In this article, we are going to look at how we can create and use our own custom annotations in a Symfony 3 application. You know annotations right? They are the docblock metadata/configuration we see above classes, methods and properties. You’ve most likely seen them used to declare Controller routes (@Route()) and Doctrine ORM mappings (@ORM()), or even to control access to various classes and methods in packages like Rauth. But have you ever wondered how can you use them yourself?

[...] In this article we are going to build a small reusable bundle called WorkerBundle. [...] We’re going to develop a small concept that allows the definition of various Worker types which “operate” at various speeds and which can then be used by anyone in the application. The actual worker operations are outside the scope of this post, since we are focusing on setting up the system to manage them (and discover them via annotations).

He then gets into the code, creating the WorkerInterface the workers will implement and a sample worker class with an annotation describing it. Next up he creates the WorkerManager to create and get the current set of workers. Then comes the discovery process and the creation of a simple class that looks through files and finds those with the @Worker annotation and makes them available as a worker instance. Finally he "wires it all together" in the services configuration and shows an example of a basic worker instance and using it by calling its work method.

tagged: custom annotations worker example symfony application tutorial

Link: https://www.sitepoint.com/your-own-custom-annotations/

Adam Wathan:
Writing Your Own Test Doubles
May 11, 2016 @ 10:19:58

In this recent post to his site Adam Wathan about writing your own custom test doubles (fakes) to help make your tests cleaner and improve their overall readability/maintainability.

Once in a while I run into a situation where trying to use a mocking library hurts the readability of my test. For example, say I’m building out a basic user registration flow where someone signs up and receives a welcome email. [...] To test that an account is created correctly, I can make a request to the endpoint and verify that the new account exists in a test database. [...] This covers creating the account itself, but what’s the best way to test the welcome email?

He goes through a few of the options that could be used to test this including using Mockery to replace the mailer class with a spy or actually sending emails. There's downfalls to both of these methods and he suggests using a custom "fake" where the mailer class is swapped out with an "in-memory" option with the same kind of interface. He does point out a few issues with this method, however, and offers a few tips to remember when using them.

tagged: custom test unittest doubles email example video screencast

Link: http://adamwathan.me/2016/01/25/writing-your-own-test-doubles/

Ibuildings Blog:
Working with Entities in Drupal 7
May 05, 2016 @ 12:29:26

On the Ibuildings site there's a tutorial posted talking about working with entities in Drupal 7 and how creating your own classes for them can make them easier to manage.

Developers love object-oriented code. But how can this be achieved with Drupal 7 entities? By default Drupal uses a single class for all entities of a given type. For example, all node objects are standard classes (stdClass) and all entity objects have the Entity type. I personally like to have an entity model that only exposes the functionality that is applicable for the logic of your domain.

[...] Wouldn’t it be nice to create your own classes for entities?

First off, he starts with a refresher on what entities are and how they relate to the database schema. He points out the difficulties in using them and testing their types. He then provides his suggested solution for "all" of your entity problems - the creation of classes for the different entity types. He gives an example using an Article type and how to create/use them in your Drupal code.

tagged: drupal7 entities custom class stdclass tutorial

Link: https://www.ibuildings.nl/blog/2016/04/working-entities-drupal-7

SitePoint PHP Blog:
Theming Views in Drupal 8 – Custom Style Plugins
Mar 24, 2016 @ 12:40:30

The SitePoint PHP blog has another post in its series about working with Drupal 8. In this new tutorial author Daniel Sipos talks about theming views in the content management system and introducing custom style plugins.

In this article, we are going to look at how we can create a custom Style plugin for Views in Drupal 8. We will use the Bootstrap tab markup as a goal and implement a tabbed output for our View results. In the View configuration, the Style settings will allow us to specify which field will be used as the tab navigation copy, leaving the rest of the fields shown in the respective tab panes. Basically, each View result will represent a tab – so this example is not suited for Views which have more than a few results. The main goal is to illustrate how we can create our own Views Style plugins in Drupal 8.

He starts by talking about Style plugins - what they are and where they fit in the application execution flow. He then walks you through the creation of the custom style plugin to integrate the Bootstrap tabs. This also includes the creation of the theme and the matching template to build out the tab markup.

tagged: drupal8 theme view custom style plugin tutorial

Link: http://www.sitepoint.com/theming-views-in-drupal-8-custom-style-plugins/

Zaengle Blog:
Exploring Laravel's Custom Blade Directives
Mar 14, 2016 @ 13:38:30

On the Zaengle blog there's a post spotlighting the custom Blade directive functionality that comes with using the Blade templating engine of the Laravel framework. These allow the definition of custom functionality available directly from the templating layer.

Earlier today, I was working on coding up a design that displays a varying number of cards - each with a unique title and description… think Masonry/Pinterest-esque. I’ve been using Model Factories to stub out a bunch of cards, all with different content. Once I’d hooked up the dummy data to the card templates, I realized that the design didn’t work as well with titles that had more than 20 or so characters.

A common solution to this would be to use CSS to break the line and automatically add an ellipsis. [...] However, this wouldn’t work well in my situation because the design allows titles to be two lines long. Another solution would be to chop off the title at a given length and add an ellipsis using a php snippet. [...] However, adding this much PHP to my Blade templates would have really mucked up my otherwise-clean templates. So I decided to drop this functionality into a custom Blade directive that I could reuse where necessary.

He shows how to define the custom Blade directive in the AppServiceProvider class (autoloaded with every request) for a simple "Hello World" example. He also shows how to use this in the template code, making a simple call to its matching helloWorld tag. He then implements his custom truncate handling, returning some simple PHP code that automatically reduces the content down to a given length and echoes out the result.

tagged: laravel custom blade directive tutorial helloworld truncate

Link: http://zaengle.com/blog/exploring-laravels-custom-blade-directives

TutsPlus.com:
What Are Laravel 5.0 Facades?
Feb 23, 2016 @ 12:22:49

The TutsPlus.com site has posted a tutorial for those either new to the Laravel framework or wanting to get started a bit easier. In this new post they talk about the "facades" the framework makes wide use of - what they are and how they work.

The facade is very similar to the adapter and decorator patterns. The adapter acts like a bridge between two interfaces which are not compatible, while the decorator is more complex and used for dynamically changing the way objects behave.

[...] Sweet syntax, which Laravel uses, makes writing code cleaner and easier to understand. Laravel facades are actually the syntactic sugar for service location.

He uses the Cache facade in his introduction, showing where the various parts of it are defined and how it hooks in to the framework's functionality. With the basics out of the way he then shows how to create a custom facade instance: a simple check to see if the file name provided is a PDF or not.

tagged: laravel facade introduction tutorial custom pdf file

Link: http://code.tutsplus.com/tutorials/what-are-laravel-50-facades--cms-25347