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

CloudWays Blog:
Create Live Search In Laravel Using AJAX
Dec 07, 2017 @ 12:55:08

The Cloudways blog has posted a new tutorial for the Laravel users out there showing you how to create a "live search" box for the contents of your site. They use an e-commerce example and make use of some simple Javascript to return results from a backend script.

Whether you have a blog or an ecommerce store, a search bar is always an essential component of the UI. However, the days of simple search bar is over. These days, a live search bar is much more efficient than a simple search bar because it displays similar content in real time. This increases the chance of landing a sale because the customer could see the largest selection of related products.

The article starts off with the prerequisites for following along including an installation of PHP, MySQL, Apache and Laravel. They include an optional step of setting it all up on a Cloudways server but this isn't required if you want to do it locally. The tutorial then walks through the setup steps and creating the migration for the "products" table it will search. It also shows the creation of the controller and routes for the search endpoints and the view with the Javascript to make the Ajax request. It finishes with a way to test the result and a screenshot of what should be returned.

tagged: live search laravel tutorial ajax request ecommerce

Link: https://www.cloudways.com/blog/live-search-laravel-ajax/

Laravel News:
Using Named Routes in a Lumen Test
Nov 21, 2017 @ 12:56:49

On the Laravel News site there's a quick tutorial posted showing you how to use named routes in Lumen in writing tests for your application.

When writing tests in Lumen, I recently discovered that the route() helper doesn’t work with tests out-of-the-box.

I prefer to define named routes and make requests against them in my tests. If you follow the Lumen documentation, the typical way that you make a request for a test [of the return of a JSON endpoint results in an error message]. [...] If you inspect things a little closer, you can see the issue: [...] Interestingly, the route doesn’t look quite right, and the router is returning the / route. It looks like the localhost part of the request isn’t being set, and the route isn’t matching. We can fix that by bootstrapping the request as Laravel does.

The post then walks you through the manual process of bootstrapping things so that routes are correctly resolved. This includes changes to the code for the base test case to handle the "boot" and set the path value for the request correctly.

tagged: named lumen route test request boot base testing tutorial

Link: https://laravel-news.com/using-named-routes-lumen-test

Sergey Zhuk:
Building ReactPHP Memached Client: Making Requests And Handling Responses
Oct 26, 2017 @ 11:37:03

Sergey Zhuk has kicked off a series of posts to his site showing how to create a ReactPHP memcache client that can work as a streaming client for your PHP application rather than single get/set requests.

Before writing any code we should think about our future client’s API: how we are going to use it [and] what methods it is going to have.

The client is going to be used in ReactPHP asynchronous ecosystem, so I’m going to provide a promise-based interface for it (when methods return promises). Also, we are building a streaming client. Under the hood, we will open a socket connection and use it as a stream. The client itself will be a wrapper on this binary stream communication. That means that it is our job to manually parse Memcached protocol to write and read data with sockets. So, having all of this in mind, let’s start.

He then starts in on the development of the base for the client including the factor class that will create the client (connector) with the provided Loop instance. He includes an example of this in use to create the client and point it to a local memcache server. Next he creates the client class that will use the stream to send requests and a parser to work with the responses and resolve actions that need to be taken based on their contents.

tagged: reactphp tutorial memcache client stream loop request response

Link: http://seregazhuk.github.io/2017/10/09/memcached-reactphp-p1/

Jen Segers:
Goodbye controllers, hello request handlers
Sep 26, 2017 @ 11:12:21

In a post to his site Jen Segers says "goodby to controllers" in favor of request handlers. Request handlers are a concept similar to the ideas in the ADR pattern but are defined a bit differently.

If you have worked on large applications before, you might have noticed that you end up with bloated controllers sooner or later. Even if you use repositories or service classes to extract logic from the controller, the amount of dependencies, methods and lines of code will grow over time.

Let me introduce you to request handlers. The concept is very simple, yet very unknown to a lot of PHP developers. A request handler is basically a controller, but limited to one single action.

