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

Sergey Zhuk:
Promise-Based Cache With ReactPHP
Sep 20, 2017 @ 10:11:55

Sergey Zhuk has written up a tutorial showing you how to implement promise-based caching with ReactPHP, a continuation of a previous post.

In the previous article, we have already touched caching (when caching DNS records). It is an asynchronous promise-based Cache Component. The idea behind this component is to provide a promise-based CacheInterface and instead of waiting for a result to be retrieved from a cache the client code gets a promise. If there is a value in a cache the fulfilled with this value promise is returned. If there is no value by a specified key the rejected promise returns.

He starts by defining the caching interface and how it would look in use to set/get a cache value. He shows how to update this with a "done" handler to output the value when the get is complete. He continues on showing how to use a fallback handler: either "otherwise" or "then". He also shows how these can be chained together to make more complex operations. The post ends with an example of this caching component in action and links to other library that use the same ideas.

tagged: promise cache reactphp get set tutorial component interface

Link: http://seregazhuk.github.io/2017/09/15/reactphp-cache/

Zend Framework Blog:
Logging PHP applications
Sep 13, 2017 @ 10:56:38

On the Zend Framework blog there's a new post in their series highlighting components of the main framework. In this new tutorial they look a the Zend-log component and how it can be used for logging in your application.

Every PHP application generates errors, warnings, and notices and throws exceptions. If we do not log this information, we lose a way to identify and solve problems at runtime. Moreover, we may need to log specific actions such as a user login and logout attempts. All such information should be filtered and stored in an efficient way.

[...] Zend Framework offers a logging component, zend-log; the library can be used as a general purpose logging system. It supports multiple log backends, formatting messages sent to the log, and filtering messages from being logged.

The tutorial then walks you through the installation of the component and some basic usage of it to write directory to the 'php://output' stream. It then shows you how to set up custom formatting on the message and a few other examples of it in use:

  • logging PHP events (errors/exceptions)
  • using the built-in data (level) filtering functionality
  • custom processors
  • working with multiple backends for storage

The post ends with a look at using the Zend-log package in either a traditional MVC application or one based more on middleware (like an Expressive application).

tagged: zendframework component zendlog logging tutorial mvc middleware

Link: https://framework.zend.com/blog/2017-09-12-zend-log.html

Symfony Blog:
The Symfony Unicorn: 1 Billion Downloads
Sep 12, 2017 @ 12:55:32

On the Symfony blog they talk about a major milestone the framework has reached: one billion downloads of the components that make up the Symfony framework.

A Symfony unicorn!!? It's real! On September 5th, the Symfony Components crossed a huge landmark: the 1 billionth download! This happened less than one year after the last milestone: 500 million downloads. That's right: the PHP community downloaded the Symfony components more than 500 million times in just one year. That's well over 1 million downloads every day!

There are so many people that have made this possible, and we would like to thank all of you who have supported Symfony: developers, contributors, companies, evangelists and - of course - users! There are now over 80 Symfony packages and 1,500 people have contributed code to these. None of this is possible without your help. Congrats!

The post then talks about the current state of the components (including their download numbers), their backwards compatibility promise and how it relates to the upcoming Symfony 4 release.

tagged: symfony component billion download achievement framework

Link: http://symfony.com/blog/the-symfony-unicorn-1-billion-downloads

Omniceps.com:
Introduction to Latte | Best PHP templating engine
Aug 17, 2017 @ 11:30:15

On the Omniceps site there's a new post introducing you to Latte, the templating component of the Nette framework that can be used independently.

A PHP templating engine is the one which gives you ability to write html for your clients efficiently using PHP variables.

However, PHP was originally built to be a templating engine, but yet it was never used primarily as a templating engine. There has been so many PHP templating engine so far in the market since PHP evolved, but none of them has made as great impression on us as Latte. Introducing you to Latte, one of the best PHP templating engine you have come across so far. Also Latte protects your web app from vulnerabilities like XSS (cross site scripting).

The tutorial starts with an example comparing "plain old PHP" templating with the Latte version for a foreach loop. Next they show how to install the Latte component and the two different kinds of macros the tool includes. They also talk about filters, performance concerns, context aware escaping and "pretty output" options.

tagged: nette framework template component latte introduction tutorial

Link: http://www.omniceps.com/latte-best-php-templating-engine/

Zend Framework Blog:
REST Representations for Expressive
Aug 09, 2017 @ 10:12:18

On the Zend Framework blog there's a new post from project lead Matthew Weier O'Phinney covering REST representations in Expressive and the release of two new components to help with their implementation.

We've been working towards our various Apigility on Expressive goals, and have recently published two new components: zend-problem-details and zend-expressive-hal.

These components provide response representations for APIs built with PSR-7 middleware. Specifically, they provide: Problem Details for HTTP APIs (RFC 7807) and Hypertext Application Language (HAL).

These two formats provide both JSON and XML representation options (the latter through a secondary proposal).

The post then goes on to talk about the idea of "representations" and how it relates back to the structure of the data returned and how the user knows what operations they can take on it. He also talks about the differences between normal responses and errors and gives example output helping to illustrate his points. The remainder of the post gets into the details of the two new components, their use and how to implement them in Expressive.

tagged: zendexpressive rest representation data structure response api component tutorial

Link: https://framework.zend.com/blog/2017-08-08-expressive-rest-representations.html

Zend Framework:
Convert objects to arrays and back with zend-hydrator
Jun 21, 2017 @ 11:32:01

The Zend Framework blog has posted another in their series of component spotlights, focusing in on a single component of the framework and its use. In this latest article they cover the zend-hydrator component, useful for converting objects to arrays and back.

