Implementing cross-origin resource sharing (CORS) using middleware in CakePHP 3.3

I worked on a project recently where I had to allow XMLHttpRequests from a different domain. I initially thought about adding the necessary Access-Control headers at the controller level, but after doing some reading, it turned out it was a better idea to make use of a dispatcher filter. However, dispatcher filters would only apply prior to CakePHP 3.3 since they are now deprecated.

Enter middleware, which I found to be quite familiar since I have made use of a similar concept in Express during my Node.js development. Think of middleware as reusable components which you can use to handle your web requests and modify responses.

The CakePHP middleware classes should be placed in the src/Middleware folder. You can create the folder if it doesn’t already exist in your project.

We’ll create a class in the Middleware folder called CorsMiddleware.php.

The code listing is pretty straightforward. The response is modified with the necessary headers to enable the cross origin requests to be successfully handled by the browser.

You can modify the header values as you see fit, like limiting the Access-Control-Allow-Origin header to specific domains – the wildcard (*) allows requests to be accepted from all domains – or the the request methods to just GET and POST, or the allowed request headers.

To make use of the cors middleware, modify src\Application.php and add the middleware using:
$middleware->add(new CorsMiddleware())

And that’s all there is to it. There are more details about what you can do with middleware in the CakePHP documentation, so be sure to check it out.

Building Visual Studio Code for the Raspberry Pi 3

If you’d rather prefer to use Visual Studio Code for C# development instead of MonoDevelop, you can build Microsoft’s own Visual Studio Code from the source repository. I’ve used Visual Studio Code on Ubuntu and it’s actually a pretty neat tool for developing on Linux. It supports over 30 languages including C#, C++, Python, Java and more, so even if you’re not writing C# code, it’s still a useful tool.

Prerequisites for the build on Linux include Python 2.7, make and libx11-dev which should already be installed on your Pi if you started of with a Raspbian Jessie image. Also, if git has not been installed yet, run sudo apt-get install git. Nodejs and npm also need to be installed, as they will be used by the build script to retrieve some required packages. A recent version of nodejs has to be downloaded since the package version in the repository is not adequate.

Install required dependencies for running Visual Studio Code.

Let’s clone the repository and start the build process.

If you get an error like so:

edit npm-shrinkwrap.json and delete the following lines.

Once the build is completed, you can run:

The script will download a few more required files and perform a few initialisation steps before the IDE launches. The editor performance was very poor when I ran it using X11 forwarding however. Perhaps, it works better if running within a native X11 display.

Visual Studio Code running on the Raspberry Pi 3 using X11 forwarding

Visual Studio Code running on the Raspberry Pi 3 using X11 forwarding