CakePHP Routing File Extension

CakePHP (I’m using 3.x), via the Bake console, provides developers a fast and easy way to stand up an application.  With a small amount of information about your database and executing a few Bake commands, CakePHP can scaffold various MVC classes for you to use in building your application. This post doesn’t detail how to build a CakePHP application — the documentation does an excellent job of that — but rather illustrates how, with a few lines of code, you can enable your routes to return JSON or XML by simply appending the desired extension to your URL.

The CakePHP documentation has two nice tutorials to get you started quickly and showcases some of Cake’s essential tools like the Bake console. The Blog Tutorial will walk you through installing CakePHP, creating a database, and creating enough classes and artifacts to create a small CRUD system. The database instructions create an “articles” table: I’ll refer to that table here to demonstrate enabling the routing file extension capability and showcase how you can return responses to clients in JSON or XML by simply adding the appropriate URL extension (e.g. http://localhost:8765/articles.json).

After the Blog application is Baked load the ‘RequestHandler’ component in your src/ArticlesController.php class or make it available to all your controllers by adding the code to your src/AppController.php:

route-appcontroller

The final step is to enable named extensions in your config/routes.php file:

route-routes

And your done! If you access your articles route without an extension you’ll get your view content returned:

route-articles-1

However when the .json or .xml file extension is added to the URL corresponding to the Articles route (e.g. articles.json) CakePHP will serve the response in the requested format:

route-articles-2

Have a look at the following resources for more information about CakePHP, it’s routing features and REST capabilities:

NetBeans Terminal

I have a confession — I use NetBeans.  I’ve been a user (fan) since the late 1990’s when it was being actively developed in the Czech Republic, before being purchased by Sun Microsystems.  I’ve always enjoyed it’s features and ease of use.  It’s grown very nicely over the years and I don’t sense that it’s fully appreciated for the features and capabilities it possesses.

PHP support was added to NetBeans several years ago and is my preferred IDE when working on PHP projects.  Occasionally I’ll use Vim (my preferred editor) to work on my projects, but by and large I use NetBeans because of all the power a good IDE provides.

The reason for my post today is that I wanted to mention one of those features that many may be unaware of within NetBeans.  A colleague of mine is an Emacs fan and really appreciates the shell capabilities it offers, but did you know that NetBeans has a similar feature baked in?  I was working on a PHP project today and when I fired it up I thought a small post was warranted.

I make regular use of CakePHP’s bake console and the mysql cli, so while using the editing power of NetBeans the Terminal feature is a convenient and handy way of working without leaving the IDE. The Terminal has both local and remote flavors to suit your taste.

netbeans-terminal-2

netbeans-terminal-3

If you haven’t tried NetBeans or its Terminal tool I invite you to give it a try. The ability to have multiple tabs to perform separate and distinct shell work is a bonus. I hope you find it as convenient and useful as I do.

A few resources for your convenience:

 

Contributing to CakePHP Documentation

I started a quest in 2015 to learn modern PHP development. After a long hiatus from PHP I thought I’d give it another look.  I did some web development with PHP around 2005 but became involved in other things and got away from it. Earlier this year I decided to take a look and see where things were these days and was very surprised to see how far it has come.  Many of the features I’d used and read about in other languages were now available in the PHP ecosystem.  I decided to start spending as much time as possible getting acquainted with some of the frameworks and of course Composer.

I started working with Laravel and really like it. The features remind me of the Java + Spring + Hibernate platform that I’ve used. It felt familiar.  I also started reading up on Propel, Symfony2 and CakePHP.  For no particular reason I’ve been spending some time lately working my way through the CakePHP documentation and tutorials.  I spotted a few typos and began contributing to the documentation.  It’s nothing really — just clarifying a few points here and there, or fixing the occasional typo — but I’ve been having fun doing it.  I’ve dabbled with contributing to other Open Source projects but I’ve never quite found a home yet.  I like the CakePHP framework a lot.  We’re still getting acquainted but the community feels inviting and I’d like to be able to contribute to the core system someday if possible.  It’ll take some time I’m sure.  In the mean time I’m going to continue my quest to learn all I can about developing web applications using Modern PHP and CakePHP in particular.  Like I said, I really like Laravel but I’m going to see where the CakePHP thing goes.