AutoLoad Url 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 Template Page
Create new folder named templates in views folder. In templates folder, create new PHP file named mytemplate.php as below:
<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<a href="<?php echo site_url('home'); ?>">Home</a> |
<a href="<?php echo site_url('aboutus'); ?>">About Us</a> |
<a href="<?php echo site_url('news'); ?>">News</a>
<br><br>
<?php $this->load->view($page); ?>
<br><br>
Copyright PMK Lab
</body>
</html>
Create Controller
In controllers folder create new controllers as below:
Home Controller
Create new PHP file named home.php in controllers folder as below:
<?php defined('BASEPATH') or exit('No direct script access allowed');
class Home extends CI_Controller
{
public function index()
{
$data['title'] = 'Home Page';
$data['page'] = 'home/index';
$this->load->view('templates/mytemplate', $data);
}
}
AboutUs Controller
Create new PHP file named aboutus.php in controllers folder as below:
<?php defined('BASEPATH') or exit('No direct script access allowed');
class AboutUs extends CI_Controller
{
public function index()
{
$data['title'] = 'About Us Page';
$data['page'] = 'aboutus/index';
$this->load->view('templates/mytemplate', $data);
}
}
News Controller
Create new PHP file named news.php in controllers folder as below:
<?php defined('BASEPATH') or exit('No direct script access allowed');
class News extends CI_Controller
{
public function index()
{
$data['title'] = 'News Page';
$data['page'] = 'news/index';
$this->load->view('templates/mytemplate', $data);
}
}
Set Default Controller
In CodeIgniter Project, Open routes.php file in config folder. Set value for default_controller as below:
$route['default_controller'] = 'home';
Create Views
In views folder, create new views as below:
Home View
Create new folder named home. In home folder, create new view as below:
<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<h3>Home Page</h3>
AboutUs View
Create new folder named aboutus. In aboutus folder, create new view as below:
<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<h3>About Us Page</h3>
News View
Create new folder named news. In news folder, create new view as below:
<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<h3>News Page</h3>
Structure of CodeIgniter Project
Run Application
Access index action in home controller with following url: http://localhost:9092/LearnCodeIgniterWithRealApps/home/index
Output
Access index action in aboutus controller with following url: http://localhost:9092/LearnCodeIgniterWithRealApps/aboutus/index
Output
Access index action in news controller with following url: http://localhost:9092/LearnCodeIgniterWithRealApps/news/index
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)