CodeIgniter Introduction & Basics Part 2 – Create First Program

Create First Program

Based on our previous activity, we have created a new Route and Function called aboutUs inside the Site Controller. Now, let’s try to run our server and see how our web app should look like.

Type in;

php spark serve

Then, open our localhost in our browser. And add our new route which is about-us

http://localhost:8080/about-us

At this stage, we won’t be able to see anything yet. This is because we haven’t created our view. For testing purposes, let’s create our static view. Open your Site controller. Then, add the following code inside your aboutUs function;

echo "<h2> Welcome to About Us Page</h2>"

We now have this

Let us now create a new page called Contact Us. To do that, let’s first create a new Route by opening your Route.php then add the following code:

$routes->get('contact-us', 'Site::contactUs');

Then open your Site controller and add this function:

public function contactUs()
{
    return view("contact-us”);
}

The above code will call the contact-us view. So we need to create our view. In the app > Views folder, create a new PHP file called contact-us.php. Then add the following code:

<h2>Welcome to Contact Us Page</h2>

We will have this page: