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

Matthew Weier O'Phinney:
Creating Exception types on-the-fly in modern PHP
Dec 07, 2018 @ 17:44:03

Matthew Weier O'Phinney has posted a tutorial to his site sharing a method he's found for creating Exception types dynamically allowing you to create a system that can still be caught by normal means but is more flexible than hard-coded exceptions.

We pioneered a pattern for exception handling for Zend Framework back as we initially began development on version 2 around seven years ago. The pattern looks like this: we would create a marker ExceptionInterface for each package. [Then] we would extend SPL exceptions and implement the package marker interface when doing so.

What this gave users was the ability to catch in three ways. [...] This kind of granularity is really nice to work with. [...] So, what happens when you're writing a one-off implementation of something that is expected to throw an exception matching one of these interfaces?

Why, use an anonymous class, of course!

He includes an example of putting this approach to work, using a throw call along with a dynamic (anonymous) class to extend the required class and implement the associated interface. In his example he creates a dynamic exception for handling a "not found" type of exception.

tagged: exception dynamic tutorial anonymous class custom

Link: https://mwop.net/blog/2018-12-05-on-the-fly-exceptions.html

Jonathan Reinink:
Dynamic relationships in Laravel using subqueries
Dec 05, 2018 @ 18:50:20

In a recent post to his site Jonathan Reinink has written up a guide to using dynamic (Eloquent) relationships in Laravel applications by making use of subquery functionality. In it, he shows how to make use of the selectSub method to select additional information in a single query versus having the overhead of custom, hard-coded relationships.

When building web apps that interact with a database, I always have two goals in mind: keep database queries to a minimum [and] keep memory usage to a minimum. These goals can have a drastic impact on the performance of your app.

Developers are typically pretty good at the first goal. We're aware of N+1 style problems, and use techniques like eager-loading to limit database queries. However, we're not always the best at the second goalkeeping memory usage down. In fact, we sometimes do more harm than good trying to reduce database queries at the expense of memory usage.

He starts off with the challenge he's trying to solve: gathering login information for users in a performant way. He includes the schema for the users and logins table and shows the code of how a normal relationship select might look to get login information for each user (creating an N+1 issue).

To help solve the issue, they try caching the last login information but realize they can do better - this is where subqueries come in. They provide an example of using the selectSub method to get the login information, mapping it to a macro for easier use and defining scopes. Finally, the tutorial shows how to use this method to select information via dynamic relationships. It also talks about lazy-loading issues and if the same thing could be accomplished with a "has one" relationship.

tagged: tutorial laravel eloquent dynamic relationship subselect database query

Link: https://reinink.ca/articles/dynamic-relationships-in-laravel-using-subqueries

Pineco.de:
Adding a Subdomain to Your Laravel Application
Nov 16, 2018 @ 17:44:42

On the Pineco.de blog there's a tutorial for the Laravel users out there showing how to set up subdomains in your application using the built-in framework functionality (and their setup with Valet/Forge).

Often we need to handle subdomains with our Laravel application. Let’s take a look, how can we treat the routes dynamically and set up the subdomain with Valet and Forge.

The tutorial then walks you through the steps to get subdomains working:

  • Defining Subdomains in Our Routes
  • Dynamically Generate Subdomains
  • Faking Subdomains in Valet
  • Setting Up Subdomains in Forge

Each section comes with a brief explanation and any code or configuration changes that would need to be made to make it all work.

tagged: subdomain laravel tutorial route dynamic valet forge

Link: https://pineco.de/adding-a-subdomain-to-your-laravel-application/

Delicious Brains Blog:
Full Page Caching With Personalized Dynamic Content
Jan 03, 2018 @ 17:19:13

On the Delicious Brains site there's a tutorial posted by Ashley showing you how to set up full page caching with personalized dynamic content instead of the usual package caching of static content every user should see. The post is focused on improving the performance of a WordPress-based site.

We’ve talked a lot about WordPress performance here at Delicious Brains and the importance of page caching. However, implementing a page cache on highly dynamic sites or sites which display personalized content isn’t always easy. Previously, we’ve covered Microcaching for dynamic content, but that still doesn’t help when personalized content is involved.

In this article we’re going to tackle that issue. We’re going to use Easy Digital Downloads and the Themedd theme to build a fictitious online store. This will present us with a few problem areas that mean we can’t perform page caching out-of-the-box.

The tutorial starts by outlining the issues that come up with traditional caching tools and dynamic content. It then gets into the changes required to make it work with the Simple Cache plugin. It shows the code involved in splitting the caching functionality by generic, static page caches and how to inject the dynamic content cache back into the page when a user-specific resource is requested.

tagged: full page caching dynamic content wordpress tutorial inject

Link: https://deliciousbrains.com/page-caching-personalized-dynamic-content/

Hayden James:
PHP-FPM tuning: Using ‘pm static’ for max performance
Oct 13, 2017 @ 16:28:13

Hayden James has a post on his site sharing a method he's found for getting better performance out of PHP-FPM with the help of a configuration change. In this tutorial he shows how to use the pm static setting to squeeze the best performance out of your web server.

Lets take a very quick look at how best to setup PHP-FPM for high throughput, low latency and a more stable use of CPU and memory. By default, most setups have PHP-FPM’s PM (process manager) string set to dynamic and there’s also the common advice to use ondemand if you suffer from available memory issues.

He starts by defining the three "pm" settings and what they do: dynamic, ondemand and static. He then talks some about how the PHP-FPM process manager is similar to CPUFreq Governor and the settings it allows. Finally he gets into talking about the "pm static" handling, how it relates to available memory and when it makes more sense to use "dynamic" over "static".

tagged: phpfpm tuning performance static dynamic ondemand tutorial memory

