You are reading the article Php Mvc Framework Tutorial: Codeigniter Example updated in September 2023 on the website Phuhoabeautyspa.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Php Mvc Framework Tutorial: Codeigniter Example
What is PHP MVC framework?PHP MVC is an application design pattern that separates the application data and business logic (model) from the presentation (view). MVC stands for Model, View & Controller.
The controller mediates between the models and views.
Think of the MVC design pattern as a car and the driver.
The car has the windscreens (view) which the driver (controller) uses to monitor traffic ahead then speed or brake (model) depending on what he sees ahead.
Why use PHP MVC Framework?
PHP MVC Frameworks simplify working with complex technologies by;
Hiding all the complex implementation details
Providing standard methods that we can use to build our applications.
Increased developer productivity, this is because the base implementation of activities such as connecting to the database, sanitizing user input etc. are already partially implemented.
Adherence to professional coding standards
In this tutorial, you will learn-
PHP MVC Design PatternLet’s now briefly discuss each component of the MVC design pattern.
Model – this part is concerned with the business logic and the application data. It can be used to perform data validations, process data and store it. The data can come from;
flat file
database
XML document
Other valid data sources.
Controller – this is the part deals with the users’ requests for resources from the server.
As an example, when the users requests for the URL …/index.php?products=list, the controller will load the products model to retrieve the products data then output the results in the list view.
In a nutshell, the controller links the models and views together depending on the requested resources.
Views – this part deals with presenting the data to the user. This is usually in form of HTML pages.
Types of PHP MVC frameworkSelecting the best PHP framework is a challenge.
You should only attempt to create your own MVC related application design for understanding how MVC frameworks work.
Once you are comfortable with the way MVC frameworks work, you should move on to the mature and already tested frameworks.
The table below briefly describes some of the popular php frameworks and the features that each framework offers.
Framework
Description
CodeIgniter
It is one of the most popular PHP MVC frameworks. It’s lightweight and has a short learning curve. It has a rich set of libraries that help build websites and applications rapidly. Users with limited knowledge of OOP programming can also use it. CodeIgniter powered applications include;
Kohana
http://kohanaframework.org
It’s a Hierarchical Model View Controller HMVC secure and lightweight framework. It has a rich set of components for developing applications rapidly. Companies that use Kohana include;
CakePHP
www.cakephp.org
It is modeled after Ruby on rails. It’s known for concepts such as software design patterns, convention over configuration, ActiveRecord etc. CakePHP powered applications include;
Zend
It is a powerful framework that is;
Secure, reliable, fast, and scalable
Supports Web 2.0 and creation of web services.
Pimcore CMS,
DotKernel.
Companies using the Zend framework include;
BBC
Cisco
Webex
Porting the opinion poll application to CodeIgniter
In this tutorial, we created a PHP poll application. Here, we will port that code to CodeIgniter
Download the latest version of CodeIgniter from their website.
Extract the contents of the zipped file to your development directory in your web server directory. We will use ciopinionpoll as the folder name in this lesson.
We are now going to port our opinion poll application to CodeIgniter. Recall that our application was divided into three major components namely the;
Front controller – this is the part that responds to URL requests and returns the requested page. This code will go into the controller
Model – this is the code that responds to data requested and returns the requested data. This code will go into the model
Views – this is the code responsible for formatting and displaying the data. This code will go into the view
Browse to ciopinionpoll folder
Open the chúng tôi file located in application/config directory.
Locate the following lines of code
Database configuration settings
Set the username to root
Set the password to your localhost root password
Database name to opinion_poll. Note we will be using the database created in the previous lesson.
Save the changes and close the file.
Creating Our ModelNext we are going to create our model that will extend the CI_Model. The CI_Model is part of the CodeIgniter libraries. The model will be located in application/models opinion_poll_model.php
<?php class Opinion_poll_model extends CI_Model { public function __construct() { } public function total_votes() { } public function get_results() { $libraries = array("", "JQuery", "MooTools", "YUI Library", "Glow"); $table_rows = ''; for ($i = 1; $i < 5; $i++) { $sql_stmt = "SELECT COUNT(choice) choices_count FROM js_libraries WHERE choice = $i;"; } public function add_vote($choice) { } }HERE,
“class Opinion_poll_model extends CI_Model…” is our model that extends the CI_Model
“…parent:: __construct();” calls the CI_Model constructor
Creating Our Controller Let’s now create the controller. We will use the default CodeIgniter controller located in application/controllers/welcome.php. Replace its source codes with the following code.
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); class Welcome extends CI_Controller { public function __construct() { parent::__construct(); } public function index() { } } else { } } } /* End of file chúng tôi */ /* Location: ./application/controllers/welcome.php */HERE,
“if (!defined(‘BASEPATH’)) exit(‘No direct script access allowed’);” ensures that users do not directly access the controller class
“class Welcome extends CI_Controller…” our controller extends the CI_Controller class
Creating Our Views
Recall from the previous example that we had two HTML pages, one for voting and the other for results. We will use the same HTML code with minimal modifications to create our views. Create the following files in application/views directory
opinion_poll_form.php JavaScript Libraries - Opinion PollLet’s now create the results page results.php
Testing our applicationConclusion
CodeIgniter is an easy to learn and use PHP MVC framework that can greatly reduce the time spent developing applications.
Summary
A framework is a set of libraries that provide partial implementation of common tasks.
PHP has a number of open source mature and tested MVC frameworks.
Traditional PHP applications that follow application design best practices can be ported to MVC frameworks with minimal modifications.
You're reading Php Mvc Framework Tutorial: Codeigniter Example
Update the detailed information about Php Mvc Framework Tutorial: Codeigniter Example on the Phuhoabeautyspa.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!