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

Sameer Borate:
Creating custom stream filters in PHP
Apr 11, 2018 @ 09:45:43

Sameer Borate has a new post to his site showing you how to create custom stream filters for use with the streams functionality already included in the PHP language. The streams handling provides a resource instance (filesystem, network connection, etc) that can be interacted with in a more standardized way.

In this post we will see how to create a custom stream filter. Streams, first introduced in PHP 4.3, provide an abstraction layer for file access. A number of different resources besides files – like network connections, compression protocols etc. can be regarded as “streams” of data which can be serially read and written to.

He shows how to get the current list of streams available and includes an example of one in use, the "string.strip_tags" filter. From there he shows the creation of a custom filter, one that replaces any URLs detected in a string with a string of [--URL--]. He includes the code for the filter and shows how to register it using the stream_filter_register function. He also includes an example of it in use, grabbing the contents of the BBC site and having the filter automatically applied.

tagged: custom filter tutorial beginner strip url

Link: https://www.codediesel.com/php/creating-custom-stream-filters/

Tomas Votruba:
Try PSR-12 on Your Code Today
Apr 10, 2018 @ 10:51:19

In a post to his site Tomas Votruba shows you how to test the PSR-12 standard on your current codebase using some custom PHP-CS-Fixer rules. The PSR-12 standard is a recommendation from the PHP-FIG group about consistency in coding styles and formatting. It expands and replaces the previous PSR-2 standard.

The standard is still behind the door, but feedback, before it gets accepted, is very important. After accepting it will be written down and it will be difficult to change anything.

Try PSR-12 today and see, how it works for your code.

Korvin Szanto, a developer working on the PHP-CS-Fixer project, has put together a commit with the rules to update and enforce the PSR-12 coding standard. The post shows how to install these rules and how to change up your YAML configuration to include them. He also includes a discussion about agreeing/disagreeing with the coding standard idea and gives examples of two rules he personally doesn't agree with.

tagged: psr12 coding standard phpcsfixer rules custom tutorial

Link: https://www.tomasvotruba.cz/blog/2018/04/09/try-psr-12-on-your-code-today/

Laravel News:
User Defined Schedules in Laravel
Mar 09, 2018 @ 11:43:31

On the Laravel News site there's a new post looking at the use of user-defined schedules and some of the "hidden" features the Laravel framework provides to allow for even more customization.

Recently Adam Wathan and Taylor Otwell have used Basecamp to track what they are doing every day–a tech diary. Notably, they are using the check-in feature which allows you to schedule questions to be sent to members about almost anything. Taylor and Adam are using it for “What did you work on today?”

It’s not that it’s the same time of day or even that it’s only weekdays which peaked my curiosity but the fact every check-in has a custom schedule defined by a user. In Laravel, we can quickly schedule a job to run every weekday at 4 pm, and we can use a custom timezone. But out of the box, we cannot set a different schedule for every individual check-in. We would have to store a cron expression and manually check every minute if it’s due.

After some "source code diving" the author figured out that the Schedulable trait allows you to define a more customized version of a schedule to fit your needs, making it simple to implement in any class or model. Code examples of these changes are included in the post as well as some next steps to have it implemented more widely either in the Laravel core or a package.

tagged: laravel schedule custom trait tutorial example

Link: https://laravel-news.com/user-defined-schedules-in-laravel

Gates and Policies in Laravel
Nov 20, 2017 @ 12:46:28

On the TutsPlus.com site there's a new tutorial posted for the Laravel users out there covering a few pieces of the authorization features of the framework. The tutorial covers "gates" and "policies", introducing some of their basic concepts and providing example code to implement your own.

Today, we're going to discuss the authorization system of the Laravel web framework. The Laravel framework implements authorization in the form of gates and policies. After an introduction to gates and policies, I'll demonstrate the concepts by implementing a custom example.

I assume that you're already aware of the built-in Laravel authentication system as that's something essential in order to understand the concept of authorization. Obviously, the authorization system works in conjunction with the authentication system in order to identify the legitimate user session.

The article starts by introducing some of the basic approaches the framework takes to authorization handling and where gates and polices fit in. It then gets into the details of each including example code showing how to define them based on the interfaces provided. The tutorial then shows how to put them to use in a simple application, applying them at both the controller and view level.

tagged: laravel tutorial gate policy authorization custom introduction

Link: https://code.tutsplus.com/tutorials/gates-and-policies-in-laravel--cms-29780

Laravel News:
Appstract Laravel Blade Directives
Oct 04, 2017 @ 10:22:54

In a new post to the Laravel News site they highlight a package that adds a set of common Blade directives to your Laravel application.

With no shortage of excellent blade extensions circulating in the community, you should also check out the appstract/laravel-blade-directives package.

Whether you end up installing the package or copying a few that you like to your project, here are some notable directives from the project.

Directives included in the package can be used to: check the current route for a match, see if a value is true or not, inline the contents of a CSS file and many more. Check out the full post for examples of these and others the package includes. The source for the package can be found in this repository on GitHub.

tagged: laravel blade directive custom package tutorial

Link: https://laravel-news.com/appstract-laravel-blade-directives

Jeff Madsen:
PhpStorm: Tasks & Contexts with Your Git Branches
Sep 26, 2017 @ 09:37:29

On his Medium.com site Jeff Madsen shows you how to use contexts in the PhpStorm IDE to switch between environments or current work using a more streamlined process.

Switching context is a pain.

