Alejandro Celaya | Blog

Software development, agile methodologies and open source projects.

  • Managing PUT requests with file uploads in psr-7 and middleware PHP applications

    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.

  • Run PHPUnit tests inside a docker container from PhpStorm

    Docker is, without any doubt, the trending tool these days. Everybody wants to use it, because it is very useful, allowing to easily generate development environments for any kind of application.

    A couple months ago I started working with docker myself (it has taken me a while, I know), and now I can't imagine working without it.

    I started using it at work, but now I'm migrating all of my OSS projects too.

    The first stopper

    Regardless docker is very cool, there is a problem when you start using it.

    I had my development environment perfectly configured, and all my tools properly integrated. I was able, among other things, to run any project tests from within PhpStorm, just by right-clicking any test class, test method or phpunit.xml file, and selecting the "Run" option.

  • Setup a "Let's Encrypt" certificate in a AWS Elastic Load Balancer

    Let's Encrypt is the latest internet trending. Its a relatively young Certification Authority which intends to provide free and automated SSL certificates so that everybody can set encryption to their sites and applications.

    It is a Linux Foundation project sponsored by most of the biggest Internet companies, such as Google, Facebook, Mozilla or Cisco.

    They provide tools to automatically generate, setup and renew SSL certificates, but those tools are still in early stages, and not all the integrations are easy.

    In this article I'm going to explain how to setup one of this certificates in an Amazon Web Services Elastic Load Balancer (ELB from now on).

  • Creating a content-based Error Handler for Zend Expressive

    The other day I was working on a Zend Expressive application I'm currently building. The application includes a REST API among other things, but it also has some endpoints which render HTML.

    In one of my tests of the REST API I saw that when an error occurs (404, 405 or 500), I was getting an HTML response, which is not easy to handle when the client is expecting JSON.

    I started to dig on how to fix this problem and thought that using ErrorMiddleware (which is invoked in case of an error) should be the solution, but after some tests I saw that it is only invoked if a regular middleware invokes the next one by passing an error as the third argument or an uncaught exception is thrown.

  • Project Scalability with Zend Expressive

    This article was first published in Zend Developer Zone

    I've been working with some different frameworks lately. One of them is Zend Expressive, and I've come to the conclusion that I don't need to choose between different frameworks; depending on the project, Expressive always fits my needs and scales from small projects to bigger applications.

    The Microframework approach

    The first thing that one could see on the "Hello world" example is that Expressive seems like a typical microframework: you create an application object and register routes with it.

    Something like this:

    use zend\Expressive\AppFactory;
    
    include __DIR__ . '/../vendor/autoload.php';
    
    $app = AppFactory::create();
    
    $app->get('/greet/{name}', function ($request, $response, callable $out = null) {
        $response->getBody()->write(sprintf('Hello %s!!', $request->getAttribute('name')));
        return $response;
    });
    $app->('/about', function ($request, $response, callable $out = null) {
        ob_start();
        include 'templates/about.html';
        $response->getBody()->write(ob_get_clean());
        return $response;
    });
    
    $app->pipeRoutingMiddleware();
    $app->pipeDispatchMiddleware();
    $app->run();