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

How to Create a Laravel Helper
Apr 21, 2017 @ 15:15:28

On the NetTuts.com site there's a new tutorial from author Sajal Soni showing you how to create a custom Laravel helper, a set of helper functions that can be used through out the entire application.

Laravel includes a variety of global "helper" PHP functions. Many of these functions are used by the framework itself; however, you are free to use them in your own applications if you find them convenient. So, basically, helpers in Laravel are built-in utility functions that you can call from anywhere within your application. If they hadn't been provided by the core framework, you might have ended up developing your own helper classes.

Although the core provides a variety of helpers already, there’s always a chance that you’ll need your own and would like to develop one so you don’t have to repeat the same code here and there, thus enforcing better maintainability. You'll learn how to create a custom Laravel helper in this tutorial.

They start by talking about some of the current helpers and the functionality they relate to (arrays, paths, strings, etc). It then goes into the creation of the custom helper including the skeleton of the code layout, where to place them and how to load them into the application via a service provider.

tagged: custom laravel helper function creation tutorial

Link: https://code.tutsplus.com/tutorials/how-to-create-a-laravel-helper--cms-28537

Introducing Laravel Mix (new in Laravel 5.4)
Feb 09, 2017 @ 09:23:40

Matt Stauffer has posted the latest in his "What's new in Laravel 5.4' series with this new post covering Laravel Mix. Mix is a reworking of the Laravel Elixir package in previous framework releases but has changed a few things up in the process.

Laravel Mix. The same and yet entirely different from Laravel Elixir.

If you're not familiar with Laravel Elixir, it's a wrapper around Gulp that makes it really simple to handle common build steps—CSS pre-processing like Sass and Less, JavaScript processing like Browserify and Webpack, and more.

In Laravel 5.4, Elixir has been replaced by a new project called Mix. The tools have the same end goals, but go about it in very different ways.

He starts with a look at what's new about Mix and how it differs from previous version of Elixir. This includes changes in the structure of the configuration file, a different files/folders structure, helpers and quite a few other changes. Matt gets into detail on each item, providing code examples for the changes and a brief summary of how it's different for those that might have used Elixir before. Check out the full post for the full details.

tagged: laravel mix elixir css javascript compile helper v54 framework

Link: https://mattstauffer.co/blog/introducing-laravel-mix-new-in-laravel-5-4

Laravel News:
Laravel 5.4: JSON Based Language Files
Nov 22, 2016 @ 09:44:33

The Laravel News site has a new post today describing a feature of the upcoming Laravel 5.4 release: the ability to define language files with JSON formatted files.

One of the most wanted requests we receive at Laravel is introducing better support for multilingual web applications, there are already packages out there that add some nice functionality to Laravel for better handling of multilingual projects requirements, but one of the painful issues when building such applications is managing translation keys.

Previously the trans/trans_choice helper to reference the value defined in the PHP array from your language files by a key name. With this new functionality, a new __() helper method is defined that will look through the English translation file, locate the correct key and find the matching record in the requested language to return. They also show how to pass in some parameters into the translator and how to use it directly from Blade.

tagged: laravel json language file helper tutorial parameter blade

Link: https://laravel-news.com/2016/11/json-based-translations/

SitePoint PHP Blog:
Framework-Agnostic PHP Cronjobs Made Easy with Crunz!
Aug 23, 2016 @ 13:36:59

The SitePoint PHP Blog has a new tutorial posted from author Reza Lavaryan showing you how to use the Crunz package to make cronjobs a bit simpler in a more framework-agnostic way.

In this article, we’re going to learn about a relatively new job scheduling library named Crunz. Crunz is a framework-agnostic library inspired by Laravel’s Task Scheduler, but improved in many ways. [...] Before getting started, you should have a firm grasp of cronjobs, so please read our in-depth walkthrough if you’re unfamiliar with how they work.

The tutorial then starts in on the code, getting the library installed and setting up a basic task example. The "task" files are sets of cron-formatted commands that are read and executed much like the cron daemon would on a Unix-based system. Example code for a task and command are included. They also talk about the "frequency" settings allowed by the library and the helper methods to make creating them simpler. The post also includes details about task lifetime, running conditions, configuration and parallelism (among other topics).

tagged: crunz library cronjob cron helper tutorial introduction

Link: https://www.sitepoint.com/framework-agnostic-php-cronjobs-made-easy-with-crunz/

Matt Stauffer:
The new cache() global helper in Laravel 5.3
Jul 08, 2016 @ 11:11:28

Matt Stauffer has continued his series covering some of the new features in Laravel v5.3 with this look at the cache helper, a new feature introduced to help make working with your cached data easier.

As I was writing my book I noticed a pattern in the global helper functions like session() and, in some ways, cookie(). There are three primary functions that they can perform: get a value, put a value, or return an instance of their backing service.

[...] I mentioned that it seems like there should be a cache() helper, and before I could even think much more about it, Jeffrey (Way) had already written one up. So! Behold! The global cache() helper, new in Laravel 5.3.

He includes code samples showing the helper in action. It's a simplified helper so the main functionality is basically the same as the session and cookie helpers: read, write and return an instance of the CacheManager.

tagged: cache global helper laravel cachemanager overview

Link: https://mattstauffer.co/blog/the-new-cache-global-helper-in-laravel-5-3

Freek Van der Herten:
Debugging collections
Jun 17, 2016 @ 09:47:42

Freek Van der Herten has a post to his site with a guide about debugging collections in Laravel applications. He quickly shows how to use the "macro" functionality to gather more insight into what's happening inside.

Lately I’ve been working a lot with collections in Laravel. If you’re not in the know: a collection is a sort of super charged array with a lot of powerful functions to transform the data inside it. The only thing I found a bit of a hassle is how to debug the various steps in a collection chain. Here’s how I improved the workflow.

He shows a simple collection setup with a chain of functions being applied to transform the data inside (ex: filter, map, sortBy). He was able to get a bit more information by using the dd helper Laravel provides but it's not the most "clean" way to just wrap the collection on it. Instead he proposes he use of a "macro" to call the dd function and keep it in the flow of the methods called on the collection (as ->dd()).

tagged: debug collection laravel dd helper function macro

Link: https://murze.be/2016/06/debugging-collections/

Displaying stream progress in PHP
May 27, 2015 @ 08:39:26

Hannes Van De Vreken has posted a tutorial to his site showing users of the Symfony Console component how to show progress on a stream using the ProgressBar helper and a bit of code to inspect the stream itself.

With PHP you can, next to handling HTTP requests, invoke scripts from the command line. [...] The Symfony console component is a very useful tool to define and invoke these kind of CLI tasks. [...] What is actually printed on the console is very important for the issuer of the task. Think of it as the command’s usability. Too little runtime information, the less usable the task. [...] Enter the ProgressBar. The ProgressBar is an output helper that wraps the OutputInterface object.

He talks briefly about how the ProgressBar helper works in the console component's output and gives a simple example of the output. He then shows how to hook it into a bit of PHP using the stream_notification_callback optional parameter (defined in stream_context_create) to point to another class method that handles the progress bar updates. It performs a bit of introspection on the stream and updates the progress as its contents progress. He does point out a few caveats though, including that the transfer is not made asynchronous by this handling.

tagged: symfony console stream progress progressbar helper tutorial

Link: https://hannesvdvreken.com/2015/05/12/stream-progress/

JetBrains Blog:
Laravel Development using PhpStorm
Jan 21, 2015 @ 09:03:16

The JetBrains blog has posted about an update to their popular PHPStorm IDE tool. In this new post they talk about the next level of integration they've introduced for those developing Laravel-based applications.

Last summer, we introduced support for Blade, the template language used by Laravel. Support for artisan, the command line tool for Laravel developers, is baked into PhpStorm as well. Using the Laravel plugin and the Laravel IDE helper, we can further extend PhpStorm's support for Laravel applications.

They walk you through each of these two new updates, showing what kind of features they enable and some screenshots of the interface in use. For more information and to check out other features in this new plugin/helper setup, see this documentation page.

tagged: development phpstorm laravel plugin helper integrate

Link: http://blog.jetbrains.com/phpstorm/2015/01/laravel-development-using-phpstorm/

Code Reuse By Inheritance
Jan 20, 2014 @ 10:55:18

On the Qafoo blog today Kore Nordmann has a new post talking about code reuse through inheritance. He talks about base classes, sharing code and abstraction.

To me, inheritance has two properties: Defining an is-a relationship [and] making it possible to share code between classes by extending from a common base class. The is-a relationship between classes is one thing I like to use inheritance for. [...] The other thing you can use inheritance for is to share code between multiple classes. [...] I personally think that the latter is one of the worst things you can do in object oriented design. Bear with me for a moment and let me try to explain why I think that.

His example of doing it the wrong way is using the Active Record design pattern and how it's usually implemented - storage logic in the base class and business/table logic in the extending class. He then gets into an example that's a bit "smaller" creating diff display functionality and how the "code reuse by inheritance" creeps in a lot in helper methods. He also briefly looks at testing (or not testing) private methods and and the "Depth of Inheritance Tree" metric's recommended value.

tagged: code reuse inheritance helper activerecord testing depth

Link: http://qafoo.com/blog/063_code_reuse_by_inheritance.html

Using Traits for Code Reuse in Zend Framework 2
Oct 02, 2013 @ 10:56:28

For those that might have heard of traits (made available in newer versions of PHP, 5.4+) but haven't seen much of a practical application, this new post from Matthew Setter could help.

Here’s the situation which prompted the use of them, in a nutshell. I had a custom view helper which performed some rather elementary date & time formatting, based purely on US standards. When I first wrote the ViewHelper, I wasn’t aware of any other use case I’d have for it. So it made sense for it to be self-contained. Such is life however, as later in development the need did arise to do more date/time formatting. But this time, far removed from the view layer in a model. [...] So I weighed up my options and chose to go with Traits. I’ll be honest, there was the new & cool factor to them as well – as well as an irresistible sense of simplicity in them.

He shares the actual trait code he implemented, making two simple methods - one for formatting date and another for formatting the time - for his views to use. He also includes examples of it in use. He also sought some feedback

tagged: zendframework2 traits reuse view helper

Link: http://www.maltblue.com/php/using-traits-for-code-reuse-in-zend-framework-2