Skip to content

Controllers

John O'Grady edited this page Mar 26, 2017 · 1 revision

Controllers

Controllers are responsible for the transfer of model data to the view. Controllers contain actions, which are basically routes that controller can load. For example, if you had a Controller User, you could have multiple actions associated with that control that relate to a user account. Actions may include, login, logout, register, settings. Here is an example Controller class:

You can now type http://yourdomain.com/new/action into your browser and the code above will run.

Note: you don't need to append the word Controller in the URL bar, all Controller's end in the word Controller so the framework is aware of this and will append automatically.

Example Controller

namespace Ecne\Controller;

class NewController extends Controller
{
    public function action()
    {
        ... perform action ...
    }
}

To run the code above, point your web browser to:

http://localhost/new/action

or

http://yourdomain.com/new/action

Default Controllers

By default, when you download the Ecne Framework, it comes with two default controllers. Index, and Error. The index controller handles actions for the index of your website, like landing page, contact, about, pages. Error will load every time a user tries to navigate to a controller that has never been created. If a user attempts to call an action that doesn't exist but the controller does, the controllers index action is called by default.

You can create a custom 404 page using the ErrorController class. Inside the view folder for the ErrorController (app/views/error/index.php), modify the index.php file with your own design for 404 errors.

Code

IndexController

namespace Ecne\Controller;

class IndexController extends Controller
{
    function __construct()
    {
        parent::__construct();
    }

    /**
      * @param $parameters|array
      */
    public function index($parameters = array())
    {
        $this->view->render('index/index', [
            'title'=>'Home',
        ]);
    }
}

ErrorController

namespace Ecne\Controller;

class ErrorController extends Controller
{

    function __construct()
    {
        parent::__construct();
    }

    /**
      * @param $parameters|array
      */
    public function index($parameters = array())
    {
        $this->view->render('error/index', [
            'title'=>'Error 404'
        ]);
    }
}

Creating a Controller

To create a controller, you need to create a controller class inside the app/controller folder. You must append the word Controller to the end of all filenames for controllers. For example, if you wanted to create a controller for a person. You would create the controller under the controllers folder, name it PersonController.php to be loaded when your browser points to http://yourwebsite/person/. Controllers must reside inside the Ecne\Controller namespace and must extend the base Controller class.

Code

namespace Ecne\Controller;

class PersonController extends Controller
{
...
}

Defining Actions

To define actions for a controller you just create methods in the controller class with the names of the actions you wish to run. If you wanted a logout action in the Person Controller, you would create a method called logout in the PersonController.

The code inside the logout function will run any time you visit http://yourdomain/person/logout

Code

namespace Ecne\Controller;

class PersonController extends Controller
{
    public function logout()
    {
        session_destroy();
    }
}