Make coding learn easy

How to make static page in CodeIgniter 3.1.2


Make static page in CodeIgniter 3.1.2 it's easy and simple. First we need going to codeigniter folder project, for example my folder C:\xampp\htdocs\myci.


after entered the folder codeigniter, we need to create a file at application/controllers/Pages.php with the following code:

<?php
class Pages extends CI_Controller {

        public function view($page = 'home')
        {
        }
}

next create header file, with create file at application/views/templates/header.php and add the following code:

<html>
        <head>
                <title>CodeIgniter Tutorial</title>
        </head>
        <body>

                <h1><?php echo $title; ?></h1>

and create footer, with create file at application/views/templates/footer.php that includes the following code:

                <em>&copy; 2016</em>
        </body>
</html>

after that, Adding logic the controller. before adding logic the controller we must create directory application/views/pages/, and create file home.php and about.php, and write in one of file "Hello world!" or anything you need to write, and save them.

for adding logic the controller, we need to open file application/controllers/Pages.php,and adding controller with following this code:

<?php
class Pages extends CI_Controller {

        public function view($page = 'home')
        { if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
        {
                // Whoops, we don't have a page for that!
                show_404();
        }

        $data['title'] = ucfirst($page); // Capitalize the first letter

        $this->load->view('templates/header', $data);
        $this->load->view('pages/'.$page, $data);
        $this->load->view('templates/footer', $data); }
}

after adding logic the controller, we need to routing. for routing we need to open file at  application/config/routes.php, and add two line with following this code:

$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';

after routing now we have make static page in codeigniter 3.1.2, for running we need to run localhost server application(xampp) and write url http://localhost/your_folder_codeigniter/index.php/home in browser.


0 Comments for "How to make static page in CodeIgniter 3.1.2"

Back To Top