Kris Jordan recently posted five tips to help you get a "more correct" REST interface in your application.
As we entered a programmable web of applications with APIs the decision to ignore HTTP gave us problems we're still dealing with today. We have an internet full of applications with different interfaces (GET /user/1/delete vs. POST /user/delete {id=1}). With REST we can say /user/1 is a resource and use the HTTP DELETE verb to delete it.
In a recent post to his blog Mike Bernat gives a quick tip on how to change the default value of a data-time input field in a CakePHP application.
Automagically generated date/time input fields normally default to the current date and time. For a couple of reasons, I had to change this to another default value.
His included code shows how to modify the default behavior of the form input field for the date with an array of parameters including the hour, minute and meridian (am/pm) values for the element.
The PHPFreaks site has a new tutorial posted today showing how to create a method for custom list ordering in PHP.
So you have some tabular data printed out in your browser. You can even change the order of the information by clicking on the column name at the top of your list. But can you make your own custom list order? Or maybe you're deciding to make a content management system (CMS). You're making your features modular, so it's easier to add/remove modules. How do you go about displaying them in a custom order in the browser?
They create some MySQL tables to store the data in and set off into the code. The sorting allows for the data to be ordered by any column in either ascending or descending order based on the users' clicks. The results are output in a standard HTML table.
After looking around for different hosting for some of his projects (one that was a bit more PHP-developer friendly), Brandon Savage looked into Slicehost, a virtual server option that gives a bit more control than the larger, more typical shared hosting environments. Here's his review of the move.
I'm not sure what I was expecting, but what I got was pretty amazing. Slicehost works by offering virtual servers for each account - that is, for each account they offer, you get your own box. [...] You're responsible for putting anything on it you want. And that's where the fun starts: you really can put anything you want on it.
The slices allow you to customize your PHP builds, add in MySQL, change up Apache - whatever you want. All you need to know is how to compile things and get them working together. Slicehost isn't a good option for someone that's not familiar with linux system administration, but if you know exactly what you want and how to put it there, their pricing is one of the best out there (plus they just got bought up by Rackspace so their future seems bright).
The AskAboutPHP.com blog has posted a helpful new tutorial for those using CodeIgniter out there - how to extend the native Model class to bend it to your will.
I'm in the process of creating models for my CI project, and realized that certain functions within the models were getting repetitive. Using CI's ability to create my own custom libraries, I was able to create my own custom 'Model' which extends from the core 'Model' object. How this simple architecture has cleaned up my code is simply remarkable.
Rather than overwriting the main Model.php file with some of your own changes, he suggests creating a new library, a "parent model" that can be extended instead to provide some common functions that all of your application's models might need. For something a bit more complex, he also points to this library that extends the models to give it CakePHP-like functionality.
I came across an instance using Zend_Form recently where the level of flexibility offered was a bit of a double-edged sword. In order to provide maximum flexibility per form element instance, each has not only their own filter, validator, and decorator instances, but also a plugin loader instance for each of these three types of plugins. These add up quickly when you have a form with several hundred elements in it.
To prevent bloated code and redundant plugin definitions, he subclasses the Zend_Form component into a Custom_Form class that defines central plugin loaders for all of the elements so that when an element is created and a plugin is used, it will always pull from the centralized location rather than a (possibly different) custom one defined on the element.
The NETTUTS.com site has a new tutorial posted today showing how to combine jQuery and PHP to add auto complete functionality to a simple search engine.
This tutorial will show you how to use the "Popular Queries" feed from your Google Custom Search Engine (CSE) as a data source for a jQuery autocomplete.
They walk through the creation of the search page and provide the code for the jQuery half that calls the autocomplete() method on the search field. The PHP script that's called loads the XML file from Google's server with the latest from the "Popular Queries" page. This is then parsed and sent back out as Javascript back to the waiting jQuery. They also include a brief look at caching the results by writing them out to a file that's checked and updated based on the results of a filectime() call.
In this new postJani Hartikainen shows a quick and easy method for creating a custom form element in your Zend Framework application. His example is a custom time element.
The alternatives would be creating custom view helpers to output the custom form elements, and using the viewscript decorator. Creating a custom view helper would also require a custom form element class, and it would be a bit tricky. [...] I think the viewscript approach is the most flexible and simplest to implement, so I chose to go with that.
His example defines a time field made up of three drop down lists, one each for hours, minutes and seconds. Included is the code to make the element (including a regular expression for validation) and the view script to display it.
DevShed continues their series looking at exceptions in PHP with this second part focusing on throwing and handling customized exceptions in your PHP5 application.
While the built-in exception system that comes bundled natively with PHP 5 is usually good enough to handle errors and other critical conditions during the execution of a given web application, it's worthwhile to mention that there are a number of additional cases where it is necessary to implement an exceptions system that can handle different failures in a more specific way.
They show how to trigger some basic custom exceptions and detail the creation of a class to handle catching these exceptions in a bit more OOP kind of way.
Michelangelo van Dam has written up a quick introductory post on his blog about the process around throwing and catching exceptions in your apps.
One thing that I noticed was that although the code was well written [in the Zend Framework], implementing coding standards and best practices on many of the classes, I did notice a wrong usage of throwing exceptions (the try - catch statements).
He gives a few examples - catching a "divide by zero" the right and wrong way, how to grab/handle the message that comes along with the exception and how to define your own custom exception handler to help your code do more useful things with the errors it might throw.