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

Freek Van der Herten:
Making Nova fields translatable
Nov 05, 2018 @ 15:53:23

Freek Van der Herten has a new post to his site sharing a method he's worked up to make Nova fields translatable as a part of the functionality provided by the Laravel Nova too.

Laravel Nova is cool package to quickly create admin interfaces in a Laravel app. Unfortunately there's no support for multiple locales out of the box. A while ago we published a package called nova-translatable that makes any of the built in field types translatable.

He shows how to use the package they've developed to store the translations in a JSON column in the related model's database table. He includes screenshots of the interface and code snippets along the way to help you easily integrate this feature into your resulting administration page(s).

tagged: laravel nova laravelnova field translate tutorial package

Link: https://murze.be/making-nova-fields-translatable

Cees-Jan Kiewiet:
ReactPHP with RecoilPHP: Creating for/http-middleware-psr15-adapter
Feb 09, 2018 @ 17:21:13

Cees-Jan Kiewiet is back with the latest tutorial in his series covering ReactPHP and RecoilPHP. In the previous parts he introduced some of the basic concepts and set up the first bits of code combining ReactPHP and RecoilPHP. In this latest tutorial (part three) he shows how to integrate this with a PSR-15 compliant middleware to evaluate response time.

There are more uses for coroutines than just making working with promises easier. In this post we're diving into the details on how they are used by the Friends of ReactPHP in the PSR-15 Middleware adapter for react/http.

When we started discussing how middleware for react/http should work we also look at the state of PSR-15 at the time. We decided against implementing it directly because of the fully blocking nature of PSR-15, in favour of callable. Which turned into an even better decision when return type hints where added to it to PSR-15. Now I love PSR-15, and middleware in general, which is why I created for/http-middleware-psr15-adapter to bridge the gap.

He starts with the code required to create a normal PSR-15 middleware and recreating the same functionality in a ReactPHP middleware. The article then shows how to use the package he developed to transform the middleware "on the fly" to enable it to be used both as a normal PSR-15 middleware and as a ReactPHP middleware. He ends the post with a word of caution and a bit of advice about using this method of rewriting - basically that just because you can doesn't mean you should.

tagged: reactphp recoil psr15 middleware translate onthefly package tutorial part3 series

Link: https://blog.wyrihaximus.net/2018/02/reactphp-with-recoilphp-party-three-http-middleware-psr-15-adapter/

Zend Framework:
Convert objects to arrays and back with zend-hydrator
Jun 21, 2017 @ 16: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

SitePoint PHP Blog:
Building a Micro Markdown API App with Lumen
May 12, 2015 @ 15:35:57

The SitePoint PHP blog has a new tutorial posted showing you how to create a small API service with Lumen, the recently released microframework from the creators of Laravel. Their example service takes in Markdown content and translates it to be returned as JSON.

If you’ve been using Laravel for a while, you know that it sometimes feels a little heavy for a small application or service. For that same purpose, Taylor, the creator of Laravel built Lumen. A small micro-framework built on top of Laravel Illuminate components, it doesn’t load all the components by default like Eloquent, Blade, Middleware, etc, remaining light as it boots up. We will explore all of that this short tutorial. [...] To illustrate a practical use case for the micro framework, we will be creating a Markdown parser API application where the user can submit a Markdown text and get back the parsed content as JSON. I will be using the league/commonmark package from the PHP League.

They walk you through the installation of a Lumen instance (via Composer) and how to build out the folder structure for things like resources, database configuration and views. They then include the code for the route and controller to take in the Markdown content and translate it out to HTML as a first step. Then they use the same functionality and return the HTML result as a JSON document. Also included is a simple few line call with Guzzle to the API to pass in a Markdown file and fetch the result.

tagged: tutorial markdown lumen microframework commonmark translate api microservice

Link: http://www.sitepoint.com/building-micro-markdown-api-app-lumen/

Symfony Blog:
Help Symfony reach 3 billion people
Jan 17, 2014 @ 17:53:02

The Symfony project wants your help to allow this popular PHP framework to reach more people all around the world (3 billion, according to them) by helping out with translations in the Validator and Security component.

Internationalization has been one of the pillars of the Symfony success since the very beginning. Besides providing tools to translate both the strings and the contents of your applications, Symfony itself is translated into a lot of different languages. [...] Some of these translations are incomplete or haven't been updated for a long time. For that reason, we are organizing a community initiative to improve the internationalization of Symfony.

The strings they're talking about are in the Validator and Security components and are currently in 20 different languages. They've broken up the ones they're needing help on into a few categories:

  • Completed translations
  • Incomplete translations
  • Unavailable translations
  • Additional translations

Obviously, they'd love ones from any of the last four in that list and they include a few steps on how to grab the latest version of Symfony and get translating.

tagged: symfony project framework translate message validator security

