News Feed

News Archive
feed this:

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

SitePoint PHP Blog:
Building a Custom Twig Filter the TDD Way
June 08, 2015 @ 13:40:18

The SitePoint PHP blog has a new tutorial posted showing you how to create your own Twig template following a TDD (test-driven development) mentality.

Twig is a powerful, yet easy to master template engine. It is also my personal favorite as all my web development is based on either Symfony or Silex. Apart from its core syntax ({{ ... }} and {% ... %}), Twig has built-in support for various filters. A filter is like a "converter". It receives certain original data (a string, a number, a date, etc) and by applying a conversion, outputs the data in a new form (as a string, a number, a date, etc).

He starts with a brief introduction to what filters in Twig are and some simple ways to use them. From there he gets into building a custom filter, starting with the tests first (hence the test-driven design). He walks you through the creation of a filter that turns times into relative strings, like "Just now" or "Within an hour". He shows how to make the extension classes and integrate it into a Symfony application.

0 comments voice your opinion now!
twig filter tutorial custom timediff extension tdd testdriven development


SitePoint PHP Blog:
Pagination with jQuery, AJAX and PHP
May 28, 2015 @ 09:46:57

The SitePoint PHP blog has a new tutorial posted showing you how to set up pagination with jQuery and PHP using a simple Silex-based application.

n this article, we're going to explain how easy it is to paginate your data set using PHP and AJAX via jQuery. We're also going to use the Silex framework for simplicity.

The data he's going to paginate through is a list of "people" data with ID, name and age values. He starts by helping you get Silex installed and a new project created. With that in place, he shows how to inject the database connection (PDO) into the application and set up the simple route to output the "people" data back to the waiting Javascript. The route includes a page number value that's used in the LIMIT statement to segment the results into pages. He also includes another route that returns a total count of people records so the pagination knows when to end. With the backend in place, he then moves to the frontend, showing the complete code to get the page records and populate them into the page (via a list).

0 comments voice your opinion now!
pagination tutorial ajax jquery silex people data


Anthony Ferrara:
Tries and Lexers
May 18, 2015 @ 09:47:32

Anthony Ferrara has an interesting new post to his site talking about tries and lexers, two pieces of a puzzle that are used during script execution. In this case, he's tried his hand at writing a parser which, naturally, lead to needing a lexer.

Lately I have been playing around with a few experimental projects. The current one started when I tried to make a templating engine. Not just an ordinary one, but one that understood the context of a variable so it could encode/escape it properly. [...] So, while working on the templating engine, I needed to build a parser. Well, actually, I needed to build 4 parsers. [...] I decided to hand write this dual-mode parser. It went a lot easier than I expected. In a few hours, I had the prototype built which could fully parse Twig-style syntax (or a subset of it) including a more-or-less standards-compliant HTML parser. [...] But I ran into a problem. I didn't have a lexer...

He starts with a brief description of what a lexer is and provides a simple example of an expression and how it would be parsed into its tokens. He then talks about the trie, a method for "walking" the input and representing the results in a tree structure. He shows a simple implementation of it in PHP, iterating over a set of tokens and the array results it produces. He then takes this and expands it out a bit into a "lex" function that iterates over the string and compiles the found tokens.

From there he comes back to the subject of Javascript, pointing out that it's a lot looser than PHP in how it even just allows numbers to be defined. His testing showed a major issue though - memory consumption. He found that a regular expression method consumed too much and tried compiling out to classes instead (and found it much faster once the process was going).

0 comments voice your opinion now!
lexer parser example javascript tries tree data structure


Eric Barnes:
How To Validate an array of form fields with Laravel
April 07, 2015 @ 09:48:34

Eric Barnes has a new post to his site showing you how to validate form input in a Laravel application using the form requests feature.

If you've used Laravel's form validation for any length of time, then you know it's a powerful system. It makes the tedious task of validation very simple while still keeping the door open for complex rules. In this tutorial, I want to show you a simple and easy way of validating forms that contain dynamic fields. A common use case for these types of forms is when you would like to allow a user to add more fields to a form.

His example uses a form with a handful of text fields rendered with a simple "for" loop in the template. He then helps you make a new Request instance (OrderRequest) and adding custom validation rules into its "rules" method. In this case, he sets a rule that the content is required and can be no longer than 255 characters. He also shows how to use the custom messages functionality, defining custom values for each of the form's fields.

0 comments voice your opinion now!
validate form data laravel formrequests example tutorial


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


Community Events

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

php7 language yii2 composer opinion podcast introduction community part2 list application framework example symfony interview api project series programming laravel

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