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

SitePoint PHP Blog:
RESTful Remote Object Proxies with ProxyManager
Sep 13, 2016 @ 11:03:15

The SitePoint PHP blog has posted a tutorial introducing the use of ProxyManager in RESTful APIs to help interface your API endpoints directly with backend objects for the typical CRUD (create, read, update, delete) handling a REST API provides. ProxyManager is a tool created by Marco Pivetta to creating various kinds of proxies through a set of factory classes.

The proxy pattern is another cool design pattern in software development. A proxy is a class working as an interface to another class or web service. For the sake of simplicity, we’ll refer to proxied classes as subjects throughout the rest of the article. A proxy usually implements the same interface as the subject, so it looks like we’re calling the methods directly on the subject.

They start with a brief overview of proxies and the proxy design pattern for those not familiar then "cut to the chase" and show how to hook in ProxyManager via a custom adapter for the REST endpoints. They help you get all dependencies needed installed (via Composer) and the creation of a simple API using Silex and it's provider handling. They then create the application, set up the front controller and configure the relation between endpoint and proxy. Code is then included to create the required factories, interfaces and mappings. The tutorial wraps up with an example of using the API you've just created.

tagged: rest api tutorial proxymanager example factory classes

Link: https://www.sitepoint.com/restful-remote-object-proxies-with-proxymanager/

Cloudways Blog:
How To Create Simple Rest API In Symfony 3.1
Aug 30, 2016 @ 12:59:10

The Cloudways blog has posted a new tutorial helping you get up and running quickly with a simple REST API written using the Symfony framework. In the article they not only explain how to create the API but also include a bit of REST theory for those not completely familiar with the terms and functionality involved.

Symfony is fast becoming the favourite framework among developers for rapid application development. And despite releasing Symfony 3.1 and 3.2 in the previous quarter, they are still introducing many changes and upgrades. If you’re still using the previous versions, you must upgrade Symfony Framework to the latest and stable version 3.1.

Yes! We know that Symfony is one of the best frameworks to develop rest API, so in this article we will make simple rest API in Symfony 3.1. I am assuming that you’ve already setup PHPstack application on Cloudways with Symfony installed, but if you haven’t, follow this installation guide.

They briefly talk about the REST HTTP verb types and what kind of actions they relate to. With that defined the tutorial then gets into the requirements including the installation of two bundles: JMSSerializerBundle and NelmioCorsBundle. From there examples of configuration changes, commands to make users and execute migrations on the local database are included. With this system set up they include sample code for each HTTP verb type letting you perform the actions on the User entity (create, read, update and delete).

tagged: symfony rest api simple tutorial introduction phpstack

Link: https://www.cloudways.com/blog/rest-api-in-symfony-3-1/

Laravel News:
How to use WordPress as a backend for a Laravel Application
Aug 17, 2016 @ 12:51:08

The Laravel News site has posted an interesting tutorial where they describe the use of WordPress as a backend for a Laravel application. This setup is based on the Laravel News' own experience with it in the recent refactoring of the site.

Last week I relaunched Laravel News, and the new site is running on Laravel with WordPress as the backend. I’ve been using WordPress for the past two years, and I’ve grown to enjoy the features that it provides. The publishing experience, the media manager, the mobile app, and Jetpack for tracking stats.

I wasn’t ready to give these features up, and I didn’t have the time to build my own system, so I decided to keep WordPress and just use an API plugin to pull all the content I needed out, then store it in my Laravel application. In this tutorial, I wanted to outline how I set it all up.

While he did find other methods for linking the two, they didn't quite fit with what he wanted so he worked up his own. The content is then synced via a recurring task pulling over posts, categories and tags. He gets into the WordPress REST API first, showing the extraction of the posts from the API and pushing them into a Laravel collection. There's also an example of how to sync a post with the database (API) and how to create a new post in a similar way. Also included is the code to get the featured image, get the category for a post and sync the tag values. The tutorial finishes with the code for the sync command and pushing it into the scheduler.

tagged: wordpress backend laravel application tutorial rest api

Link: https://laravel-news.com/2016/08/wordpress-api-with-laravel/

Alejandro Celaya:
Dispatch REST-like requests with a single controller class in Zend Expressive
Jun 27, 2016 @ 10:21:25

In a new post to his site Alejandro Celaya shows you how to dispatch REST-like requests in Zend Expressive using a single-controller method.

I was digging into Zend Expressive and how to use controllers that allow me to share dependencies between different routes, instead of having to use different middlewares every time. Abdul wrote a great article on this subject that you can find here, which also became part of Expressive's cookbook some time later.

This is a perfect approach that easily allows to reuse some code, but then I thought how to do something similar in a rest environment, having a single class with different dispatchable methods that will be called depending on the request's HTTP method. This is a possible solution based on ZF2's AbstractRestfulController.

He starts by creating an AbstractRestController class to handle the basics of the REST handling, essentially matching verbs to their actions. He then extends this with a RestUserController class that overrides the necessary methods for only the HTTP verbs you want to change. He then shows how to register the route so it can be used by any request verb type (GET, POST, PUT, etc).

tagged: zendexpressive tutorial rest request verb http zendframework2 abstractcontroller

Link: http://blog.alejandrocelaya.com/2016/06/24/dispatch-rest-like-requests-with-a-single-controller-class-in-zend-expressive/

Symfony Finland:
GraphQL with PHP and the Symfony Framework
May 16, 2016 @ 12:19:09

The Symfony Finland site has a recent post giving an overview of GraphQL and Symfony, combing the GraphQL query language (RESTish handling) from Facebook with your application.

