architecture_codeigniter

Controller, View & Routes – Tutorial 2

The default controller & Method is defined in app – Config – Routes.php

Now we will create a new controller and it’s method we will call it from URL. First set your env file to .env file and change CI_ENVIRONMENT = development

Open terminal from directory and enter command php spark serve

Create a new controller called Site. We declare default namespace and class is extending BaseController.

namespace App\Controllers;

class Site extends BaseController
{
}

Now insert some code into index method and try to load this controller by URL http://localhost:8080/site

namespace App\Controllers;

class Site extends BaseController
{
    public function index()
    {
        echo 'This is index method of Site controller';
    }
}

You can see there is a 404 error. Because you have to define route for this Controller method. So add this line just below the default route is defined and URL will start working.

$routes->get('/site', 'Site::index');

Now create another method there.

namespace App\Controllers;

class Site extends BaseController
{
    public function index()
    {
        echo 'This is index method of Site controller';
    }

    public function aboutus()
    {
        echo 'This is about us method';
    }
}

Try to access URL http://localhost:8080/site/aboutus but it gives you 404 error stating: Can’t find a route for ‘get: site/aboutus’.

We know the CI URL structure: example.com/controller/method/parameter. So why this is not working in this case? Do I have to declare Route for each method? See what ChatGTP told me: In CodeIgniter, you typically register routes to define the URLs and map them to specific controller methods. However, you do not necessarily need to register a route for every new method you create.

So why this not working? Because in default CI setup auto-routing is not enabled. We need to enable it.

Add this line to your Routes.php file

$routes->setAutoRoute(true);

and it will start working. If you want a SEO friendly URL to access this controller method we can add Route for this method.

Added this line to Routes.php file

$routes->get('/site/about-company', 'Site::aboutus');

And you can access that page via URL http://localhost:8080/site/about-company

Great. Now start creating view files. Create a view file under Views – site – aboutus.php

Change the method code. http://localhost:8080/site/aboutus will start showing the page content.

public function aboutus()
    {
        return view("site/aboutus");
    }

Now passing the data from Controller method to view file. To pass the data we just need a array of values and pass to the view method. like

public function aboutus()
    {
        $data = array(
            'name' => 'Criatix Infotech',
            'service' => 'Web design & development',
            'founded' => 2013
        );
        return view("site/aboutus", $data);
    }

The view file looks like:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>About us - <?php echo $name; ?></title>
</head>

<body>
    <h1>Welcome to <?= $name ?></h1>
    <h2>We provides <?= $service ?> services and we founded in year of <?= $founded ?></h2>
</body>

</html>

Now we separate the logic to load header, body & footer by loading multiple view files. We can simply load more view files by echo statement in method.

echo view('header.php', $data);
echo view("site/aboutus", $data);
echo view('footer.php', $data);

View Layout

Now view file is not final file that generates HTML because view files becomes bigger and messy in many cases so we divide the that files in chunks called sub view and we will extend those sub-views into main files. Same as Laravel. Check this concept in Laravel.