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

Rob Allen:
Replacing a built-in PHP function when testing a component
Oct 22, 2018 @ 15:55:58

Rob Allen has a new post to his site sharing a method you can use in your testing to replace a built-in PHP function with something customized for your needs.

Recently I needed to test part of Slim that uses the built-in PHP functions header() and headers_sent(). To do this, I took advantage of PHP’s namespace resolution rules where it will find a function within the same namespace first before finding one with the same name in the global namespace. The idea of how to do this came courtesy of Matthew Weier O’Phinney where this approach is used for similar testing in Zend-Diactoros.

He starts off with the code he wants to test - a response method - and a simplified version of the test. This method makes use of the headers_sent and header functions in PHP but those needed to be overridden in order to make the test actually work. He includes the changes to make to the test to override these methods because of how namespaces resolve (using the global PHP namespace last).

tagged: replace builtinfunction tutorial namespace testing unittest slim

Link: https://akrabat.com/replacing-a-built-in-php-function-when-testing-a-component/

Paul Jones:
Slim and Action-Domain-Responder
Aug 23, 2017 @ 16:04:19

In a post to his site Paul Jones shows how an implementation of his proposed Action-Domain-Responder pattern might look when implemented with Slim, a popular PHP microframework.

I’ve had a warm place in my heart for Slim for a long time, and especially so since recognizing the Action-Domain-Responder pattern. In this post, I’ll show how to refactor the Slim tutorial application to ADR.

One nice thing about Slim (and most other HTTP user interface frameworks) is that they are already “action” oriented. That is, their routers do not presume a controller class with many action methods. Instead, they presume an action closure or a single-action invokable class. So the Action part of Action-Domain-Responder already exists for Slim. All that is needed is to pull extraneous bits out of the Actions, to more clearly separate their behaviors from Domain and the Responder behaviors.

He then works through each piece of the example application, first extracting out the Domain logic then building a Responder to handle the user interface functionality. He combines them to replace the current functionality, pointing out that the responder can now be tested separately from the user interface (templating system).

Now, for a simple case like this, using ADR (or even webbishy MVC) might seem like overkill. But simple cases become complex quickly, and this simple case shows how the ADR separation-of-concerns can be applied as a Slim-based application increases in complexity.
tagged: slim microframework actiondomainresponder designpattern adr tutorial refactor

Link: http://paul-m-jones.com/archives/6639

Rob Allen:
Default route arguments in Slim
Jun 14, 2017 @ 14:48:51

Rob Allen has posted a quick tip for the Slim framework users out there showing how to use default route arguments in your application.

A friend of mine recently asked how to do default route arguments and route specific configuration in Slim, so I thought I'd write up how to do it.

He illustrates with a simple "Hello world" route that responds using the "name" value from the URL path. He then shows how to modify this example and define a default using the "setArgument" method on the route itself (for both single and multiple values). He ends with an example of how it can be applied for other values needed in the route as well, like a "role" for access control handling.

tagged: slim framework default route arguments tutorial setargument

Link: https://akrabat.com/default-route-arguments-in-slim/

Rob Allen:
Slim's route cache file
May 31, 2017 @ 14:35:15

In a new post to his site Rob Allen talks about how you can speed up the routing in your Slim framework based application using the route cache file.

When you have a lot of routes, that have parameters, consider using the router's cache file to speed up performance.

To do this, you set the routerCacheFile setting to a valid file name. The next time the app is run, then the file is created which contains an associative array with data that means that the router doesn't need to recompile the regular expressions that it uses.

He gives an example of how to enable the setting and makes the recommendation that it's only used in production. He includes a simple example that defines "25 groups, each with 4000 routes, each of which has a placeholder parameter with a constraint." The first run on a route responds in 2.7 seconds but, once the cache file is created, it drops down to just 263 milliseconds - a major improvement.

tagged: slim route cache file tutorial example performance

Link: https://akrabat.com/slims-route-cache-file/

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

Rob Allen:
Handing JSON data errors in Slim 3
Nov 16, 2016 @ 15:12:01

For the Slim framework users out there Rob Allen has a quick new post to his site showing an easy way to handle JSON data errors in a Slim 3 application (besides just a silent failure).

When you send JSON data into a Slim Framework application with a content-type of application/json, then Slim will decode it for you if you use getParsedBody(). [...] If there's an error however, you get [an empty response]. If you care about this, you can use json_last_error_msg() and return an error.