The origins of GraphQL stem from the needs that Facebook's mobile applications had (and continue to have). They needed a data-fetching API that was flexible enough to describe all the different kinds of data that the social network had available. [...] Back in September 2015 GraphQL was already powering Billions of API calls a day at Facebook. [...] The core idea of GraphQL is to send a simple string to the server. This string is then interpreted by the server and it sends back a JSON payload that responds to follows the structure of the query itself.

The post includes an example of what the request and response from a GraphQL query might look like for a social network's data. They also link to several PHP libraries that have come up around the functionality making it easier to integrate. There's also links to some Symfony bundles that provide functionality to make your own GraphQL servers.

tagged: graphql symfony bundle introduction facebook rest query json library

Link: https://www.symfony.fi/entry/graphql-with-php-and-the-symfony-framework

CloudWays Blog:
Samantha Quiñones Shares Insights On APIs, Elasticsearch And R-Language Programming
Apr 18, 2016 @ 09:05:32

On the Cloudways blog they've posted another interview with a member of the PHP community. In this latest post they talk with Samantha Quiñones, a principal software engineer at AOL and user group organizer.

Samantha Quiñones is the principal software engineer at AOL, organizer of DC PHP user group and a speaker. She is a certified R-programmer with vast experience in developing REST APIs. In her interview with Cloudways she talks about the early years of her career, her experience as a speaker and in the end gives advice on who to follow in the PHP community.

In the interview Samantha answers questions about:

  • how she got started in her career as a developer
  • why she started speaking and where she'll be speaking
  • her experience as a security developer at Saint
  • what she enjoys doing when not programming

Finally, she makes a recommendation of someone to follow if you want to get involved in the wider PHP community or just want a good person to start with: Cal Evans.

tagged: cloudways interview samanthaquinones api rest elasticsearch rprogramming community

Link: http://www.cloudways.com/blog/samantha-quinones-interview/

TutsPlus.com:
WP REST API: Internals and Customization
Apr 14, 2016 @ 11:24:28

TutsPlus.com has posted the latest part of their series focusing on the WordPress REST API. In this new part of the series they look at some of the internals of the API code and the customizations you can make on the data returned.

In the previous part of the series, we learned about creating, updating, and deleting content remotely through the WP REST API. It allows us to create platform-independent applications that work seamlessly with a WordPress powered back-end, providing a rich experience to the user.

In the current part of the series, we will take a look at the internals of the WP REST API and how they work together to power the API. After that, we will learn to modify server responses for the default endpoints to include custom fields.

They walk you through a few different topics around the API including the internal classes that power it, how to modify the server and making custom fields editable. There's a bit of code involved when it comes to modifying the custom fields in the response and registering an editable field. The rest is mostly about configuration and what methods are doing what during the request.

tagged: wordpress tutorial wpapi api rest internals customization fields editable

Link: http://code.tutsplus.com/tutorials/wp-rest-api-internals-and-customization--cms-24945

Jeremy Curny:
Symfony 3 - REST API
Apr 04, 2016 @ 13:19:26

In this post to his site Jeremy Curny briefly walks you through the creation of a REST API using the Symfony 3 framework and several components/bundles to add in common functionality.

He includes the commands and dependencies you'll need to get the system set up:

  • Initialize the project
  • Install dependencies (including the FosRestBundle, JMSSerializerBundle and NelmioCorsBundle bundles)
  • Register bundles
  • Configure bundles
  • Making the first route

He ends with the command to run the built-in web server and be able to test out the result. The simple action he's created can then be called using a GET request on the default/index route with a 200 OK response with "hello world" content.

tagged: symfony3 symfony framework tutorial rest api project

Link: http://jeremycurny.com/2016/03/27/symfony3-rest-api/

Marko Pavlovic:
Restful Commander
Mar 23, 2016 @ 11:55:10

In this post to his site Marko Pavlovic talks about REST APIs, some of the non-CRUD (create, read, update, delete) operations that need to be performed and how the Command design pattern can help.

We’ve all been here: the data model has been designed, and we have the entities and relationships implemented. It is finally time to code up the business logic and we are faced with solving these problems: The actions we want to implement on a particular model are not part of the good old CRUD (Create, Read, Update, Delete) operations, and we need to make a lot of non-standard controller actions, and routes. [Also] the actions we want to implement do not belong to a any model, and we have to create a new controller just for the "API".

He gives a more concrete example to illustrate the problem using "sites", "visits" and a desire to add analytics functionality using each's data. He points out two common actions taken when something like this comes up: either new methods on the pre-existing controllers or possibly making a new controller to handle it. Instead he suggests using the Command pattern to handle requests themselves as self-contained items. This gathers all the logic needed in to one place and the Report instance can then be used as-is by the response that needs to render the results.

tagged: rest api command designpattern report site visit

Link: http://markonis.github.io/rest/api/design-patterns/2016/02/23/restful-commander.html

Gonzalo Ayuso:
Foreign Data Wrappers with PostgreSQL and PHP
Feb 22, 2016 @ 09:43:48

Gonzalo Ayuso has posted a quick tutorial to his site showing you how to work with Foreign Data Wrappers on your PostgreSQL database in PHP. If you're not familiar with the data wrappers functionality, you can find out more on the PostgreSQL wiki.

PostgreSQL is more than a relational database. It has many cool features. Today we’re going to play with Foreign Data Wrappers (FDW). The idea is crate a virtual table from an external datasource and use it like we use a traditional table.

He gives an example of a simple RESTful service with a Silex application serving up a set of user data (names). He then switches over to the PostgreSQL side and shows how to create the data wrapper and set up the mapping of it to the REST server's location. With that set up you can then select from the data returned as if it were a normal table with a slight caveat - filtering (like with where) must be done server side, not via the SQL statement.

tagged: postgresql tutorial foreigndatawrapper datawrapper database rest service

Link: http://gonzalo123.com/2016/02/22/foreign-data-wrappers-with-postgresql-and-php/