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

SitePoint PHP Blog:
Conquering Instagram with PHP and the Instagram API
Sep 22, 2015 @ 12:57:21

The SitePoint PHP blog has a tutorial posted that shows you how to "conquer" the Instagram API in your PHP application and integrate it into your application with the help of the Guzzle HTTP client.

Instagram’s API allows us to interact with data such as user info, media (photos and videos), likes, comments, and tags. For example, you can search for media around a specific location and filter the results by time. The API also allows us to post comments or like specific media. For now, only uploading media isn’t supported. You can always look at the API endpoints documentation if you want to know about the full functionality.

They start by talking about the two types of API calls (authenticated/unautheticated) and the limits that are included with each. From there the tutorial shows you how to register a new application, get a new client ID and how to use the API console to test out requests. Next they help you get the [Guzzle]((http://guzzle.readthedocs.org/en/latest/) library installed and connect it to the Instagram API with your client credentials. They integrate this into a simple Slim application using Twig as a templating layer to allow for the OAuth approval process, fetching an access token for the later requests.

With the connection made, they show you how to make requests to do thing like make tag searches, get user feed information (and searches), geographic searches and handling pagination. They also include a link to a PHP Instagram library that can help simplify the process, complete with code examples.

tagged: tutorial instagram api introduction library oauth slim twig

Link: http://www.sitepoint.com/conquering-instagram-with-php-and-the-instagram-api/

Barry vd. Heuvel:
Comparing Blade and Twig templates in Laravel
Aug 26, 2015 @ 10:02:32

Anyone that has looked at using a templating library in their application has probably come across both Blade (in Laravel) and the Twig libraries. In a post to his site Barry vd. Heuvel compares these two templating libraries based on their features, security and (briefly) performance.

In my company, we use Twig instead of Blade for our Laravel projects. I know there are a lot of developers that also prefer Twig over Blade. So the question ‘Why choose Twig over Blade?’ often pops up. The reason is usually just a matter of preference, but in this post we’re going to compare the Blade and Twig templating engines side-by-side.

He starts with an "about" for each library, giving some basic background and examples of simple templates. He talks about using Twig in Laravel (vs Blade) and then lists some similarities and differences between the two. Following this high-level list he gets into more detail on each feature of the libraries including:

  • Outputting variables
  • Control structures
  • Template inheritance and sections
  • Security and context

Each section includes a description of the feature and a template example showing how it's put to use. He ends the post with his thoughts on which one you should pick for your project, but notes that, like many things in development, the answer is "it depends" on your project and team's needs.

tagged: compare blade template twig library feature overview example

Link: http://barryvdh.nl/laravel/twig/2015/08/22/comparing-blade-and-twig-templates-in-laravel/

Symfony Blog:
Twig: How to upgrade to 2.0? Deprecation Notices to the Rescue
Aug 19, 2015 @ 10:55:58

The Symfony blog has posted a quick guide with a tip to upgrading to Twig 2.0 made simpler thanks to some built-in deprecation notices.

As Twig 2.0 is approaching fast now, it's time to focus on how to boost adoption of this new major version. [...] Also, the good news is that most of the time, it's possible to make your code work for both major versions (that's important for shared code like bundles). [...] How to make it easy? Like for Symfony, I've now added deprecation notices throughout the code to give Twig users an easy way to upgrade their code (this is available in the upcoming Twig 1.21 version).

The release of v1.21 comes with the deprecation notices already built-in making it easy to find the issues before making the jump up to v2.0. He also includes mentions of two other things included to make it easier: a deprecation collector to gather the issues in one place and a visitor base class diff that can make them compatible with both versions (1.21 and pre-1.21).

tagged: twig2 twig templating version upgrade deprecation notice

Link: http://symfony.com/blog/twig-how-to-upgrade-to-2-0-deprecation-notices-to-the-rescue

SitePoint PHP Blog:
Drupal 8 Theming Revamped – Updates and New Features
Aug 11, 2015 @ 11:08:28

The SitePoint PHP blog has a tutorial posted introducing some of the updates to the theme functionality in Drupal 8 including some new features.

If you are a Drupal developer who has dabbled in theming older versions of Drupal (5, 6, 7) you understand why frustration is the trusty companion of any Drupal themer. Luckily, though, Drupal 8 promises so many improvements that even the Angry Themer is happy for a change. It is only natural we jump in and start looking at what these improvement are.

They talk about the changes in:

  • creating a module and defining its theme
  • that Twig is now the template library
  • updates to template handling
  • how to debug themes/templates
  • working with assets and libraries

Each topic includes a summary of the changes or more information about the topic including links to other resources with more information about each.

tagged: drupal8 update theme feature twig template debug asset library

Link: http://www.sitepoint.com/drupal-8-theming-revamped-updates-and-new-features/

SitePoint PHP Blog:
Building a Custom Twig Filter the TDD Way
Jun 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.

tagged: twig filter tutorial custom timediff extension tdd testdriven development

Link: http://www.sitepoint.com/building-custom-twig-filter-tdd-way/

ServerGrove Blog:
Linters for PHP projects
Jun 03, 2015 @ 12:34:53

In a new post to the ServerGrove blog they look at linting tools for various circumstances including standard PHP, Twig templates and Composer configuration.

Today’s projects are built up from dozens of different components, configuration files, third-party libraries, tests, build scripts, etc. And even if you have the greatest test suite, bad things can happen sometimes. It’s important to catch bugs as early as possible, and syntax validators can be a great (and easy) addition to your continuous integration system. You would be surprised at how many problems are caused by syntax errors. At ServerGrove, we see these kind of problems with our clients almost every day.

Their list shows you how to lint (syntax check) several different types of content:

  • standard PHP code
  • Twig templates
  • Composer configuration
  • XML files
  • Bash scripts
  • JSON files
  • YAML files

Some of them use tools that already come built-in (like PHP's "-l" or Twig's "twig:lint") but others require the use of external software such as xmllint or melody. Command examples are also included for each.

tagged: lint project types twig bash composer xml json yaml tools

Link: http://blog.servergrove.com/2015/06/02/linters-php-projects/

SitePoint PHP Blog:
Integrate Elasticsearch with Silex
Apr 13, 2015 @ 08:38:55

The SitePoint PHP blog has continued their look at integrating Elasticsearch into a simple Silex-based PHP application. In this latest part of the series (part two) they move away from the full Drupal example in part one and go a bit more simple and create a basic site to show a node's detail (content and title).

In the previous article I started exploring the integration between Drupal 7 and the Elasticsearch engine. The goal was to see how we can combine these open source technologies to achieve a high performance application that uses the best of both worlds. [...] We’ll now create a small Silex application that reads data straight from Elasticsearch and returns it to the user.

Using Silex and the same Elasticsearch PHP SDK they create this simple site. The tutorial walks you through the installation of both tools, the configuration of the Elasticsearch client and creating the controllers to respond to the view requests. They also show how to use the Twig templating engine to render the results as a simple page containing the node title, any images attached to it and the body content. The tutorial ends with a brief mention of how this same data could also be rendered as JSON output with a different view handler.

tagged: silex tutorial elasticsearch simple twig template json node drupal

Link: http://www.sitepoint.com/integrate-elasticsearch-silex/

SitePoint PHP Blog:
Use Laravel Contracts to Build a Laravel 5 Twig Package
Mar 16, 2015 @ 11:52:13

The SitePoint PHP blog has a new tutorial posted showing you how to integrate Twig into a Laravel application with the help of the recently added "contracts" feature of the framework. Twig is a templating library that aims to be fast, secure and flexible for data output in multiple contexts.

Laravel 5 is finally out, and with all the awesome features it brings. One of the new architectural changes is the new Contracts Package. In this article we are going to understand the reasoning behind this change and try to build a practical use case using the new Contracts.

He starts with a brief look at what Contracts are and what it means to use them in a Laravel application. He then shows how to define the package installation (via Composer) to pull Twig in and register it with the application for future use. He creates a simple service provider to register Twig and return a new "TwigFactory" instance. This instance extends the "FactoryConnect" implementing the "ViewFactory" and, along with a custom "TwigView" object can be used just like you would normally output information via Blade.

tagged: laravel contract twig output template handling provider interface

Link: http://www.sitepoint.com/use-laravel-contracts-build-laravel-5-twig-package/

SitePoint PHP Blog:
Introduction to Silex – A Symfony Micro-framework
Feb 20, 2015 @ 12:31:40

The SitePoint PHP blog has a tutorial posted today introducing you to one of the more popular PHP microframeworks out there, Silex. This new article jumps right in and shows you how to use it.

Silex is a PHP micro-framework based on Symfony components and inspired by the Sinatra Ruby framework. In this article, we are going to get started with the framework and see the how it fits our needs.

He walks you through the installation of Silex (and Twig) through Composer and the creation of the basic folder structure to build the first app. He then gets into talking about how routes are handled, parameters and linking controllers to routes. He also introduces the use of providers and shows how to implement the one for Twig to use in templating the output of the application.

tagged: silex microframework introduction tutorial symfony twig

Link: http://www.sitepoint.com/introduction-silex-symfony-micro-framework/

Symfony Blog:
New in Symfony 2.7: Twig as a First-Class Citizen
Jan 15, 2015 @ 13:16:18

The Symfony blog has a new post about a change coming in the next version of the popular PHP framework - treating Twig as a first-class citizen. This update removes the abstraction layer that was introduced for it to be used in templating.

When I started to work on Symfony2, Twig didn't exist. Anyway, to ease using PHP as a templating engine, I created the Symfony Templating Component. Later on, not very satisfied with using PHP as a templating language, I decided to create a new templating language, Twig, based on the Python Jinja2 language. And Symfony2 became the first popular framework to adopt a non-PHP templating engine in core. [...] But what would Twig as a First-Class Citizen mean in Symfony2 then? To be able to support PHP and Twig in Symfony, we added an abstraction layer. [...] For Symfony 3.0, I'd like to extract the Templating Component into an independent library (for the few people using PHP with Symfony) but I'd also like for Twig to be front and center in the framework. The good news is that most of the work has already been done in Symfony 2.7.

He compares the two methods for using Twig, one in 2.6 and the other in 2.7, showing both the reduction in code needed and the overall speed improvement gained by removing the excess layer. He includes a link to the Blackfire.io reports showing the difference in the metrics with a total of 48 classes less to load just by removing this layer.

tagged: symfony twig firstclass citizen templating abstraction performance

Link: http://symfony.com/blog/new-in-symfony-2-7-twig-as-a-first-class-citizen