He includes a code example of using this function on an endpoint, providing more information on why the JSON parsing failed. He also includes a "bonus" that makes use of the jsonlint library to inspect the JSON message and provide back a more human-readable message letting the user know what exactly was wrong with the JSON they provided.

tagged: json error slim slimframework jsonlint tutorial jsonlasterrormsg

Link: https://akrabat.com/handing-json-data-errors-in-slim-3/

CloudWays Blog:
Josh Lockhart Discusses Slim Framework And Photography
Jul 29, 2016 @ 17:30:58

The Cloudways blog has posted their latest interview with a member of the PHP community that has made major contributions. This time they talk with Josh Lockhart, the creator of the popular Slim framework and contributor to the PHP: The Right Way guide.

Josh Lockhart is the creator of Slim Micro Framework for PHP, a well known framework for writing powerful API and web apps. He is also managing PHP The Right Way, a project that provides best practices and quality information about PHP in one place. He works as a senior web developer and special project director at New Media Campaigns.

In this interview Josh talks about his experience with PHP. He also describes how he came up with the Slim Framework and offers great advice to PHP newbies.

In the interview Josh answers questions about:

  • his own history and experience in PHP development
  • how he sees PHP has progressed over the years (and where it's heading)
  • why he created the Slim framework and what makes it different
  • the tools/software he uses to do his work

He also shares some recommendations to beginners with the language and suggests some software for those looking at things like content management systems. The interview finishes up with Josh sharing some about his own passion for photography and his own work environment.

tagged: joshlockhart slim slimframework photography interview community

Link: http://www.cloudways.com/blog/josh-lockhart-interview/

Rob Allen:
Configuration in Slim Framework
Mar 16, 2016 @ 17:30:10

If you're a Slim framework user you should check out the latest post on Rob Allen's site covering all things configuration in using the framework and it's simple configuration handling.

Configuration in Slim Framework is nice and simple: the App's constructor takes a configuration array for the DI container.

He shows how to pass in the configuration as a optional constructor parameter on the main application, including a settings value containing some of the common options. These include the displayErrorDetails flag to show/hide detailed error messages and a logger setup (in his example Monolog). He also shows how to:

  • get settings from the configuration
  • use a separate file for the configuration
  • using a .env configuration file
  • combining multiple configuration files

He includes code examples for each of these cases as well as a method for using a non-array structure (like YAML or XML) via the ZendConfig component.

tagged: slim slim3 slimframework configuration option tutorial settings

Link: https://akrabat.com/configuration-in-slim-framework/

PHP-DI:
Introducing the PHP-DI bridge for Slim
Mar 11, 2016 @ 16:15:24

In this new article on the PHP-DI project site they've shared one of the latest tools they've created to make it easier to integrate PHP-DI with the latest version of the Slim microframework (v3).

Slim 3 was released 3 months ago and it was significant. It is one of the first frameworks to integrate the latest standards and concepts in its core.

[...] While PSR-7 and middlewares are very interesting, what's more useful for us is the use of container-interop. That means Slim 3 can work with any dependency injection container. It is very easy to replace the default container (Pimple) with PHP-DI, but today we are releasing a "PHP-DI - Slim" bridge that goes a little further.

They then show how to use their new bridge to create controllers as services in the container and pass them parameters. The post ends with the instructions on getting the PHP-DI bridge installed and how to use it to replace the default AppSlim creation of your application.

tagged: phpdi dependency injection container slim microframework slimframework v3 bridge

Link: http://php-di.org/news/18-slim-bridge-released.html

Rob Allen:
Improved error handling in Slim 3.2.0
Feb 26, 2016 @ 16:46:53

In this recent post to his site Rob Allen, a developer with the Slim framework project, covers some of the improvements around error handling in the latest version of the framework, v3.2.0.

We released Slim 3.2.0 yesterday which includes a number of minor bug fixes since 3.1.0 and also a few nice improvements around the way we handle errors.

He talks about the previous error handling (suppressing them in favor of a bland error page) and how v3.2.0 changes this by writing them to the error log by default. He also talks about changes around the addition of a PHP 7 error handler that works with the PHP 7 Error exception types and functions the same way as the default Exception handler.

tagged: slim slim3 slimeframework error handling improvement errorlog exception php7

Link: https://akrabat.com/improved-error-handling-in-slim-3-2-0/


Trending Topics: