Controllers


Controllers are important entities in a framework, as most transactions are done in them. So knowing about Controller is very necessary to do website development using MVC concept. Writing on progress

Controllers can also be likened to a bridge between users and information providers. As print media with readers would require sellers of newspapers, magazines, and etc.

As you already know in the flow chart section of the Introductions page. Controllers are among views and models which means that controllers are an important part of transacting data between users and services. following example create controller with Artisan Harmony:

> php artisan make:controller HomeController # and enter

in this example it is assumed that we have created a controller named HomeController, if you look at app/Controllers folder then you will find the HomeController.php file created by the artisan command.

<?php

namespace App\Controllers;

use Slim\Views\Twig as View;

class HomeController extends Controller
{
    /**
     * Method INDEX
     *
     * Ini adalah default dari HomeController
     * @param [object] $request
     * @param [object] $response
     * @return object
     * 
     * Rendering view sesuai routes request
     * routes/
     * |__ web.php
     * 
     * $app->get('/controller/method/params', 'HomeController:index')
     */

     # protected $property = value|blank;

    public function index($request, $response)
    {
        /**
         * Anda bisa menyesuaikan view yang diiginkan dengan menggunakan
         * Psuedo Variabel [ $this ] yang mewakili container atau
         * wadah dari MVC Application ini.
         * 
         * ***********************************************************
         * 
         *      $this->view->render($response, 'page.twig');         *
         * 
         * ***********************************************************
         */

         # code..
    }
}

if you want to display the template in the controller you can use the command line that already exists in the example above:

$this->view->render($response, 'page.twig');

if you delete all comments on HomeController then it will look like this:

<?php

namespace App\Controllers;

use Slim\Views\Twig as View;

class HomeController extends Controller
{
    public function index($request, $response)
    {
        $this->view->render($response, 'page.twig');
    }
}

Here's the Controller's understanding of the Harmony Framework. read the next section to find out the notion of Routes in the Harmony Framework.


Previous Next
Betta Dev Indonesia .Ltd - © All Right Reserved 2019