Author Archives: Steve
CodeIgniter – One Year On
It’s been about a year since I selected CodeIgniter as my framework of choice, and 3 or 4 projects later I can say with sincerity that I have never looked back.
With the benefit of hindsight I want to go over some of the reasons why moving to a framework (or perhaps a better framework) is a great choice for any programmer once they have a grasp on the fundamentals of PHP.
Proper MVC gives method to mayhem
I used to think I got MVC architecture, but it wasn’t until I delved into CI that I started to really understand how its supposed to look and feel, and why the correct structure is so beneficial to an application.
The way that CI is predictably structured means that debugging is almost fun. There is such a distinct pattern of organisation that finding a gremlin is as easy as looking at the URL and going directly to the responsible controller and method. This carries across projects and across developers.
Gone are the days of following breadcrumbs off to some other obscure part of the application (they don’t really exist anymore) – at worst you would find yourself in a library or helper file which is easily tracked down to a predictable location from the controller level (and generally speaking very well documented).
[On the topic of debugging I will give a curteous nod to CI's error handling capabilities when in development mode as well.]
Reasonable code organisation is imposed on you to an extent that you probably are in the wrong industry if you manage to make a mess of things.
Not to mention models. Sure, I still have some confusion about how to deal with multiple table joins in an elegant way given the supposed “one model per table” rule, but at least … Read more
How to Run Cron Jobs in CodeIgniter
After a fair bit of trawling Google, I got the impression that setting up a cron job in codeigniter was going to be a bit tricky.
The two potential solutions I discovered were:
- execute the “wget” command to replicate a normal browser request, which then exposes my cron functionality to the big wide world and creates some additional unnecessary overheads, or
- Create a bespoke front controller a bit like the existing index.php but specifically for cron jobs
Neither of these options sound particularly ideal to me, and thankfully, there is a better way (and it turned out to be a case of RTFM).
Firstly, just create a normal controller. I creatively called mine Cron.
Note the call to $this->input->is_cli_request() which ensures that this controller cannot be accessed directly from a url.
Next we setup our cron job as you would any other, except that you provide the path in a special format.
php /path/to/index.php controller_name method_name ["params"]
The first part is either the command “php” or your server path to php.
The second part is the absolute path to your CI front controller, eg. /path/to/index.php.
The third part is the controller name, followed by the method name, followed by optional parameters.
The CI manual gives the example:
php /path/to/index.php cron foo
If that doesn’t work you might need to specify your path to php. I set mine as follows:
/usr/local/bin/php -f /home/clinic/public_html/index.php cron foo
You can even pass parameters in quotes, which have the same effect as parameters in regular url requests!
/usr/local/bin/php -f /home/clinic/public_html/index.php cron foo “beer”
Automatically Submit Google CSE
This tutorial outlines two techniques to customise Google’s Custom Search Engine:
- Generating your own custom styled search form
- Fetching the search results on page load rather than the default which requires the Google provided search form to be submitted
This enables you to place your custom styled search form anywhere on your site (for example in a global site header), and when the form is submitted the search results are automatically loaded as soon as the user lands on the search page – as opposed to the default behavior which requires submission of the Google provided search box.
This is a sample global form that I use in my header file, which submits to the url “/search”. I will leave the CSS side to your imagination.
On my search page, the following code displays the Google CSE search box and pre-loads the results which are passed in from PHP’s $_GET. Don’t forget to replace myUniqueID with your Google CSE unique ID.… Read more
…