News Feed

News Archive
feed this:

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

Michael Dyrynda:
Filtering models with Eloquent in Laravel
March 06, 2015 @ 10:14:12

Michael Dyrynda has a recent post about handling matching and limiting results in Eloquent models in a Larvel-based application.

Say you have a users table with the following fields in it name, email, city, state, zip. You may want to provide fuzzy searching for the name, email, or city and exact matching for the state and zipfields. Why fuzzy matching for only some of the fields? Well, you might want to search for everyone whose name contains Michael or has has an address. Be mindful of the latter; it will expose a large dataset if you're not careful in restricting access to the functionality. You probably wouldn't want to allow it in anything bigger than a proof of concept (which this is!).

He goes through the model process, showing how to set up a simple model with the fields mentioned and make use of query scopes to limit returned results. Code is included showing how to define the "scopeFilter" method in the model and call the "User" model instance with the "filter" method. The example limits the results to only the users with a value in the "name" and "state" field.

0 comments voice your opinion now!
filter model results tutorial eloquent laravel scope query


Edd Mann:
Implementing Streams in PHP
January 16, 2015 @ 10:09:22

Edd Mann has a new post today looking at implementing streams in your PHP applications. In this case we're not talking about the streams built into PHP but the concept of a source of information that only produces the next item when requested (aka "lazy loading").

Typically, when we think about a list of elements we assume there is both a start and finite end. In this example the list has been precomputed and stored for subsequent traversal and transformation. If instead, we replaced the finite ending with a promise to return the next element in the sequence, we would have the architecture to provide infinite lists. Not only would these lists be capable of generating infinite elements, but they would also be lazy, only producing the next element in the sequence when absolutely required. This concept is called a Stream, commonly also referred to as a lazy list, and is a foundational concept in languages such as Haskell.

He talks about how streams of data should be interacted with differently than a finite list of data and the promises they're based on to provide the right data. He shows two different approaches to implementing a an object to stream data from - a class-based method and one that uses generators. Sample code is provided for each with the generator approach being a bit shorter as they're designed to lazy load items as requested.

0 comments voice your opinion now!
stream data lazyload generator class iterator tutorial


Rob Allen:
Overriding the built-in Twig date filter
December 16, 2014 @ 09:45:31

In his latest post Rob Allen shows a way you can override the default Twig date filter with your own custom Date extension handling.

In one project that I'm working on, I'm using Twig and needed to format a date received from an API. The date string received is of the style "YYYYMMDD", however date produced an unexpected output. [...] This surprised me. Then I thought about it some more and realised that the date filter is treating my date string as a unix timestamp. I investigated and discovered the problem in twig_date_converter.

He includes some example code you'll need to create the custom renderer. As part of the internals of how Twig formats the date currently is internal and can't be changed, he opted to override the extension itself. As a result, the call to the filter is exactly the same as before, the output results are just formatted more correctly.

0 comments voice your opinion now!
twig override default date filter custom extension


Michael Dowling:
Transducers in PHP
December 08, 2014 @ 09:28:48

Michael Dowling has a new post to his site announcing a project he's recent released to try to bring some of the functionality of Clojure to PHP with the introduction of transducers.

Rich Hickey recently announced that transducers are going to be added to Clojure, and it prompted a bit brief announcement, Hickey followed up with a couple videos that describe transducers in much more detail: Transducers and Inside Transducers + more.async. Transducers are a very powerful concept that can be utilized in almost any language. In fact, they have been ported to various other languages including JavaScript (2), Python, Ruby, Java, and Go. And now…transducers are available in PHP via transducers.php!

He starts with an official definition of what a transducer is from the Clojure documentation then explains it in a bit more layman's terms as "a fancy way of saying that you can use functions like map and filter on basically any type of data source (not just sequences)" and can output any kind of structure as a result. He then gets into some code examples using his project showing eager and lazy evaluation, how they're composable and a list of the ones the library makes available (and what they do). He then gets into a more complete example of their application with a streams example, working with/modifying a string. He ends the post looking at how to create your own custom transducer and how they compare to generators.

0 comments voice your opinion now!
transducer conjure step data data structure introduction library


SitePoint PHP Blog:
Getting Started with FigDice
November 21, 2014 @ 12:19:12

The SitePoint PHP blog has posted the second part of their series highlighting the FigDice template rendering system. In this latest article Lukas White focuses on FigDice's ability to "pull" data into templates as needed rather than having it injected.

Amongst the many templating systems out there, most work in pretty much the same way; variables are "injected" using some syntax or another, be it curly braces, percentage signs or whatever that library's convention happens to be. They'll usually have basic control structures, such as if...then and, of course, iteration. FigDice, however, takes an altogether different approach. Inspired by PHPTAL - the subject of a future article - it gives the view layer the responsibility of "pulling" in the data it requires, rather than relying on controllers to assemble and "push" it into the templates.

He walks you through the installation of the tool (via Composer) and how to create a basic FigDice view to work with (including template loading). He uses a sample Silex-based application for his examples, making a layout with the FigDice additions to the attributes. He then shows how to make the template for the main index page with a "mute" region for the include logic. He shows how to include this basic template into the view and render it directly as output. Next he shows how to integrate data with the template, pulling in "tweets" from an array dataset via a loop (walk) and a factory to provide the template the data.

0 comments voice your opinion now!
figdice template tutorial series part2 data integration


Stanislav Malyshev:
unserialize() and being practical
November 04, 2014 @ 10:49:40

Stanislav Malyshev has a new post to his site talking about his proposal for a filtered unserialize change and why he sees it as a practical next step.