He suggests using invokable classes to build out request handlers in your PHP code, making use if the magic __invoke method to make them callable. He gives a "hello world" kind of example and talks about how Laravel and Slim already implement this idea in their routing. He then looks at how these responders help you adhere to the Single Responsibility Principle (part of SOLID) and how they make the code easier to test and simpler to refactor.

tagged: controller request handler invokable class example tutorial

Link: https://jenssegers.com/85/goodbye-controllers-hello-request-handlers

Alex Bilbie:
Looking for a new maintainer of league/oauth2-server
Aug 23, 2017 @ 09:30:16

In a new post to his site Alex Bilbie has started the search for a new maintainer for the league/oauth2-server package citing the lack of time on his part to give the project the time it needs to flourish.

A little over five years ago I pushed the league/oauth2-server. The project has been downloaded over 2.5 million times, has more than 3000 Github stars and has been contributed to by 77 awesome people across some 2000 commits.

Last year when I became self-employed I had intentions that I’d have more time to dedicate towards open source projects but reality worked out slightly differently and I’ve had one of the busiest (and best) years of my life.

He also points out that, besides the lack of time to work on the project, he also doesn't actively use the project himself and wants to find a maintainer that "is/are really impassioned by the project, want to advance the project (for example implement OpenID support) and are willing to devote time to answering support requests and review pull requests."

If this interests you and you'd like to see the project move forward, the post includes his contact information for you to reach out.

tagged: oauth2server phpleague package maintainer request community project

Link: https://alexbilbie.com/2017/08/looking-for-a-new-maintainer-of-league-oauth2-server/

Dotkernel.com:
What is PSR-7 and How to Use It
May 22, 2017 @ 10:18:50

On of the standards that have come out of the PHP-FIG (PHP Framework Interoperability Group) in the past few years has been PSR-7, a standards definition for working with HTTP requests and responses as PHP objects. While those that have worked with most of the PHP frameworks out there may be familiar with the concept, it can be confusing if you're just getting started with the idea. In this post on the Dotkernel site they introduce PSR-7, talking about its goals and what it defines to help bring everyone on the same page for HTTP requests.

PSR-7 is a set of common interfaces defined by PHP Framework Interop Group. These interfaces are representing HTTP messages, and URIs for use when communicating trough HTTP.

Any web application using this set of interfaces is a PSR-7 application.

