Last Updated: February 25, 2016
·
2.445K
· desertlynx

PHP/Codeigniter: Traps for the inexperienced

Codeignighter is looking a little long in the tooth these days. However, it's a solid, adaptable and tested framework which offers unopinionated and low-level simplicity to doing PHP server-side work.

For the most part using it is pretty simple and the official documentation will serve far better than I can. However, there are a number of points for the beginner which are a trap.

Controller, model and library naming rules:

File name must be the same as the class name

Controller 'Users' must be in the file '/applications/controllers/users.php'

Libraries must always be loaded in lowercase

Library 'rest' must be loaded with
$this->load->library('rest')
and used as
$this->rest->post();
Any attempt to load it up with upper case appears to fail.

Controller and Model or library names can't conflict.

Controller 'users' can't be used with a model named 'users'

Case sensitivity is not entirely predictable:

$this->Db->get()

is not the same as

$this->db->get() 

but $this->db->get() is handled like $this->db->GET().

Arrays in views

Codeigniter flattens arrays when loading views, this can a little confusing at first. However, this turns out to be quite convenient once understood.

For example, you load up the view from a controller like so:

** Within controller **

$currentUser  = array(
    'userid' => 1,
    'username' => 'Bob',
  );  //Some object representing a user. 

$this->load->view('site/userslist, array(
   'user' => $currentuser,
   'time' => time()
 ); 

Codeigniter will then flatten the array and provide the two associative array entries as individual variables. Thus:

** Within view **

<?= print_r($user) ?>                     //contents of $currentUser
<?= date('l jS \of F Y ', $time) ?>   //string representation of time

PHP debugging

PHP is so simple in its implementation that there's not much required in the way of debugging (unlike ahem... java, javascript). My experience has been that not much is required other than the following:

print_r($arrayVariable);

Prints the contents of a variable, array or object. Absolutely essential to figure out what's being passed and looking at the contents. var_dump works similarly, but is almost unreadable in its output.

die();

Stops the code execution at the precise point you want. Useful particularly if you wish to see the code execution and state at a particular point in time (nested loops for example).

debug_backtrace();
Naturally useful for finding out how a method is being called.

Codeigniter database debugging:

While buried deep within the documentation, I find that seeing the last query executed is invaluable for finding joining errors and the like:

echo $this->db->last_query();  //Show's last sql statement
die()

Handling incoming JSON from RESTful services

AngularJS, Backbone and similar client-side RESTful frameworks often interact with the server-side through sending straight JSON because url-encoding is quite limited. The unfortunate effect of this however is to be more or less completely invisible to Codeignighter's $_POST variable or it's corresponding input class.
The solution is to grab input straight from Apache's input rather than letting PHP deal with it. This takes the unintuitive form of:

$inputString = file_get_contents('php://input');

Naturally this will need to be decoded, so my preferred way in which to handle this is:

$array = (array)json_decode(file_get_contents('php://input')); //Returns an array from the JSON string.