Blog for Learning

| lesson material | material summary | questions and answers | definitions | types and examples | other information | materi pelajaran | ringkasan materi | pertanyaan dan jawaban | definisi | jenis-jenis dan contoh-contoh | informasi lainnya |

Advertisement

Powered by Blogger.

Visitors

Flag Counter

Pages

Codeigniter Tutorial # 2: MVC and Routing, Basic CI Concepts You Need to Understand

Understanding the Codeigniter MVC Concept
In the previous tutorial , we have learned how to install CI and get to know the directory structure of CI.
Now we continue to learn about routing and MVC.
The plan, we will create an online shop application (olshop).
However, before we start making applications with CI, it's good to get acquainted with the basic concept of CI.
Codeigniter is a frame that uses the MVC concept.
What is MVC?
Let's discuss ...

Know the MVC Concept on Codeigniter

MVC (Model, View, Controller) is a design pattern (design pattern) of application development architecture that separates and groups several codes according to their functions. 1
MVC divides the application into three functional parts: model, view, and controller.
  • Models are codes for business models and data. usually dealing directly with databases to manipulate data (insert, update, delete, search), handle validation from controller parts, but cannot relate directly to the view section.
  • View is the part that handles presentation logic . contains codes for display.
  • The controller is the part that regulates the relationship between the part of the model and the part of view , the controller functions to receive requests and data from the user then determine what the application will process.
MVC on Codeigniter
The workflow is like this: 2
  1. Start;
  2. The user sends a request to the web;
  3. The file that was first executed is index.php;
  4. Then from index.php, the request will be forwarded by routers.php;
  5. routers.phpwill search the cache on the server, if there is a cache , the cache will be sent as a response . If there is no cache then the request is forwarded to Controller ;
  6. The controller will be responsible for retrieving data from the Model and rendering it into View using existing libraries , plugins , and helper .
  7. The rendering result (view) is sent to the user and stored in the cache , if the cache feature is active;
  8. Finished.
Flochart Codeigniter application

Understanding the Router on Codeigniter

The Codeigniter router is responsible for determining the controller and the method / function to be executed.
When we open, http: // localhost / tokobuah / then the function to be executed is the function index()inside the controller welcome .
class Welcome extends CI_Controller {
 public function index()
 {
  $this->load->view('welcome_message');
 }
}
How come?
This is because the default router configuration is the controller welcome .
Try opening the file ðŸ“„ config/routers.php.
Default router
Function index()is a function that will be executed when we access the controller welcome .
Now try opening: http: //localhost/tokobuah/index.php/welcome/index , then we will get the same thing as opening http: // localhost / tokobuah / .
Route in Codeigniter

Creating Multiple Routers

As an exercise, try adding route /about/and /contact/ inside the Controller Welcome with this method:
public function about()
{
 // fungsi untuk me-load view about.php
 $this->load->view('about');
}

public function contact()
{
 // fungsi untuk me-load view contact.php
 $this->load->view('contact');
}
Add under method index().
Route about and contact
After that add two view files in the directory ðŸ“ viewswith the contents as follows.
📄 views/about.php
<h1>About Us</h1>
<p>Ini adalah halaman about</p>
📄 views/contact.php
<h1>Contact Us</h1>
<p>Ini adalah halaman Contact</p>
File View for About and Contact
We have added the route.
Now try opening:
The result:
Open the route in the browser
If we open http: //localhost/tokobuah/index.php/contact , there will be an error 404 not found .
404 Route not found
How come?
This is because the route that we have just created, has not been registered in routers.php.
Now open the file ðŸ“„ config/routers.php, then add the following code.
$route['about'] = 'welcome/about';
$route['contact'] = 'welcome/contact';
Add it under the existing route code.
Add route
The result:
Contact page
Do we have to add routes to routers.php in each new route creation?
The answer:
It doesn't have to be , because CI is also able to automatically detect routes based on controller names and methods that are created. 3
Route in Codeigniter
This is the format:
http://example.com/[controller-class]/[controller-method]/[arguments]
Adding a route is ðŸ“„ routers.phpneeded when we want to create a custom route for a particular controller.

0 Komentar untuk "Codeigniter Tutorial # 2: MVC and Routing, Basic CI Concepts You Need to Understand"

Silahkan berkomentar sesuai artikel

 
Template By Kunci Dunia
Back To Top