CodeIgniter PHP framework notes CodeIgniter (CI) PHP framework notes

CodeIgniter is designed for relative (rather than absolute) paths.

Configuration

local XAMPP development configuration

local (Ubuntu) Linux Apache development configuration

To simulate a publicly-visible (e.g. /public_html/ ) production-server directory within the typical Apache local-development /var/www/ directory structure, note that the local /www/ directory corresponds to /public_html/ on the remote, production server. Thus, you can create and setup to develop in a local /var/public_html/ directory, a structure similar to the production-server's /public_html/ directory, rather than in the default /var/www/ directory.

To emulate that production directory structure in my local (Ubuntu) Linux Apache development environment, I found it easier re-specify the DocumentRoot directory (e.g. to /var/public_html/ ), instead of /var/www/ ... than to change all other paths in my local development environment. Then, when it is time to upload/FTP the local development to the remote production server, you don't want/have to have to remember to re-change all those path references - each time you upload files.

Specify the (Ubuntu) Linux Apache DocumentRoot directory in /etc/apache2/sites-available/default . Around line #4, change DocumentRoot "/var/www" to DocumentRoot "/var/public_html" ... and re-start your local Apache server, of course. For the result to take effect in your browser, you may have to clear your browser's cache.

CodeIgniter configuration

web host private directory configuration

If (for security) you put the /CodeIgniter/ directory tree in a directory outside your (publicly visible) web document root (e.g. parallel to your /htdocs/ or /public_html/ or /www/ ), the following configuration edits are necessary.

Controller & View subDirectories

If you use directories beneath the /application/controllers/ or /application/views/ directories, you must:

Index.php

Now that you are "configured", the whole sordid affair starts with the Index.php in your designated DocumentRoot directory ... in this case \xampp\htdocs\public_html\Index.php .

Class Constructors

PHP4 class constructor syntax uses the class name as the constructor function name.

    class Blog extends Controller
    {
        function Blog()
        {
            parent::Controller();
        }
    }
PHP5 class constructor syntax uses __construct() as the constructor function name.
    class Blog extends Controller
    {
        function __construct()
        {
            parent::Controller();
        }
    }
The above applies to Model, as well as Controller, classes.

Controllers

Controllers are called from the /CodeIgniter/system/application/controllers/ directory. Calling basename(dirname(__FILE__)) will return /controllers/ as the directory!

Front Controller

CodeIgniter implements its “front controller” in the Index.php file in the top-level /CodeIgniter/ directory i.e. above the /system/ directory.

To get index.php to disappear from the URL, see http://codeigniter.com/wiki/mod_rewrite/.  If using virtual host subdirectory (e.g. /CodeIgniter/), in .htaccess, set RewriteBase /CodeIgniter/, rather than RewriteBase /. Even though this makes the Index.php front controller disappear from the URL, Index.php must still be present in the root directory i.e. above /system/.

Load->vars

Use in the controller constructor to specify variables for any/all views … from any function.

Load->view

Syntax example:
$this->load->view(‘viewFileName’,$dataSource,true/false)

Load->view 2nd parameter

The load->view second parameter specifies/provides the view data, which may be an array or an object (docs: General Topics/Views). Use paths relative to the CodeIgniter/system/application/views/ directory.

Load->view 3rd parameter

(see … docs: Class Reference/Loader Class).


Value

Meaning

Default

False

Send to browser

Yes

True

Return data

 

Paths – relative

Per (docs: Helper Reference/“File Helper”), “Code Igniter uses a front controller (the Index.php file); paths are relative to the main site directory (i.e. where the Index.php file resides).

Using an APP_PATH constant is many times faster than repeated base_url() function calls per: http://codeigniter.com/forums/viewthread/52448/

Set DreamWeaver “Test Server” to /htdocs/CodeIgniternot to /htdocs/CodeIgniter/system/ … or /htdocs/CodeIgniter/system/application/ etc.

Models

syntax: class name is: Model_name
	class Model_name extends Model{}

Operating system file name is: model_name.php (i.e. all lower-case file names). Otherwise, after FTPing from a (non-case-sensitive) Windows development PC to a (case-sensitive) Linux web host, you'll get the error: Unable to locate the model you have specified:

URI segments

Segments are explained in the Class Reference -> URI Class … User Guide section.

SQLite

sqlite_open() only supports SQLite2; SQLite3 must be accessed via PDO.

Styling

The xHTML <base> tag can be used to establish a base location in HTML files from which to locate image and CSS files within HTML files.
http://codeigniter.com/forums/viewthread/51209/

Strange characters

If DreamWeaver and Internet Explorer render the page correctly, but you experience strange characters in CodeIgniter, especially, AE (Euro sign), change the character encoding from UTF-8 to Western European.

Views

CI expects to find view.htm files in the /views/ directory.   If not, you must provide relative paths from the /views/ directory.   Absolute paths will not work – either absolute file system locations or relative paths from the web root.

Views within views

See: http://codeigniter.com/forums/viewthread/44916/
Also: http://codeigniter.com/user_guide/libraries/loader.html

Options – for providing data to a view

	$this->load->view(‘viewFileName’,$viewDataSource)
	$this->load-vars($array)

The vars() option allows setting global variables in the controller constructor.  The $array variables, thus set, can be used in multiple calls from any function to any view.

Troubleshooting

When you see the following error, it usually really means that it can not find the file i.e. the path is wrong.

	Unable to load the requested file: http://whatever.htm

Back Home