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

Liam Hammett:
Laravel Blade Helpers
Dec 04, 2018 @ 15:20:36

Liam Hammett has written up a post to his site covering the use and creation of helpers for Laravel's Blade templating. He shows the use of a package he's created to help make using them with custom callbacks simpler.

Laravel’s Blade templating engine offers a ton of convenient directives you can use to make your view files beautiful and abstract anything that may be too complex or verbose to live inside HTML. It even gives a really handy way to add your own custom directives using the Blade::directive(…) method.

However, the callback in custom directives only receives a single parameter?—?the raw string expression from the view file. [...] As this seems to be the most common use case, I put together a package that attempts to help make these helper functions that little bit easier to define without the boilerplate of returning the string or having to consider what an expression may be when creating a directive.

In his package he introduces a new method that defines the name of the method and the name of the function to call. This second option can also be a custom callback function, making it even more flexible.

tagged: laravel blade template helper package custom callback tutorial

Link: https://medium.com/@liamhammett/laravel-blade-helpers-8d710fa31fd9

Laravel News:
Unwrapping array_wrap()
Nov 30, 2018 @ 15:34:32

On the Laravel News site they have a new post that "unwraps" the array_wrap helper that makes it easier to normalize values into arrays and removes the need for manually checking if a variable is an array or set before using it like one.

Laravel has a wrap method and array_wrap() helper to normalize values into an array. Raul @rcubitto shared this nice tip about it on Twitter and before seeing his Tweet I wasn’t aware of this method.

They include an example of the helper function in use and share some of the responses to his tweet including questions of how it's different than casting to an array. They answer this by talking about more complex values and how the helper is useful for null values and creating consistency in your application.

tagged: laravel helper function array arraywrap tutorial

Link: https://laravel-news.com/array_wrap

Laravel News:
Using the Laravel Optional Helper and the New Optional Closure
Apr 09, 2018 @ 15:14:07

On the Laravel News site there's a new tutorial posted showing you how to use the "optional" helper and closure to help control issues where a property or method doesn't exist (or can't be accessed).

The Laravel Optional class and accompanying optional helper were introduced in Laravel 5.5. This class is useful to avoid those pesky Trying to get property of non-object errors in your code.

Joseph Sibler submitted an improvement to the Laravel optional helper that we covered in Laravel 5.6.13 which now allows a closure that is only called when the object is not null

They compare the "optional" functionality to the null coalesce operator and the cases when the former should be used over the latter. A few code snippets also help to illustrate the difference. For more information about this helper and other handy features, check out their other article with a "top 5" list of their favorite helpers.

tagged: optional helper closure tutorial example laravel

Link: https://laravel-news.com/laravel-optional-helper

Laravel News:
5 Laravel Helpers to Make Your Life Easier
Feb 22, 2018 @ 15:45:32

On the Laravel News site they've posted a new article with a listing of five useful helpers that come standard as a part of the Laravel framework.

There are a ton of helper methods in Laravel that make development more efficient. If you work with the framework, I encourage you to see what helpers you can introduce in your day-to-day work. In this blog post, I’d like to point out a few of my favorites.

The list in the post includes helpers for locating information in an array, pluralizing strings, throwing exceptions based on conditions and accessing object property values. For each item on the list there's a brief explanation and some sample code showing it in action (and what the result ends up being). The post finishes up by linking you over to the helpers page of the Laravel manual for more information and other handy functions to help reduce your own development time.

tagged: laravel helper simple top5 list example code

Link: https://laravel-news.com/5-laravel-helpers-make-life-easier

Laravel News:
Creating Your Own PHP Helpers in a Laravel Project
Dec 11, 2017 @ 17:16:36

The Laravel News site has a quick post sharing a helpful topic for the Laravel users out there. In the tutorial they show how to create custom helpers for use across the entire application in any scope.

Laravel provides many excellent helper functions that are convenient for doing things like working with arrays, file paths, strings, and routes, among other things like the beloved dd() function.

You can also define your own set of helper functions for your Laravel applications and PHP packages, by using Composer to import them automatically.

If you are new to Laravel or PHP, let’s walk through how you might go about creating your own helper functions that automatically get loaded by Laravel.

The tutorial starts off by recommending the placement of the custom helper file and how to ensure it's autoloaded when the application is bootstrapped. It then covers the creation of the helper functions including the use of if checks to ensure there's not a function naming collision. Finally the post includes an example of a helper file, sharing the creation of two methods: show_route and plural_from_model. The post ends with a look at packages and how to include helper files inside of them for use in your application.

tagged: helper laravel tutorial introduction project file autoload

Link: https://laravel-news.com/creating-helpers

NetTuts.com:
How to Create a Laravel Helper
Apr 21, 2017 @ 20: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 @ 15: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 @ 15: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 @ 18: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 @ 16: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


Trending Topics: