2 ND Assignment Major
2 ND Assignment Major
What is routing?
Routes are responsible for responding to URL requests. Routing matches the
URL to the pre-defined routes. If no route match is found then, CodeIgniter
throws a page not found an exception. (https://www.guru99.com/codeigniter-
url-routing.html)
Routing Example:
<?php
$routes->get('journals', 'Blogs');
URI routing?
CodeIgniter has two kinds of routing. One is Defined Route Routing, and
the other is Auto Routing. With Defined Route Routing, you can define
routes manually. It allows flexible URL. Auto Routing automatically routes
HTTP requests based on conventions and execute the corresponding
controller methods. There is no need to define routes manually.
(https://codeigniter4.github.io/CodeIgniter4/incoming/routing.html#what-is-
uri-routing)
URI routing is the process of taking the requested URI and deciding which
application handler will handle the current request.
(https://webapp2.readthedocs.io/en/latest/guide/routing.html#:~:text=URI
%20routing%20is%20the%20process,will%20handle%20the%20current
%20request.)
Example:
(https://codeigniter.com/userguide3/general/routing.html?highlight=uri
%20routing)
$route['journals'] = 'blogs';
A URL containing the word “journals” in the first segment will be remapped
to the “blogs” class.
$route['blog/joe'] = 'blogs/users/34';
(https://codeigniter.com/userguide3/general/routing.html?highlight=route)
Models are PHP classes that are designed to work with information in your
database. For example, let’s say you use CodeIgniter to manage a blog. You
might have a model class that contains functions to insert, update, and
retrieve your blog data. Here is an example of what such a model class might
look like:
class Blog_model extends CI_Model {
public $title;
public $content;
public $date;
public function get_last_ten_entries()
{
$query = $this->db->get('entries', 10);
return $query->result();
}
public function insert_entry()
{
$this->title = $_POST['title']; // please read the below note
$this->content = $_POST['content'];
$this->date = time();
$this->db->insert('entries', $this);
}
public function update_entry()
{
$this->title = $_POST['title'];
$this->content = $_POST['content'];
$this->date = time();
$this->db->update('entries', $this, array('id' => $_POST['id']));
}
}
How to create controller and view?
Creating controller:
Let’s create a simple controller so you can see it in action. Using your text
editor, create a file called Blog.php, and put the following code in it:
<?php
class Blog extends CI_Controller {
example.com/index.php/blog/
Hello World!
(https://codeigniter.com/userguide3/general/controllers.html)
Creating View:
Using your text editor, create a file called blogview.php, and put this in it:
<html>
<head>
<title>My Blog</title>
</head>
<body>
<h1>Welcome to my Blog!</h1>
</body>
</html>
Then save the file in your application/views/ folder.
(https://www.codeigniter.com/userguide2/general/views.html)