AutoLoad Helper
In CodeIgniter Project, Open autoload.php file in config folder. Add url helper to helper config as below:
$autoload['helper'] = array('url');
Set BASE URL
In CodeIgniter Project, Open config.php file in config folder. Set value for base_url config as below:
$config['base_url'] = 'http://localhost:9092/LearnCodeIgniterWithRealApps/';
Create Controller
Create new PHP file named demo.php in controllers folder as below:
<?php defined('BASEPATH') or exit('No direct script access allowed');
class Demo extends CI_Controller
{
public function index()
{
$this->load->view('demo/index');
}
public function demo1($id)
{
$data['id'] = $id;
$this->load->view('demo/demo1', $data);
}
public function demo2($id1, $id2)
{
$data['id1'] = $id1;
$data['id2'] = $id2;
$this->load->view('demo/demo2', $data);
}
public function demo3()
{
$data['id1'] = $_GET['id1'];
$data['id2'] = $_GET['id2'];
$this->load->view('demo/demo3', $data);
}
}
Set Default Controller
In CodeIgniter Project, Open routes.php file in config folder. Set value for default_controller as below:
$route['default_controller'] = 'demo';
Create View
Create new folder named demo in views folder. In this folder, create new views as below:
Index View
Create new PHP file named index.php as below:
<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<html>
<head>
<title>Demo Page</title>
</head>
<body>
<a href="<?php echo site_url('demo/demo1/123'); ?>">Demo 1</a>
<br>
<a href="<?php echo site_url('demo/demo2/123/abc'); ?>">Demo 2</a>
<br>
<a href="<?php echo site_url('demo/demo3?id1=456&id2=def'); ?>">Demo 3</a>
</body>
</html>
Demo1 View
Create new PHP file named demo1.php as below:
<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<html>
<head>
<title>Demo 1 Page</title>
</head>
<body>
Id: <?php echo $id; ?>
</body>
</html>
Demo2 View
Create new PHP file named demo2.php as below:
<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<html>
<head>
<title>Demo 2 Page</title>
</head>
<body>
Id 1: <?php echo $id1; ?>
<br>
Id 2: <?php echo $id2; ?>
</body>
</html>
Demo3 View
Create new PHP file named demo3.php as below:
<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<html>
<head>
<title>Demo 3 Page</title>
</head>
<body>
Id 1: <?php echo $id1; ?>
<br>
Id 2: <?php echo $id2; ?>
</body>
</html>
Structure of CodeIgniter Project
Run Application
Access index action in Demo controller with following url: http://localhost:9092/LearnCodeIgniterWithRealApps/demo/index
Output
Click Demo 1 link will call to demo1 action in Demo controller with following url: http://localhost:9092/LearnCodeIgniterWithRealApps/index.php/demo/demo1/123
Output
Click Demo 2 link will call to demo2 action in Demo controller with following url: http://localhost:9092/LearnCodeIgniterWithRealApps/index.php/demo/demo2/123/abc
Output
Click Demo 3 link will call to demo3 action in Demo controller with following url: http://localhost:9092/LearnCodeIgniterWithRealApps/index.php/demo/demo3?id1=456&id2=def
Output
References
I recommend you refer to the books below to learn more about the knowledge in this article:
- CodeIgniter Web Application Blueprints
- CodeIgniter for Rapid PHP Application Development: Improve your PHP coding productivity with the free compact open-source MVC CodeIgniter framework!
- Programming with CodeIgniter MVC
- Murach’s PHP and MySQL (3rd Edition)
- Learning PHP, MySQL & JavaScript: With jQuery, CSS & HTML5 (Learning Php, Mysql, Javascript, Css & Html5)
- PHP and MySQL Web Development (5th Edition) (Developer’s Library)