I have recently revived my "filtered unserialize()" RFC and I plan to put it to vote today. Before I do that, I'd like to outline the arguments on why I think it is a good thing and put it in a somewhat larger context. It is known that using unserialize() on outside data can lead to trouble unless you are very careful. Which in projects large enough usually means "always", since practically you rarely can predict all interactions amongst a million lines of code. So, what can we do?

He touches on three points that would make it difficult to just not use it this way (on external data) including the fact that there's not really any other way to work with serialized data in PHP. He suggests that by adding filtering to the unserialize handling of the language it can protect from issues around working with serialized external data.

Is this a security measure? [...] Yes, it does not provide perfect security, and yes, you should not rely only on that for security. Security, much like ogres and onions, has layers. So this is trying to provide one more layer - in case that is what you need.
0 comments voice your opinion now!
unserialize rfc filter practical security reasons

Building Advanced Email Features With IMAP and PHP
October 21, 2014 @ 12:19:47

On the site they've posted a tutorial showing you how to build advanced features with IMAP and PHP. He bases it on the SimplifyEmail project and incldues examples of three different features to get you started.

Analysis of my own email showed I was receiving email from more than 230 automated senders, far fewer actual people. I was tired of constructing filters in Gmail and filling in a myriad of unsubscribe forms. I wanted to have more control over managing my email and simplifying my life. Finally, this past year, I decided to build the features I needed. The result is Simplify Email (SE), a small web app you can host yourself which offers a variety of cool new email features all of which you can check out on the project website. The coolest thing about SE is that it's a platform for reading, analyzing, routing and managing your email - the possibilities abound. Simplify Email is essentially a programmable playground for "hacking" your own email.

His three examples show you how to:

  • Checking your inbox and filter messages
  • Implement a Whitelist challenge to unknown senders
  • Reporting unanswered email

Each of these comes with plenty of code examples, screenshots and output examples (as well as some places where you might need to change some SE configuration values).

0 comments voice your opinion now!
advanced email imap tutorial feature simpleemail filter whitelist reporting


Matt Stauffer:
Laravel 5.0 - Middleware (replacing Filters)
October 15, 2014 @ 10:18:00

In a new post to his site Matt Stauffer looks at a feature of the upcoming version 5 of the Laravel framework, middleware, and how it will replace the current Filter handling. This is part nine in a series about the new features coming in Laravel (the rest are linked at the top of the article).

If you've been following along with my previous blog posts about Laravel 5.0, you may have noticed that route filters were first moved to be their own directory and class structure, and then eventually they mysteriously disappeared. You may have even noticed that references to Middleware showed up in their place.

He starts off by defining what "middleware" actually is and how it fits into the overall execution flow of the application. He describes it as "a series of wrappers around your application that decorate the requests and the responses in a way that isn't a part of your application logic." He then gets into the code examples, showing how to write a simple Laravel-friendly middleware that blocks odd port requests to the application. He includes the configuration updates to integrate it, how to control where it runs and using before and after "filters" inside the middleware.

0 comments voice your opinion now!
series part9 tutorial laravel framework filter middleware introduction


Paul Jones:
Action-Domain-Responder and the "Domain Payload" Pattern
October 01, 2014 @ 10:16:11

Paul Jones has a new post with more information about his proposed "Action-Domain-Responder" design pattern (a replacement for the typical MVC) and suggests a new piece, the Domain Payload pattern. This pattern would use a domain payload object to wrap the data and provide the responder with additional handling and context.

In Action-Domain-Responder the Action passes input to the Domain layer, which then returns some data for the Action to pass to the Responder. In simple scenarios, it might be enough for the Responder to inspect the data to determine how it should present that data. In more complex scenarios, though, it would make more sense for the Domain to pass back the data in a way that indicates the status of the data. Instead of the Responder inspecting the Domain results, the Domain should tell us what kind of results they are.

He shows a code example of this Domain Payload object in action, starting with some typical MVC code and refactoring it along the way into an ADR structure. He shifts from a typical model into a more domain-driven approach and describes the wrapping of the data in the payload, context for the contents (even just a class name helps) and how those relate to the actual output. You can find the resulting code in this example over on Paul's GitHub account.

0 comments voice your opinion now!
action domain responder mvc adr payload wrapper context data


Sameer Borate:
Data cleaning in PHP applications
September 22, 2014 @ 09:41:56

Sameer Borate has a new post today showing the use of a "cleaner" library to help sanitize the data input to your application. In this tutorial he introduces you to Mr Clean, an "extendible PHP Data Cleaner".

Scrubbers or data cleaners are an important part of the data transformation process. Whenever you are involved in some data import or export process, data scrubbers can help you clean and standardize your data elements before storing. There are many libraries that help in sanitizing and cleaning data. One such I recently found is mr-clean; it is a extendible PHP Data Cleaner that you can use in your PHP applications to clean heterogeneous data before storing it in your database or other persistent storage like CSV files.

He walks you through the installation (via Composer) and the creation of an instance of the main "cleaner" object. He then provides a few examples of some data scrubbing features it offers:

  • Basic scrubbing (trim, stripping HTML tags, etc)
  • Booleans
  • Filtering HTML
  • Stripping CSS attributes
  • Nullify
  • Null if repeated
  • Strip Phone Number
  • Pre/Post scrubbing handling

He finishes up the post with a look at creating a custom scrubber class, an "only numeric" handler that replaces any character that's not a number in a string with an empty string (removing it).

0 comments voice your opinion now!
data cleaning mrclean library introduction tutorial


Community Events

Don't see your event here?
Let us know!

framework development conference laravel series introduction laravel5 podcast voicesoftheelephpant extension api library unittest community opinion psr7 language release video interview

All content copyright, 2015 :: - Powered by the Solar PHP Framework