They start off by defining (and linking to) the different interfaces involved in the PSR-7 specification (the spec doesn't define functionality, only the structure). From there the tutorial uses the Zend Diactoros component to illustrate an implementation of the PSR-7 structure. They cover two of the main tasks when working with HTTP requests/responses: working with the headers and fetching/writing to the body.

tagged: psr7 phpfig standard http request response introduction

Link: https://www.dotkernel.com/dotkernel3/what-is-psr-7-and-how-to-use-it/

Michael Dyrynda:
Partial model updates in Laravel
Apr 06, 2017 @ 09:44:05

Michael Dyrynda has written up a post showing the Laravel users out there how to perform partial model updates making use of the "intersect" method.

Many Laravel developers would be familiar with the helpful only method found on the request object, which allows you to specify keys to pluck from the request. Not only does this simplify your workflow, it works quite nicely when completely unguarding your models by setting protected $guarded = [];

[...] For newcomers to Laravel, you might find this suggestion dangerous, but using only means you will only pass the desired input to your model irrespective of what was passed via the request itself. [...] Adam Wathan tweeted about an approach he uncovered whilst helping somebody out when approaching partial model updates.

He shows how the method works by starting with a traditional update method call that reassigns model properties based on input (using "has" checks to ensure the property exists). He then refactors it to use the intersect method and replaces about twenty lines of code with one. He talks about the differences between using only and intersect and offers a caveat to using intersect around preserving null values on properties.

tagged: partial model update intersect only request tutorial refactor

Link: https://dyrynda.com.au/blog/partial-model-updates-in-laravel

Delicious Brains Blog:
PHP and cURL: How WordPress makes HTTP requests
Mar 30, 2017 @ 10:49:35

In a new post from the Delicious Brains site Peter Tasker looks at how WordPress makes HTTP requests with the help of the cURL functionality in PHP.

cURL is the workhorse of the modern internet. As its tagline says, cURL is a utility piece of software used to ‘transfer data with urls‘. According to the cURL website, the library is used by billions of people daily in everything from cars and television sets, to mobile phones. It’s the networking backbone of thousands of applications and services. Unsurprisingly, it’s also a core utility used by WordPress’ own Requests API as well as our own WP Migrate DB Pro.

If you’re curious about the power of the cURL library, how it works with WordPress and what to watch out for (especially on macOS), then you’re in the right place.

He starts by giving a bit of background on what cURL is and some examples of how its used to make requests. He then talks about the cURL integration with PHP via an extension and provides a simple code example fetching an endpoint from the httpbin.org site. With that background defined he moves into the main focus of the article - how cURL and PHP combine in the WordPress WP_Http class and Requests handling to make HTTP requests to remote (or local) resources. Code examples are included showing how to put these pieces to work in a custom script and includes some common issues you might see during your HTTP request development.

tagged: wordpress http request curl tutorial wphttp internal example

Link: https://deliciousbrains.com/php-curl-how-wordpress-makes-http-requests/

Zend Framework Blog:
Handling OPTIONS and HEAD Requests with Expressive
Mar 29, 2017 @ 10:39:46

The Zend Framework blog has continued its series of posts focusing on the use of the Zend Expressive framework with a new tutorial covering handling OPTIONS and HEAD requests in an Expressive-based API.

In v1 releases of Expressive, if you did not define routes that included the OPTIONS or HEAD HTTP request methods, routing would result in 404 Not Found statuses, even if a specified route matched the given URI. RFC 7231, however, states that both of these request methods SHOULD work for a given resource URI, so long as it exists on the server. This left users in a bit of a bind.

[...] In the case of a HEAD request, the specification indicates that the resulting response should be identical to that of a GET request to the same URI, only with no body content. This would mean having the same response headers. In the case of an OPTIONS request, typically you would respond with a 200 OK response status, and at least an Allow header indicating what HTTP request methods the resource allows. Sounds like these could be automated, doesn't it? In Expressive 2, we did!

The tutorial then shows you the code you'll need to add to your Expressive v2 application for handling each kind of request. It involves some custom middleware using the route handling on the HEAD request type for one and the other for OPTIONS. The HEAD requests return an empty response while the OPTIONS requests return the data from a manually defined array (no automatic generation from routes or anything).

tagged: zendframework zendexpressive options head request handling

Link: https://framework.zend.com/blog/2017-03-28-expressive-options-head.html

Alejandro Celaya:
Managing PUT requests with file uploads in psr-7 and middleware PHP applications
Mar 07, 2017 @ 13:17:01

Alejandro Celaya has posted a new tutorial to his site covering the handling of PUT requests in PSR-7 applications for file uploads via middleware.

It has been a long time since I first realized that handling file uploads in non-POST requests (like PUT) wasn't an easy task. One could assume the $_FILES array should be populated regardless the HTTP verb, but actually, PHP doesn't do it on its own.

After a long time wanting to find a solution to this problem, I've finally dedicated the time to get something functional, that allows file uploads to be transparently handled regardless the HTTP verb (it works the same way in POST, PUT and PATCH requests).

Since nowadays I try to work with psr-7/middleware based applications, I have created a Zend Expressive app that registers a middleware capable of parsing a multipart/form-data request body, populating the request's uploaded files array and parsed body array. This way, you can call $request->getUploadedFiles() or $request->getParsedBody() in any PUT or PATCH action, the same way you would do in a POST action.

His example application shows a simple HTML form that, when submitted, changes the HTTP request type based on a radio option selected at the bottom. He walks through the steps that the application takes to handle the upload via this middleware that makes it possible to work with the body of the PUT the same way as other requests. He goes through each part of the code that's required to make the middleware flow work and finishes up the post looking at a few other things to consider (like opting for POST over PUT for file uploads).

tagged: zendexpressive application tutorial psr7 middleware put request fileupload upload

Link: https://blog.alejandrocelaya.com/2017/03/06/managing-put-requests-with-file-uploads-in-psr-7-and-middleware-php-applications/