Create new page in codeigniter
Here are the steps to create a new page in CodeIgniter:
Step 1: Create a new controller
In CodeIgniter, controllers are responsible for handling requests and returning responses. To create a new controller, navigate to the application/controllers
directory and create a new PHP file with a name that follows the convention My_controller.php
. For example, let's create a controller called Welcome.php
.
Step 2: Define the controller class
In the Welcome.php
file, define a class that extends the CI_Controller
class:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index() {
// Code to be executed when the controller is loaded
}
}
Step 3: Create a new view
In CodeIgniter, views are responsible for rendering the HTML content of a page. To create a new view, navigate to the application/views
directory and create a new PHP file with a name that follows the convention welcome.php
. For example, let's create a view called welcome.php
.
Step 4: Define the view content
In the welcome.php
file, define the HTML content of the page:
<?php
echo "Welcome to my page!";
?>
Step 5: Load the view in the controller
In the Welcome.php
file, load the welcome.php
view in the index()
method:
public function index() {
$this->load->view('welcome');
}
Step 6: Access the new page
To access the new page, navigate to the URL http://localhost/your_project/index.php/welcome
(replace your_project
with the name of your CodeIgniter project).
That's it! You have now created a new page in CodeIgniter.
Here's the complete code:
application/controllers/Welcome.php:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index() {
$this->load->view('welcome');
}
}
application/views/welcome.php:
<?php
echo "Welcome to my page!";
?>
Note: Make sure to update the routes.php
file in the application/config
directory to include the new controller and view. For example:
$route['welcome'] = 'welcome/index';
This will map the URL http://localhost/your_project/index.php/welcome
to the index()
method of the Welcome
controller.