Web Development


2
Feb 12

CakePHP 2 – Hashing passwords before saving.

A quick gotcha here that leverages CakePHP’s inbuilt utilities to hash data before you commit it to the database.

The beauty of this method is that you can still run all your validation checks and then hash the data between validation and saving. Why is that important? Consider you have a rule that says a password should be no more than 15 characters. When you hash it, it would break the rule and your record would never save.

All you have to do is hash your fields in the beforeSave function of your model like this example from a User Model:


public function beforeSave() {
App::uses('Utitlity','Security');
if(!empty($this->data['User']['password'])) {
$this->data['User']['password'] = Security::hash($this->data['User']['password']);
}
return true;
}

One import aspect of this is to always return true. If you don’t your record will NEVER save.


9
Jul 11

CakePHP Development

The new website for my CakePHP development company, Zumo Internet is now live.

I’ve been building with PHP for 10 years, but over the past 2 years all of my projects have been built using CakePHP. I’ve never looked back as far as stability and speed of development is concerned.

So if you are looking for a CakePHP developer, please visit and maybe hire me.


17
Jun 11

Using CakePHP without a database

I enjoy CakePHP’s structure and methods so much that I have started  using it for my non-database driven projects and for prototyping big projects.

However, when no database is present CakePHP throws a warning that it can’t connect to the database. The solution is to create a dummy database connection to convince CakePHP that all is ok. And, it’s as simple as this:

Create a new folder app/models/datasources/dbo

Create a new file at app/models/datasources/dbo/dbo_dummy_db.php and add the following:

1
2
3
4
5
6
7
<?php   
    class DboDummyDb extends DboSource {         
                 function connect()  {                
                          $this->connected = true;
                          return $this->connected;
                  }
            }

In your app/config/database.php find the default connection array – a line that starts $default = array
and replace the line for the driver from
'driver' => 'mysql',
to
'driver' => 'dummy_db',

And that’s all there is to using CakePHP without a database.


13
Jun 11

Removing tracked files from git without deleting them

— DISCLAIMER: someone may tell me where I went wrong shortly after publication —

When I develop CakePHP apps, my local database configiration file is very different from the live one. Therefore, when I am using Git to push my changes to a live environment I want to be sure this file is not pushed. Fortunately Git  has the ability to let me do this with the .gitignore file. I can specify the following:

app/config/database.php

One path to file on each line will ignore those files.

However, I recently found myself in the situation where I had already added files I wanted to ignore to my repository. After reading the docs and several forums, the answer seemed to be using the following command:

git rm --cached filename

I did this, commited my changes, then pushed them live. Horror. The database.php had been removed on my local repo and this was subsequently pushed to the live site which duly collapsed. Fortunately I used Git to quickly revert my changes. I’ve no idea why the command above didn’t work for me, but I did find a failsafe way of removing a tracked file without deleting it.

  1. make sure the path to file is listed in your .gitignore
  2. copy the file outside of your repo.
  3. do a git rm -rf path/to/file.ext
  4. do a git commit to remove the file from the repo
  5. copy the file back from where you placed it in step 2

You should be able to change the file now and when running git status it should not show up.

It may seem long winded and a more-than-likely unorthodox to do it this way, but it makes me feel safer.

 


12
Sep 10

SEO friendly URL slugs with CakePHP.

The CakePHP framework API is a treasure trove of often undiscovered methods and functionality. Recently I came across the Inflector Class methods and noticed the “slug” method close to the bottom of the documentation, which is documented thus:

Returns a string with all spaces converted to underscores (by default), accented characters converted to non-accented characters, and non word characters removed.

Continue reading →


20
Jun 10

CakePHP Full Text Search

CakePHP’s database abstraction is just one of the reasons to use this powerful framework for agile web development. However, it is not always obvious how to build complex queries. I recently had to implement a MySQL full text search against a music database that searched on titles, descriptions and tags. Full text search is a quick, and not too dirty way to make a pretty decent search, although it does come with a caveat, as described in the MySQL documentation:

Full-text indexes can be used only with MyISAM tables, and can be created only for CHARVARCHAR, or TEXTcolumns.

Continue reading →


15
Jan 10

Select and Unselect All Checkboxes with jQuery

I’ve been working with jQuery to spice up my interfaces for over 12 months now and figured it’s time to share some of the little techniques I’ve developed on the way.

The first is something I use quite alot when I am giving users the ability to administer lists of items in bulk. That including a checkbox that when checked sets the state of all the checkboxes beside a list of items to “checked”. Naturally unchecking it will uncheck all checkboxes. I’m sure you get the picture here’s the code based on two scenarios depending on your preference. But firstly you need to include the jQuery library in the head of your document (I grab mine straight from Google’s repository to save my bandwidth):

Continue reading →


27
Nov 09

A workaround for the CakePHP alphaNumeric issue

Recently whilst building a CakePHP app on a shiny new server I noticed some unexpected behaviour when using an alphaNumeric rule to validate a model. Whatever I passed into the model returned false. That is to say that even a valid alphanumeric string threw up an exception. (if you haven’t already dived into CakePHP, you should – it’s a rapid development framework based on MVC.)

The issue seems to be tied to PHP 5.1.2 and the way regular expressions are handled, however it seems the it may appear in other configurations too. Before I get to a workaround, note I haven’t levelled the “bug” word at either PHP nor CakePHP – for all I know it could be a difference of opinion on how something should be handled. What I do know is that I would rather harness CakePHP’s flexibility to create custom model rules than try upgrading my current live server PHP installation.

Here is an example of how you should be able to implement the alphaNumeric rule using validation in CakePHP based on a simpliefied model called “User”:
class User extends AppModel

{
var $name = 'User';
var $helpers = array('Form');
var $validate = array(
'username' => array(
'loginRule-1' => array(
'rule' => 'alphaNumeric',
'last' => true,
'message' => 'Only letters and numbers allowed'
));
}

The rule in bold italics above is the one that doesn’t always work. However this can be easily rectified with a custom rule built on a regular expression seeking out valid alphanumeric strings.

To achieve this the alphaNumeric rule just needs to be replaced with this:

"rule" => array('custom', '/^[a-z0-9]*$/i'),

Problem solved.


8
Apr 09

New Launch Sports Recruitment International

The latest project of my fledgling web design company in Spain, Zumo Internet has left the shelves. Sports Recruitment International is a re-tool of an already successful web site for a major recruiter of professionals in the sports and leisure industry.

We built the site in co-operation with T-MAX media from Oxford. Our input was cutting the new designs into valid XHTML and CSS, building some custom jQuery functionality and programming new features into their content management system. The result is a high-impact site for a major corporate client – and all just as Zumo Internet turns 1.


26
Feb 09

New Launch Commercial Roofing and Single Ply Membrane products.

The most recent project from my web design company in Spain, Zumo Internet has been launched. IPS UK Ltd supply commercial roofing products. They supply and arrange contracting for some of the largest roofing projects in the UK as can be seen from their case studies of installed roofing systems.

Continue reading →