Adding Views in Codeigniter
PHP CodeIgniter

Adding Views in Codeigniter

Mishel Shaji
Mishel Shaji

What is a view?

A view is a user interface. It presents data from the model to the user and allows the user to modify the data. In a Codeigniter project, all the view files (views) are located in root_directory->application->views.

Creating a view

  • Navigate to root_directory->application->views.
  • Create a new folder and name it public.
  • Under public, create a new file named index.php.
  • Paste the following code.
<!DOCTYPE html>
<html lang="en" dir="ltr">
    <head>
        <meta charset="utf-8">
        <title>Welcome to CISite</title>
        <style>
            .navbar{
                display: inline;
                float: right;
                height: 50px;
                width: 100%;
                background-color: grey;
                margin: 0;
                padding: 0;
                padding-top: 20px;
            }
            .navbar>a{
                margin: 10px;
                color: white;
                font-weight: bold;
                text-decoration: none;
            }
            .body{
                margin: 0;
                padding: 0;
            }
            .logo{
                float:left;
            }
        </style>
    </head>
    <body>
        <div class="navbar">
            <a href="http://localhost/cisite" class="logo">CISite</a>
            <a href="about">About Us</a>
            <a href="login">Login</a>
            <a href="signup">Sign Up</a>
        </div>
        <h1>Welcome to CISite</h1>
        <h2>This is the Homepage</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    </body>
</html>

As you have already created a Controller for the view which we have created now, simply navigate to http://localhost/yourproject/user to see the user page. If you have not created a controller or appropriate routing rules, you will get an error.

DOWNLOAD SOURCE CODE

Download the complete project and refer the following files:

  • cisite/application/config/routes.php
  • cisite/application/controllers/PublicController.php
  • cisite/application/views/public/index.php