Codeigniter and MVC: Enterprise Class Web Application Development
Codeigniter and MVC: Enterprise Class Web Application Development
• You have worked with PHP, for small sites this works very well. HTML files
can be easily extended with dynamic content from the database, form
processing, etc.
• When sites grow, you might have realized that across multiple pages lots of
code repetition occurs. This is a problem when you need to change certain
parts of a page, that affects many or all pages.
• Enterprise class (big) web applications need structure, one HTML file per
page with PHP code is not optimal => things get confusing, hard to work in
teams, etc.
• Most common for Web application development are frameworks that are
based on the Model-View-Controller design pattern
• Controller: Handles all incoming HTTP requests, passes data to the views
• There are countless similar PHP frameworks, the most popular ones being
CakePHP and symfony. Ruby on Rails is famous in the Ruby world.
• CodeIgniter is very light weight. It doesn‘t force any convention but provides
many commonly required features through a set of build in libraries.
• CodeIgniter has a low learning curve and is one of the best documented PHP
web frameworks.
• CodeIgniter is Free
CodeIgniter is licensed under the MIT license so you can use it
however you please.
• CodeIgniter is Fast
Really fast.
• CodeIgniter is Extensible
The system can be easily extended through the use of your own l
ibraries, helpers, or through class extensions or system hooks.
Dienstag, 4. Mai 2010
CodeIgniter: Features
• Data Encryption
• Benchmarking
• Full Page Caching
• Error Logging
• Application Profiling
• Calendaring Class
• User Agent Class
• Zip Encoding Class
• Template Engine Class
• Trackback Class
• XML-RPC Library
• Unit Testing Class
• Search-engine Friendly URLs
• Flexible URI Routing
• Support for Hooks and Class Extensions
• Large library of “helper” functions
Dienstag, 4. Mai 2010
Application Flow Chart
1. The index.php serves as the front controller, initializing the base resources needed
to run CodeIgniter.
2. The Router examines the HTTP request to determine what should be done with it.
3. If a cache file exists, it is sent directly to the browser, bypassing the normal
system execution.
4. Security. Before the application controller is loaded, the HTTP request and any
user submitted data is filtered for security.
5. The Controller loads the model, core libraries, plugins, helpers, and any other
resources needed to process the specific request.
6. The finalized View is rendered then sent to the web browser to be seen. If caching
is enabled, the view is cached first so that on subsequent requests it can be
served.
Dienstag, 4. Mai 2010
Installing CodeIgniter
1) Download CodeIgniter from its official website.
https://www.codeigniter.com
2) Unzip CodeIgniter package.
• Downloaded CodeIgniter will be in zip format.
• Unzip it
• Create folder named CodeIgniter in
C>xampp>htdocs
• Copy content of unzip folder inside CodeIgniter
folder.
Installing CodeIgniter
Installing CodeIgniter
• Open browser and type:
localhost/CodeIgniter
Routing Requests
/index.php/controller/action
/application/config/routes.php
Index.php
• In this file, we can set the application environment and error level
and we can define system and application folder name.
• recieves all requests and routes to the right controllers classes
and actions, parameters are included in the URL
MVC
• Models are objects, which represent the
underlying data. They hover above the database
and access it as required. They can also perform
operations on data to add meaning to it.
• Views show the state of the model. They are
responsible for displaying information to the end
user.
• Controllers offer options to change the state of
the model. They are responsible for consulting
models. They provide the dynamic data to views.
MVC
• CI has subfolders for models, views, and controllers.
Each file within them is a .php file, usually in the form
of a class that follows certain naming conventions.
• CI MVC is based on
• Loose Coupling: Coupling is the degree to which the
components of a system rely on each other. The less
the components depend on each other, the more re-
usable and flexible the system becomes.
• Component Singularity: Singularity is the degree to
which components have a narrowly focused purpose.
In CodeIgniter, each class and its functions are highly
autonomous in order to allow maximum usefulness.
Controller
• A controller is the intermediary between models and views to
process HTTP request and generates a web page.
• Must extend the main Controller class of CodeIgniter
(CI_Controller)
• Each class function represents an controller action, which is
redering a HTML page
• index() is the default action
• The name of the controller class must start with an uppercase
letter.
• The controller must be called with lowercase letter.
• Default Controller: The file specified in default controller will be
loaded by default when no file name is mentioned in the URL. By
default, it is Welcome.php which is the first page to be seen after
installing CodeIgniter.
Controller
• Default Controller
• To specify own default controller, open
your application/config/routes.php file and
set this variable:
$route['default_controller'] = ‘Test';
• Where ‘Test’ is the name of the controller
class you want used
Controller Example
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$this->load->view('folder_name/file_name');
Adding Dynamic Data to the View
• Data is passed from the controller to the view
by way of an array or an object in the second
parameter of the view loading function
• If you use an object, the class variables will
be turned into array elements.
Adding Dynamic Data to the View:Array
Controller: ViewArray.php View: firsthome.php
<?php <html>
class ViewArray extends CI_Controller <head>
{ <title><?php echo $title;?></title>
function index()
{ </head>
$data=array( <body>
'title'=>'Homepage', <h1><?php echo $content;?></h1>
'content'=>'Welcome Here‘ </body>
); </html>
$this->load->view('firsthome',$data);
}
}
?>
Adding Dynamic Data to the View:Object
Controller: ViewObj.php View: sechome.php
<?php <html>
class ViewObj extends CI_Controller <head>
{ <title><?php echo $title;?></title>
function index() </head>
{ <body>
$data['title'] = "Homepage"; <h1><?php echo $content;?></h1>
$data['content'] ="Frameworks !!"; </body>
</html>
$this->load->view('sechome', $data);
}
}
?>
Creating Loops
(Controller: Comp.php)
<?php
class Comp extends CI_Controller
{
function index()
{
$data=array(
'title'=>'Company Homepage',
'content'=>'Welcome to kJSCE',
'projects'=>array('library management
system','online shopping system','provident fund statutory returns
system','hotel management system')
);
$this->load->view('home',$data, false);
}
}
?>
Creating Loops
(View: home.php)
<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
<br>
<h1><?php echo $content;?></h1>
<br><hr>
<h2>Projects done:<h2>
<ul>
<?php foreach($projects as $item){?>
<li><?php echo $item;?></li>
<?php }?>
</ul>
</body>
</html>
Model
• In any application you need to call a function to
retrieve some information from the database.
• Models responsibility is to handle all data logic
and representation and load data in the views.
• Model classes are stored in
your application/models/ folder.
• They can be nested within sub-folders if you
want this type of organization.
Model
Basic Prototype Example
class Model_name extends CI_Model { class User_model extends CI_Model {
Inserting Data
Method Chaining