Showing posts with label Configuaration. Show all posts
Showing posts with label Configuaration. Show all posts

Monday, August 20, 2012

Codeigniter Initial Configuration


In this section, you learn the ins and outs of the files inside the application/config/ folder. Four of them are important for your project and they are :
  •  config.php
  •  database.php 
  • autoload.php 
  •  routes.php  

 Config.php

 The config.php file contains a series of configuration options (all of them stored in a PHP array called,appropriately enough, $config ) that CodeIgniter uses to keep track of your application ’ s information and settings. The first configuration option you need to set inside config.php is the base URL of your application. You do that by setting the absolute URL (including the http:// part) for $config[ ‘ base_url ’ ] , like so:

$config[‘base_url’] = “http://www.example.com/test/”;

 Once you ’ ve set this configuration option, you can recall it whenever you want using the CodeIgniter base_url() function, which can be a very handy thing to know. This one feature keeps you from having to  rewrite hard - coded URLs in your application, when you migrate from development to test or from test to production.
The second thing you need to do is set a value for your home page by editing the $config[ ‘ index_page ’ ] configuration option. CodeIgniter ships with a value of “ index.php ” for this option, which means that index.php will appear in all of your URLs. Many CodeIgniter developers prefer to keep this value blank, like so:

$config[‘index_page’] = ‘’;


To make this work, you need to include an .htaccess file to the CodeIgniter root directory, After you ’ ve set this option value, there ’ s very little to do. For now, leave all the other values at their default settings:


$config[‘uri_protocol’] = “AUTO”;                            
$config[‘url_suffix’] = “”;                                           
$config[‘language’] = “english”;                                
$config[‘charset’] = “UTF-8”;                                    
$config[‘enable_hooks’] = FALSE;
$config[‘subclass_prefix’] = ‘MY_’;
$config[‘permitted_uri_chars’] = ‘a-z 0-9~%.:_-’;     
$config[‘enable_query_strings’] = FALSE;
$config[‘controller_trigger’] = ‘c’;
$config[‘function_trigger’] = ‘m’;
$config[‘log_threshold’] = 0;                                       
$config[‘log_path’] = ‘’;                                              
$config[‘log_date_format’] = ‘Y-m-d H:i:s’;                

$config[‘cache_path’] = ‘’;                                            
$config[‘encryption_key’] = “enter_a_32_character_string_here”;
$config[‘sess_cookie_name’] = ‘ci_session’;
$config[‘sess_expiration’] = 7200;
$config[‘sess_encrypt_cookie’] = TRUE;
$config[‘sess_use_database’] = FALSE;
$config[‘sess_table_name’] = ‘ci_sessions’;
$config[‘sess_match_ip’] = FALSE;
$config[‘sess_match_useragent’] = TRUE;
$config[‘cookie_prefix’] = “”;
$config[‘cookie_domain’] = “”;
$config[‘cookie_path’] = “/”;
$config[‘global_xss_filtering’] = TRUE;
$config[‘compress_output’] = FALSE;
$config[‘time_reference’] = ‘local’;
$config[‘rewrite_short_tags’] = FALSE

 For more details on each of these configuration options, simply read the comments embedded in /system/application/config/config.php. You will also get more detail on certain settings as you work through the sections of the book and tweak the configuration as needed. For example, at some point, you will want to use encryption for security purposes or set your logging threshold for debugging, and they both require making changes to this file.


 database.php

 The database.php file contains all the information required to connect to a database. Currently, CodeIgniter supports mysql, mysqli, postgres, odbc, and mssql connections. To connect to your database, simply enter valid information for your hostname, username, password, database name, and database driver. Each of these is stored in the $db array under the “ default ” group, which means you could have numerous connection groups, each with their own unique name. For example, you could have one set of connection variables for your development environment and another for your production environment. As long as you ’ ve set the $active_group variable correctly, your application will keep connected.
$active_record = TRUE;
$active_group = “default”;
$db[‘default’][‘hostname’] = “localhost”;
$db[‘default’][‘username’] = “db_username”;
$db[‘default’][‘password’] = “db_password”;
$db[‘default’][‘database’] = “db_name”;
$db[‘default’][‘dbdriver’] = “mysql”;
$db[‘default’][‘dbprefix’] = “”;
$db[‘default’][‘pconnect’] = TRUE;
$db[‘default’][‘db_debug’] = TRUE;
$db[‘default’][‘cache_on’] = FALSE;
$db[‘default’][‘cachedir’] = “”;
$db[‘default’][‘char_set’] = “utf8”;
$db[‘default’][‘dbcollat’] = “utf8_general_ci”;

autoload.php

The autoload.php file specifies which systems are automatically loaded by CodeIgniter. There ’ s one school of thought that maintains that the autoloader should be kept as minimal as possible to keep the framework lightweight and nimble, but another school of thought maintains that certain frequently used systems should be autoloaded instead of called at the local level repeatedly. You ’ ll find your own balance as experience and local project needs warrant, but for now, set your options like this:

$autoload[‘libraries’] = array(‘database’,’session’,’email’,’validation’);
$autoload[‘helper’] = array(‘url’,’form’,’text’,’date’,’security’);
$autoload[‘plugin’] = array(‘captcha’);
$autoload[‘model’] = array();
$autoload[‘config’] = array();
The first $autoload option, libraries , is a list of libraries that should be loaded. You ’ re going to be using the database, session, e - mail, and form validation libraries a lot in this (and many other) CodeIgniter project(s), thus loading them now makes a good deal of sense. You learn more about these libraries later. The second $autoload option, helper , is a list of helper collections that you will need to get work done. Almost every CodeIgniter project uses the URL, form, and text helpers frequently; it ’ s not a bad idea to have date and security autoloaded as well, particularly as you ’ ll need them to help you parse dates and handle security features. For the time being, keep the third $autoload option, plugin , either blank or filled in with the CAPTCHA plugin, as you may have a need to use CAPTCHA devices in forms for your application for extra security. The fourth $autoload option, model , allows you to autoload models. This is a new feature ofCodeIgniter 1.6. You are going to create two basic models for Claudia ’ s Kids below, one that handles product data and the other that handles category data. When you create those two models, you ’ ll have the opportunity to come back to this file and autoload them. Finally, the fifth $autoload option, config , is where you would list any custom configuration files. As it ’ s unlikely that you ’ ve created any of those at this point, leave it blank.


routes.php 

The routes.php file lets you remap URI requests to specific controller functions. For example, you may have  a controller named site with a function named index . The URI for this controller/function combination might be :
http://www.example.com/site/index
Furthermore, if your site controller had a pages function that accepted a numeric ID for database lookup, the URI might look like this:
http://www.example.com/site/pages/4
In some cases, you might want to remap one or more of these default routes. For example, the second
example might be better displayed as this:
http://www.example.com/about_us/
In that case, your routes.php file would contain a rule like this:
$route[‘about_us’] = “site/pages/4”;
For right now, though, this kind of manipulation falls under “ advanced usage, ” so don ’ t worry too much
about it. However, please do note that this kind of thing is possible. Also, be aware that two “ reserved
routes ” exist: default_controller and scaffolding_trigger.
$route[‘default_controller’] = “welcome”;
The default_controller route tells CodeIgniter which controller should be loaded if no controller is
identified. For simplicity ’ s sake, keep this setting at welcome, because you ’ re going to create a welcome
controller for the project associated with this book.


For more information visit the official website of codeigniter see the userguide.