Link: http://symfony.com/blog/help-symfony-reach-3-billion-people

SitePoint PHP Blog:
Using Google Translate API with PHP
Oct 31, 2013 @ 18:14:11

On the SitePoint PHP blog Jacek Barecki has a new tutorial showing you how to use the Google Translate API to handle the translation of dynamic input that may have come from other sources (including users).

If your site serves visitors from different countries, you may already have translated all its static content into several languages. But what to do with the content posted daily by the users in comments, opinions and ratings? As this may be as valuable a part of your site as the static content, you should think of finding a way to translate it into other languages. One service that can help is, of course, Google Translate.

He walks you through the process of setting up a Google API account (with screenshots) and how to turn on the Translate API specifically. The Translate API is not a free service, unfortunately, so you'll need to set up some billing information to use it. He then points you to where you can find your API key and shows a sample API call to get the currently supported languages. With that working, he shows you how to make an actual translation call, passing in the text and desired language on the URL and sending it to the API via curl. He also talks some about handling errors based on HTTP response code and the message returned.

tagged: google translate api tutorial curl

Link: http://www.sitepoint.com/using-google-translate-api-php/

Michael Nitschinger:
Benchmarking Cache Transcoders in PHP
Jan 31, 2013 @ 17:31:01

Michael Nitschinger has written up a new post comparing a few different methods for serializing or translating objects to store them in a cache. In it, he compares the PHP serializer, the igbinary extension and translation to JSON.

Storing PHP objects (or simpler data types like arrays) in caches always requires some kind of transformation. You need a way of encoding/decoding data so that it can be stored and loaded properly. In most languages, this process is known as object serialization. PHP provides a mechanism for this out of the box, but in this article we'll also look at igbinary as a drop-in replacement for the default serializer. We also compare the results to object transcoding based on JSON, which is not really an object serialization mechanism but commonly used as a data chache structure which has its own benefits and drawbacks.

He goes through each of the three technologies and includes a snippet of code showing how they'd work in object translation. He also talks about things like the size of the result and the performance of each when the results are looped over. Based on the results of some of his "microbenchmarking" of each of the methods, igbinary came out on top, even faster than PHP's own serialize/unserialize.

tagged: cache transcoder serialize translate object igbinary json

Link:

PHPMaster.com:
In My Language, Please! - Translating WordPress Themes and Plugins
Apr 23, 2012 @ 17:27:36

On PHPMaster.com there's a new tutorial showing how to use the localization support that comes with WordPress to make using different language definitions much simpler.

WordPress itself is translated to many languages and people can use it in their preferred language. But this is not the case with themes, plugins, and other front-end customizations. [...] The purpose of this article is to show you how to translate properly any theme or plugin by using internationalization and localization methods. In brief, internationalization (i18n) deals with making sure strings of text are wrapped in specific function calls.

He starts by introducing the PHP functions (and configuration) you'll need to get the localization support up and running in your plugin, including a few examples of how to translate a string. Also included into the post is an introduction to using the Poedit software to create the different translation files.

tagged: wordpress translate poedit plugin tutorial

Link:

Lorna Mitchell's Blog:
Using iterator_to_array() in PHP
Feb 29, 2012 @ 14:55:52

Lorna Mitchell has a new post to her blog today showing off a lesser-known but very useful function included in PHP - the iterator_to_array function, used to translate things that implement Traversable into arrays.

Someone watching over my shoulder recently had never seen the ubiquitously-useful iterator_to_array() before. [...] Mostly I find this useful when I'm working with collections of data as these often present themselves as an object that you can foreach() over, but you can't dump it directly. If the object in question implements the Traversable interface, you can instead pass it into iterator_to_array to get the data as an array.

She includes a brief snippet of code showing it in use - transforming the results from a MongoDB cursor object back into an array.

tagged: iterator translate array snippet traversable

Link:

SitePoint.com:
How to Create an XML to JSON Proxy Server in PHP
Oct 19, 2011 @ 18:07:08

On SitePoint.com today there's a new post from Craig Buckler showing you how to create a simple XML to JSON proxy server in PHP with a SimpleXML object at its heart.

Unless you're new to this web development lark, you'll know the 'X' in 'AJAX' stands for XML - eXtensible Markup Language. But you’re probably not using XML. If you are, you'd probably prefer not to. All the cool kids are using JSON or JSON-P: it has a smaller payload, is easier to use and faster to process. [...] Fortunately, there are a couple of solutions which allow you to retain the benefits of XML data interchange but provide the ease of JSON in JavaScript. In this article, we’re going to create an XML to JSON proxy server in PHP.

Of course, this will only work with well-formatted XML documents, but it's a quick little hack that pulls in the XML data with a curl request and parses it via SimpleXML and uses json_encode to push it back out as JSON.

tagged: xml translate json proxy server tutorial simplexml

Link:


Trending Topics: