Coding Guidelines

This section covers what you need to know for coding.

Controllers

All function args should have defaults, and the controller logic should handle the requirements. If an arg does not have a default, and is not provided in the URL, then a PHP warning will result. In the production environment, PHP errors and warnings are off, so there is a possibility that the user will get a white screen.

Cookies

In order to avoid collisions, use a cookie prefix. The problem is that for subdomains, you will always get served the cookie from the base domain, if it exists.

** DO NOT set using ci()->input->set_cookie() nor retrieve using $_COOKIE[] **

  • Make sure to set cookie_prefix in config.php
  • Use the cookie helper
  • Use set_cookie() and provide the name without the prefix
  • Use get_cookie() and provide the name WITH the prefix. Suggest doing it like this:

get_cookie(config_item(‘cookie_prefix’).’identity’);

Config files

QVCI contains a set of common config files which are kept up to date with CodeIgniter. Applications should have their own config folder and include the qvci configs, then define app-specific overrides.

How to determine which environment you’re running on

The code should check the value of ENVIRONMENT, which is defined in index.php

Possible values are: development or production

Mixing PHP code with templates

Here’s an interesting scenario. In the following block of code, it is tempting to save the creation of $hdr_display and put a condition inside the style attribute, but here’s a good reason why not: If the function were to cause an error, for example if the database table did not exist (it actually happened), then the error string would end up being part of the style attribute. The code below avoids that situation:

<?php
$hdr_display = 'block';
ci()->load->model('user_pref_model');
if (ci()->user_pref_model->item('hide_banner')) {
        $hdr_display = 'none';
}
?>
<div class="qv-hdr" style="display: <?php echo $hdr_display ?>;">

Odd Cases

There are some situations which can be considered odd.

  • In PHP files, it may happen that the phpdoc block does not automatically fill in before a function. One reason this may happen is because there may be a */ inside that code that gets interpreted as end of block comment. Example:

    $utc = preg_replace('/\\[(.*)\\].*/', '$1', $rec);
    

To fix that, use an alternate delimiter:

$utc = preg_replace(':\\[(.*)\\].*:', '$1', $rec);