Not just because you need to mentally switch the complex web of ideas in your head. Think about all the physical files on different git branches you have to remember in order to answer a “quick question about task #123”. [...] PhpStorm has a lot of great context links and shortcuts to help you navigate among all these, but it is still a royal pain whenever you need to put one set of files aside and work in a different area of the codebase.

[...] When I finished something and pushed it up for review if there was even a small request to change a default or label I had to reopen the branch and track down the correct files where the work was done. How could I turn that all into a single, easy step?

Enter Contexts and Tasks!

He starts off by defining what a "context" is in the world of PhpStorm - a group of open files with a name attached - and how they can be created/saved inside the IDE. Next is the idea of "tasks" that help with performing operations and relating them to contexts and groupings of files. He then shows how to switch between tasks related to a certain feature and how to close it out when you're done.

tagged: phpstorm custom task context group file switch tutorial

Link: https://medium.com/@codebyjeff/phpstorm-tasks-contexts-with-your-git-branches-92d9d1c5a34b

Asmir Mustafic:
How to add custom error codes to your Symfony API responses
Sep 22, 2017 @ 11:10:01

Asmir Mustafic has posted a guide on his site showing how to create custom error codes in the API responses from your Symfony-based application.

When writing APIs, a proper error handling is fundamental. HTTP status codes are a great start, but often when we deal with user inputs is not enough. If out model has complex validation rules, understanding the reason behind an 400 Bad Request error can be not trivial.

Fortunately when for symfony developers there are many libraries to deal with it. Symfony Validator, <a href="https://github.com/symfony/form>Symfony Form, <a href="https://github.com/FriendsOfSymfony/FOSRestBundle>FOS REST Bundle and JMS Serializer combined allows you to have nice error messages to be shown to your users.

He walks you through the code required to create the default error handling with an "author" example. This includes the creation of the entity as well as the form and controller to handle the request/response. He then refactors this away from the default to create the custom error handler with handlers for the message and code to be returned. The post ends with the configuration changes to register it with the application and what the result ends up looking like.

tagged: symfony tutorial custom error code api example

Link: http://www.goetas.com/blog/how-to-add-custom-error-codes-to-your-symfony-api-responses/

Laravel News:
Creating Your Own Configuration in Laravel
Sep 19, 2017 @ 12:14:16

On the Laravel News site there's a new tutorial posted showing you how to create your own custom configuration for those that are newer to the framework.

If you are new to Laravel, you might not yet know how you can create your configuration files in your projects and a few other helpful things that will help you master configuration. As always, the official documentation provides great information, and there are a few things we’ll cover here that I think will help people new to the Laravel framework.

They walk you through a complete guide to the Laravel configuration handling including:

  • what pieces are involved and where they belong
  • accessing the configuration service
  • working with configuration values
  • creating a custom configuration
  • caching the configuration for better performance

Each item on the list has a brief description helping to illustrate the point and commands/code examples where appropriate.

tagged: laravel configuration custom tutorial introduction

Link: https://laravel-news.com/creating-configuration-in-laravel

Jeff Madsen:
Custom Pivot Table Models, or Choosing the Right Technique in Laravel
Sep 11, 2017 @ 11:17:09

Jeff Madsen has some helpful hints for the Laravel (or just Eloquent) developers out there when it comes to models. In this new post to his Medium site he looks at using custom pivot table models (and choosing the right technique to use them).

Laravel is “opinionated”, but that usually just means that there are a lot of defaults set up that push you toward a certain style. Over the years I have found very few cases where you can’t easily override these; more often than not, it is simply a case of adding another function call when you instantiate something.

As I thought about the way I would normally code around Mark’s problem, and then searched and found the more direct answer from the docs, it occurred to me that this was a perfect example.

He starts with the more "standard" way of working with pivot tables in Laravel (code example include) using the updateExistingPivot method. He talks about the difference between writing the code and designing the code, making it more maintainable and well-architected. He talks about moving the logic into a service class but points out a few issues with that approach as well. Finally, in his last option, he covers the special "Pivot" model included with Eloquent and shows it in use.

tagged: custom pivot table model laravel tutorial example

Link: https://medium.com/@codebyjeff/custom-pivot-table-models-or-choosing-the-right-technique-in-laravel-fe435ce4e27e

How to Create Custom Drivers in CodeIgniter
Sep 11, 2017 @ 10:36:54

On the TutsPlus.com site there's a new tutorial posted showing you how to create custom drivers in a CodeIgniter application. In this case the "drivers" are what lefts the application work with external technology or services.

The best way to understand the concept of drivers is to look at how caching is implemented in the core CodeIgniter framework. The main Cache class acts as a parent class and extends the CI_Driver_Library class. On the other hand, you'll end up finding child classes for APC, Memcached, Redis and the like, implemented as pluggable adapters. The child classes extend the CI_Driver class instead of the main driver class.

[...] Creating a custom driver in the CodeIgniter application is the aim of today's article. In the course of that, we'll go through a real-world example that creates a MediaRenderer driver used to render the media from different services like YouTube, Vimeo and similar. The different services will be implemented in the form of adapter classes.

He starts by listing the files that he'll be creating along the way and where they need to be located in the application structure. He then starts in on the configuration changes required and the contents of the files. He then walks through the code for each of them briefly explaining how they work. He starts with the drivers then moves to the adapters and, finally, how to put them together to make a functional renderer for either Vimeo our Youtube videos.

tagged: tutorial codeigniter custom driver video media renderer

Link: https://code.tutsplus.com/tutorials/how-to-create-custom-drivers-in-codeigniter--cms-29339