Link: https://haydenjames.io/php-fpm-tuning-using-pm-static-max-performance/

Laravel News:
Dynamic templates in Laravel Blade with View::first
Sep 14, 2017 @ 15:54:11

On the Laravel News site there's a tutorial posted showing you how to use Blade's View::first functionality to dynamically show a template if it exists.

When building dynamic components or pages sometimes we want to display a custom template if it exists or otherwise fall back on a default one.

[...] We can solve this problem with a series of conditionals or by using view()->exists() to check if a custom template exists or not, however, Laravel 5.5 brings us a better and more elegant way.

A screencast of the functionality in action is included as well as a text-only version with code samples showing the previously used "if view exists" check and the refactor to use View::first instead.

This dynamic view loading feature was added to Blade in Laravel v5.5 and is a great way of keeping your controllers simple by avoiding extra conditionals when dealing with dynamic templates.
tagged: dynamic template laravel blade view first refactor laravel55

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

TutsPlus.com:
Dynamic Page Templates in WordPress, Part 3
Jun 19, 2017 @ 15:45:04

The TutsPlus.com site has posted the third part of their "Dynamic Page Templates in WordPress" tutorial series today. In this latest article author David Gwyer finishes off the series using all that they've shared from part one and part two to create two examples.

In the first two parts of this tutorial series, we covered what dynamic page templates were and why they were needed. We also looked at the code required to implement them.

In this third and final tutorial in the series, I'll be creating two examples of fully working dynamic page templates you can use in your own projects. These were specifically chosen to be easily extendable to suit your own needs, and are intended as inspiration for any other type of dynamic page templates you can think of.

He then walks you through the creation of the two page templates: a Simple Contact Form and a Blog Post Archive. The first allows you to dynamically control the form elements for a UI interface (rather than code) and the second uses dynamic data to display the list of previous blog posts. The tutorial then finishes with a look at how, since WordPress 4.7, you can use dynamic page templates with any kind of post, not just pages.

tagged: wordpress series part3 dynamic page template blog archive simple form tutorial

Link: https://code.tutsplus.com/tutorials/dynamic-page-templates-in-wordpress-part-3--cms-28514

StartTutorial.com:
Dynamic image resizing via URI with Glide and Slim
Feb 02, 2017 @ 18:45:58

The Star Tutorial site has a new article posted showing you how to create a system based on Glide and the Slim framework to dynamically resize images with a few simple URLs.

If you have ever used WordPress.com backend. You will notice its image resizing works by appending a dimension string at the end of the URL.

In this tutorial, we will show you how to do that for your own project. At the end of this tutorial, you will have an image resizing server that is able to give you specific dimension upon request.

The tutorial starts with a basic introduction to the Glide image manipulation tool and how to get a Slim installation up and running. They then help you install the league/glide-slim package to help link the two. With those in place, they show you how to build out the resizing code using the ServerFactory handling to grab the image, passing in the URL parameters to tell Glide how to manipulate the image.

tagged: tutorial dynamic image resize uri glide slim

Link: https://www.startutorial.com/articles/view/dynamic-image-resizing-via-uri-with-glide-and-slim

Matthias Noback:
Creating virtual pages with Sculpin
Dec 14, 2016 @ 15:28:58

Matthias Noback has continued his series looking at using Sculpin to create static pages for "in-project documentation" with this second post focusing more on the creation of "virtual pages". These pages allow the insertion of dynamic content into pages pulled from other sources (in this case, from the source of the project).

Previously we looked at how to use the static site generator Sculpin to generate in-project documentation. When Sculpin builds the HTML files and assets it looks at what files are in the source/ directory and processes them based on certain rules (e.g. "parse Markdown files", "format HTML files with Twig", etc.). The purpose for my "living documentation" project is to also dynamically generate documentation based on PHP and configuration files from the main project.

[...] The Sculpin documentation mentions several ways to generate dynamic content for a static site, as opposed to the standard way of adding static Markdown or HTML files. [...] So, I dug into the source code of Sculpin to find out what would be a reasonable extension point. It turned out that the best way to approach this was to create a custom data source (which is currently not documented).

He then walks you through the creation of a custom data source with "dataSourceId" and "refresh" methods the tool uses to pull in the dynamic data. He shows how to set up the script to be called at any time and have the same effect (Sculpin does this when the "server" is running) and defining the service in your Sculpin configuration.

tagged: sculpin static site generator virtual page dynamic content tutorial

Link: http://php-and-symfony.matthiasnoback.nl/2016/12/creating-virtual-pages-with-sculpin/

SitePoint PHP Blog:
The Delicious Evils of PHP
Dec 07, 2016 @ 15:50:49

On the SitePoint PHP blog Christopher Pitt is back with another interesting article, this time talking about two "delicious evils of PHP" - the eval and exec functionality.

I want to look at two PHP functions: eval and exec. They’re so often thrown under the sensible-developers-never-use-these bus that I sometimes wonder how many awesome applications we miss out on.

Like every other function in the standard library, these have their uses. They can be abused. Their danger lies in the amount of flexibility and power they offer even the most novice of developers. Let me show you some of the ways I’ve seen these used, and then we can talk about safety precautions and moderation.

He then talks about some of the "interesting" things you can do with these two pieces of functionality including:

  • Dynamic Class Creation
  • [Creating] Domain Specific Languages
  • Parallelism (with exec)

He ends the post with some advice how to avoid issues with the topics he's mentioned and how to "stay safe" while still using these two dangerous pieces of functionality.

tagged: evils language eval exec dynamic class dsl parallelism tutorial safe

Link: https://www.sitepoint.com/the-delicious-evils-of-php/


Trending Topics: