Skip to content

Controllers

Keramat Jokar edited this page May 13, 2024 · 1 revision

What is a controller?

A controller is where all your page logic should be handled. CodeIgniter's own description: "Controllers are the heart of your application, as they determine how HTTP requests should be handled.".

In opposite of models which only should contain database actions, and views which should only contain layout code, controllers are where you should put your actual PHP code that should perform your necessary actions.

FusionCMS uses an implementation very similar to the official CodeIgniter controllers, except of that we are using a third party implementation that requires you to extend MX_Controller instead of CI_Controller.

For more information, please check out the CodeIgniter documentation

See the example controller below:

<?php

use MX\MX_Controller;

/**
 * News Controller Class
 * @property news_model $news_model news_model Class
 */
class News extends MX_Controller
{
    /**
     * Can be accessed via http://mypage.com/news/
     * or via http://mypage.com/news/news
     */
    public function index()
    {
        // Call our private method
        $this->doSomething();
        
        // Display all news
    }

    /**
     * Can be accessed via http://mypage.com/news/view/123
     * or via http://mypage.com/news/news/view/123
     */
    public function view($id)
    {
        // Display news with ID = $id
    }

    /** 
     * Can't be accessed from the outside
     */
    private function doSomething()
    {
        // Do something privately
    }
}
Clone this wiki locally