Codeigniter One of the best PHP framework
Contents
Codeigniter One of the best PHP framework
Codeigniter is one of the popular and widely used framework out of many php based MVC frameworks. It has small footprint, minimalist architecture and fewer resources to load which makes the framework lighter and faster than it’s competitors.
Yes, we know the downside of singleton architecture it’s following, but the next major release i.e codeigniter 4 will be written ground up completely. It will eliminate the singleton pattern and will be based on the PHP 7! interesting isn’t it?
If you are working on mid scale web application project or you are a beginner to intermediate developer looking for a web development framework which is fast and easy to learn, I would recommend you have a look at codeigniter development framework.
If you are familiar with MVC concept, oop and basics of PHP you are good to go for your first MVC based web application using codeigniter.
If you want to jump start into the code igniter development, well we have this article for you guys.
The article will explain:
In this blog we will cover up following topics:
- What is MVC?
- Codeigniter structure architecture.
- Create a static program with controller and view.
- Interaction with model & view.
- Some basic Instruction regarding CI.
What is MVC?
Model
Responsible for maintaining data.
Contains Business Logic.
View
Responsible for displaying all or a portion of the data to the user.
Does not store any data except to cache state.
Controller
Responsible for interactions between the Model and View.
Updates the view when the model changes
Codeigniter structure architecture
Let’s study the basic structure and architecture by taking an example of User Registranion.
Codeigniter One of the best PHP framework ,It was built for PHP coders who need a simple and elegant toolkit to create full-featured web applications.
Create a static program with controller and view
-
Installation Of CodeIgniter
Installation of CodeIgniter is quite simple. Just navigate Codeigniter official site,you will get the Download link.
- Download the CodeIgniter latest version.
- Extract the downloaded file and put it in your server folder Apache24/htdocs or Xampp/htdocs,vice versa.
- Set base url at application/config/config.php i.e. $config[‘base_url’] = ‘http://localhost/ci/’.
- Goto browser and run CI.
-
Attach database with Codeigniter
Attachment of database in CI is very easy.
- Explore the CI structure.
- Navigate to application/config/database.php.
- At the bottom of the page you will get an array named $db.
- Assign the host name, user name,password and the database name. Now your database has been connected with CI.
Now navigate to application/controller/, Create new file here, with any name it will be your controller.
Note : The name of Controller should be starts with Capital letter.
application/controller/firstcontroller.php
<?php class Firstcontroller extends CI_Controller { public function __construct() { parent::__construct(); } public function index() { echo "This is My Frist Controller"; } }
While, working under MVC ,Open any file in browser you don’t need to call certain page, in place call the contraller, A controller has multiple methods and they act as an individual page.
In our case we can call our controller like following
localhost/start_ci/index.php/firstcontroller/index
Interaction with model and view
consider following case represents the student info.
Create Table
CREATE DATABASE `student_info`; CREATE TABLE IF NOT EXISTS `student` ( `student_id` int(11) NOT NULL, `name` varchar(200) NOT NULL, `city` varchar(200) NOT NULL, `contact` varchar(100) NOT NULL, email varchar(200) NOT NULL ) ALTER TABLE `student` MODIFY `student_id` int(11) NOT NULL AUTO_INCREMENT;
1.Create a model
application/model/Student_model.php
class Student_model extends CI_Model { public function __construct() { parent::__construct(); $this->load->database(); } public function get_all() { $sql = $this->db->get('students_table'); } ?>
Note : Make sure the name of model and class should be same.
2.Create a controller
application/controller/Student_controller.php
class Student_controller extends CI_Controller { public function __construct() { parent::__construct(); $this->load->model('student_model'); //load the model } public function index() { $get_all = $this->student_model->get_all(); $data = array( "page_title" => "Student List", "get_all" => $get_all ); //every key of $data array will be used as a variable in view $this->load->view("student_list", $data); //call view and pass the $data array }
3.Create a view
application/view/student_list.php
<html> <head> <title>Student List</title> </head> <body> <table> <thead> <tr> <th>Name</th> <th>City</th> <th>Contact No</th> <th>Email</th> </tr> </thead> <tbody> <?php foreach ($get_all as $key=>$val): //the array of student collection ?> <tr> <td><?=$val['name'];?></td> <td><?=$val['city'];?></td> <td><?=$val['contact'];?></td> <td><?=$val['email'];?></td> </tr> <?php endforeach;?> </tbody> </table> </body> </html>
Some basic Instruction regarding CI
Use of .htaccess
execute ci on browser
localhost/start_ci/index.php/firstcontroller/
to remove index.php from url you need to write following code in .htaccess
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # RewriteCond $1 !^(client) RewriteRule ^(.*)$ index.php?/$1 [L] </IfModule>
Use of autoload
localhost/start_ci/index.php/autoload.php
In this file you can load helper functions, codeigniter built in libraries,database etc so that no longer need to load them individually in every page.
I hope you will find this post very useful regarding CodeIgniter. Let me know any questions if you have in comment regarding WordPress. I will reply you ASAP.
Looking for CODEIGNITER/PHP consultation?
CONTACT US for free consultation