CodeIgniter is designed for relative (rather than absolute) paths.
Configuration
local XAMPP development configuration
- If your remote web host server uses a
directory (e.g.
/public_html/) for the publicly-visible files, it, probably, provides one higher-level directory in which to place your non-publicly-visible files. That higher-level directory might appear as just "\"; whatever the name of that higher-level, non-publicly-visible directory, it is equivalent to the\xampp\htdocs\directory.
To emulate that production directory structure in my local XAMPP development environment, I found it easier re-specify the XAMPP DocumentRoot directory (e.g. to/htdocs/public_html/), instead of/htdocs/... 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 XAMPP DocumentRoot directory in\xampp\apache\conf\httpd.conf. Around line #182, changeDocumentRoot "C:/xampp/htdocs"toDocumentRoot "C:/xampp/htdocs/public_html".
That way, you do not have to set up to develop with the CodeIgniter directory tree under/htdocs/public_html/i.e. in a publicly-visible directory. - To access SQLite3 via PDO (required for SQLite3),
enable/un-comment
extension=php_pdo.dll and extension=php_pdo_sqlite.dllin\xampp\php\PHP.ini
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
- Set the base URL in:
CodeIgniter/system/application/config/config.php … $config['base_url'] = "http://127.0.0.1/";
This is one setting that likely differs on your production server. - Change the default route class using:
CodeIgniter/system/application/config/routes.php … $route['default_controller'] = "welcome";to the name of your application controller class (without the .php suffix). - Edit the "Autoload Libraries" section of
/CodeIgniter/system/application/config/autoload.php... and add 'database' to the Autoload array:$autoload['libraries'] = array('database');
If your debugger says, "invalid database connection group" , make sure that you've done this - to specify that a database library is to be auto-loaded. - Add/edit database connection groups in
/CodeIgniter/system/application/config/database.php... and edit/set the currently-desired 'active_group' e.g.$active_group = "Dbs"; - Download
pdo_sqlite_driver.zipfrom the CodeIgniter Wiki files library. Un-zip it into:C:\xampp\CodeIgniter\system\database\drivers\pdo\ - During CodeIgniter initialization all global variables are unset (docs: General Topics/URI Security/Register_globals) i.e. like register_globals = off.
- To debug database connection issues, specify:
$db['default']['db_debug'] = TRUE; - If, especially with a new installation, you get the error:
"could not find driver", make sure that you have un-commented the database drivers inPHP.ini. If using SQLite, the PDO library (extension=php_pdo.dll) must be loaded before the SQLite library … and don't forget to un-commentextension=php_pdo_sqlite.dll.
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.
- The
/CodeIgniter/index.phpfile (the CodeIgniter "front controller") needs to be moved to that public web root directory (e.g./public_html/index.php). - In the index.php file,
$system_folder = "system";(~ line 27) must be changed to the relative path fromindex.phpto the new/system/ directory location e.g.$system_folder = "../CodeIgniter/system"; - In the .htaccess file, adjust RewriteBase e.g.
RewriteBase /public_html/toRewriteBase /for your production site, if your production site is in your web host document root. If the index() page renders, but the site won't do routing, check the RewriteBase setting. - In
/system/application/config/config.phpset$config['base_url'] = " http://127.0.0.1/dbs/ "; (or to whatever desired directory). This is one setting that can't stay the same as on your local test workstation, because on your production server it will, probably, be different i.e.:$config['base_url'] = " http://yourDomain.com";. - Adjust the styles path (e.g.
DBSSTYLESconstant) in the articles class constructor. - Consider defining a controller class constant (e.g.
DBSARTICLES) to provide the relative path from the/views/directory to the static HTML page directories. - Adjust (e.g. SQLite) database paths in
/CodeIgniter/system/application/config/database.php
Controller & View subDirectories
If you use directories beneath the
/application/controllers/ or /application/views/
directories, you must:
- Use the controller subdirectory in the URI (making it a URL-visible segment).
- Specify the view subdirectory in the controller’s
load->viewstatement e.g.$this->load->view('articles/PHPnotes.htm').
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/CodeIgniter –
not 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