APIs are all the rage these days, and a tremendous number of them are being written in PHP. When APIs were first gaining popularity, this seemed like a match made in heaven: query the database, pass the results to json_encode(), and voilĂ ! API payload! In reverse, it's json_decode(), pass the data to the database, and done!

Modern day professional PHP, however, is skewing towards usage of value objects and entities, but we're still creating APIs. [...] Zend Framework's answer to that question is zend-hydrator. Hydrators can extract an associative array of data from an object, and hydrate an object from an associative array of data.

They start with the command to get the zend-hydrator package installed (and a dependency they'll need for their examples, zend-filter). A code example is included that shows how to convert a "book" object to an array using the ReflectionHydrator. Next is an example of switching it back, changing the array of data back into a book object. Next comes the integration with zend-filter, showing how to filter values out of objects/arrays you might not want in the end result by adding the filter to the hydrator. Also included are examples of modifying data (strategies), filtering on property names, delegation of the translation based on object type and a few other features included in the component that could be helpful.

tagged: zendframework component zendhydrator tutorial introduction filter translate

Link: https://framework.zend.com/blog/2017-06-21-zend-hydrator.html

Rob Allen:
Simple way to add a filter to Zend-InputFilter
Jun 21, 2017 @ 09:16:29

Rob Allen has a quick new post to his site sharing a simple way to add a filter to the Zend-InputFilter component when it's in use on your site.

Using Zend-InputFilter is remarkably easy to use. [...] How do you add your filter to it though?

He starts with an example of putting the component to use in requiring and filtering the value in "my_field" for the data provided. He then shows how to add his "simple filter that does absolutely nothing", the MyFilter, to the current set. He also shows the creation of a "filter factory" class that registers the custom filter into the chain with an alias of "MyFIlter". You can then use it just like you would any other filter and define it in your rules specification.

tagged: zendframework zendinputfilter component custom filter tutorial factory

Link: https://akrabat.com/simple-way-to-add-a-filter-to-zend-inputfilter/

Nikola Posa:
Using Monolog with Zend Service Manager
Jun 16, 2017 @ 12:09:27

Nikola Posa has a new post to his site showing you how to can combine Monolog for logging with Zend Service Manager, a component from the Zend Framework, defining the logger as a service that can be easily used (and re-used) across an application.

Without any doubt, Monolog and Zend Service Manager are two libraries that are almost always found in the composer.json file require section of my projects. In case you didn't know, Monolog is a PSR-3 compliant logging library that allows you to save logs to various storage types and web services, while Zend Service Manager is a PSR-11 compliant dependency injection container and a service locator implementation that facilitates management of application dependencies.

In this post I'm gonna show you how the two can work together.

He starts with an example of configuring the ServiceManager instance with a factory dependency that manually creates the Monolog logger inline. While this works, it's not idea, mixing configuration and functionality. He shows how to refactor the same functionality into a factory class that performs the same function but isolates it from the configuration. He then takes this further and separates out the environment-specific configuration from the handling and, finally, shows the creation of a more general logging factory that allows the definition of different kinds of loggers based on the need.

tagged: monolog zendservicemanager component tutorial combine factory configuration

Link: http://blog.nikolaposa.in.rs/2017/06/12/using-monolog-with-zend-service-manager/

Zend Framework Blog:
Validate data using zend-inputfilter
Jun 16, 2017 @ 09:22:37

Matthew Weier O'Phinney is back on the Zend Framework blog today with a spotlight on another component of the Zend Framework. This time he features zend-inputfilter, a useful component for filtering the data coming into your application from your users.

In our previous two posts, we covered zend-filter and zend-validator. With these two components, you now have the tools necessary to ensure any given user input is valid, fulfilling the first half of the "filter input, escape output" mantra.

[...] To solve [the single shot validation] problem, Zend Framework provides zend-inputfilter. An input filter aggregates one or more inputs, any one of which may also be another input filter, allowing you to validate complex, multi-set, and nested set values.

As in the other tutorials in the series, Matthew walks you through the installation of the component via Composer and briefly describes how it operates. He then includes a code example of creating a new InputFilter instance, making inputs, attaching validators to them and then ensuring everything validates in the chain with an isValid call. He then covers input specifications - configurations based on array values - to define validators on the input elements. He ends the post looking at input filters, how to manage them and defining them by specification. He also mentions a few other pieces fo functionality the component includes but he didn't cover here.

tagged: zendinputfilter component zendframework series input filter chain

Link: https://framework.zend.com/blog/2017-06-15-zend-inputfilter.html

Zend Framework Blog:
Validate input using zend-validator
Jun 14, 2017 @ 11:25:36

The Zend Framework blog has continued their series spotlighting various components of the framework with their latest installment. In this latest tutorial they cover the zend-validator component used to validate data against a set of rules for correctness.

In our previous post, we covered zend-filter, The filters in zend-filter are generally used to pre-filter or normalize incoming data. This is all well and good, but we still don't know if the data is valid. That's where zend-validator comes in.

The post starts with showing how to get the component installed via Composer and the optional dependency of the zend-service-manager component (to handle the use of ValidatorChain functionality). Code is included showing the interface the validators all conform to and an example of the validator in use. It then covers some of the built-in validation options and how to build up a validator "chain" of multiple checks. It also shows how to break the validation if one fails, setting priority (order of execution), evaluating values in certain contexts and registering your own custom validators.

tagged: zendvalidator zendframework validation tutorial introduction component series

Link: https://framework.zend.com/blog/2017-06-13-zend-validator.html