What is CodeIgniter?
CodeIgniter is an open-source MVC framework for application development in php. It is simple to setup and easy to configure which makes it widely used by developers. It provides various libraries and helpers which makes it faster and easy to add functionality.
- Php version 5.6 or newer.
- MySQL 5.1 or newer.
How to download and setup Codelgniter?
1. Download version 3.1.11 Download.
2. Unzip the folder inside server.
3. Configure base URL : go to application/config/config.php file.
1.Set $config[‘base_url’] = ‘site_url’;
2.set index page : $config[‘index_page’] = ”;
4. Database configuration : go to application/config/database.php file.
$db[‘default’] = array(
’dsn’ => ”,
’hostname’ => ‘localhost’,
’username’ => ‘user_name’,
’password’ => ‘password’,
’database’ => ‘database_name’,
’dbdriver’ => ‘mysqli’,
’dbprefix’ => ”,
’pconnect’ => FALSE,
’db_debug’ => (ENVIRONMENT !== ‘production’),
’cache_on’ => FALSE,
’cachedir’ => ”,
’char_set’ => ‘utf8’,
’dbcollat’ => ‘utf8_general_ci’,
’swap_pre’ => ”,
’encrypt’ => FALSE,
’compress’ => FALSE,
’stricton’ => FALSE,
’failover’ => array(),
’save_queries’ => TRUE
By changing the $db array variable to ‘test’ it can be set for test environment.
- Auto Load configuration: $autoload[‘libraries’]= array[‘libraries to be included for autoload’];
Application flow in CodeIgniter:
- The index.php provide frontend controller for initializing base resource.
2. Router controls the http request, if file is already in cache it is sent directly to the browser.
3. Before passing to the controller data is checked by security.
4. It’s the controller which load the models to provide access to database, libraries and helpers etc.
Routing in CodeIgniter:
Routes are used for listening URL request. If a request matches a predefined route it is redirected to the URL address otherwise page not found exception is thrown.
Routes are defined in typical manner in CodeIgniter:
Site-URL / controller-name / method-in-controller / any-parameter.
Session in CodeIgniter
Session data is available throughout the site but to use these data we must initialize the session library.
After the library is loaded, session instance can be used :
Session value are stored as :
Passing data from controller to view in CodeIgniter
View is loaded from controller by :
We can pass data to view as an array in form of second parameter in view() function.
$data[‘data-to-pass’] = ‘name’;
The only twist is when data is retrieved in ‘view’.Parameter passed in array becomes the variable in view.
echo ‘name = ’.$data-to-pass;
Passing data from view to controller in CodeIgniter
Data is passed from view to controller in form post data.
<form method =”post” action=”<? Base_url(); ?>controller_name/controller_method_name”>
While on receiving end:
$form_data = $this->input->post();
Passing data from controller to Model in CodeIgniter
Before we use the service of a model, we must load the module into our controller class.
If we load model in constructor of our controller class, we don’t need to load it again anywhere else in the controller class. So, we can call the model function inside controller function and pass the data as parameter to model function.
Hope this article is helpful for you to setup your first CodeIgniter project locally. If you have any query please feel free to contact us.