Advertisement

Responsive Advertisement

Creating a Controller in RibbonPHP

What is the Controller? In PHP MVC, a controller is a component responsible for intercepting the request from view and passes to the model for appropriate action. After the action has been taken on the data, the controller is responsible for directly passes the appropriate view to the user.


In RibbonPHP, you can think a controller as the handler of the route. A route is a combination of an HTTP method, a path, and the function to handle it defined in an application. They help determine which controllers receive certain requests. A controller is a class defined with methods for handling one or more requests.

Here is an example:

Sample path: https://myapp.com/login

In the URL above, the path is login. There must be a controller class named Login in the ribbon/controllers directory which extends the class Controller. So, to create the controller Login, go to ribbon/controllers directory and create a PHP file and name the file login.php. Then, write the following code in that file:


<?php

if ($_SERVER['REQUEST_METHOD'] == 'GET' && realpath(__FILE__) == realpath($_SERVER['SCRIPT_FILENAME'])) {
    header('HTTP:/1.0 403 Forbidden', TRUE, 403);
    die("<h2>Access Denied</h2> <p>This file is protected!</p>");
}

class Login extends Controller{

    public function index()
    {
        $header['title'] = 'Title of the Page';
        $this->loadHeader('header', $header);
        $this->view('login/index');
        $this->loadFooter('footer');
    }
}

As you can see, there is a a class Login that extends the class Controller. Before the class is the code to prevent direct access to the file. Then, there is a method index() which tells that this is the index page of the controller login. Some controller does not require the method index().

In the sample above, the method index() calls several function like loadHeader(), view(), and loadFooter(). These functions work to "require" the frontend. For example, the loadHeader() takes the header.php (just write the file name without the extension). It also works like loadFooter() and view().

If there is data to pass to the required file, they must be defined before the requiring. Then, the data must be passed as an argument in the function. In the example above, the $header['title'] is a variable that stores the title of the page. Then, in calling loadHeader(), the variable must be passed as an argument.

loadHeader('header-file-name', $header); view('view-directory-name/view-file-name', $data); loadFooter('footer-file-name', $footer);

$header, $data, and $footer are in arrays. So, it should not be $data = ... but $data['something'].

The footer and header files can be seen in ribbon/templates directory and the view can be seen in the ribbon/views directory.

A controller also works to handle input from forms. For example, a form in the frontend has action attribute to http://myapp.com/login/user_login, there must be a method within the login class named user_login which may seem like this:

public function user_login(){
        if(isset($_POST['user_login'])){
            $accountData = $this->sanitize($_POST);
            $this->model('Account_model')->userLogin($accountData);
        }
    }

As you can see, the method user_login() evaluates if there is an input $_POST with name user_login (this is usually set as the submit button name). If yes, then it takes all $_POST values (e.g., username and password) and stores them to a variable named $accountData. The data stored in this variable are firstly sanitized using sanitize() function. This is super important to avoid SQL Injection or any other hacking attack like XSS or CSRF. Then, the data is sent to a model (e.g., Account_model) which is in the ribbon/models directory (the model file name must be similar to the class name inside the file, in this case is Account_model.php, class Account_model. There is a method inside the Account_model class named userLogin() that accepts one argument, the $accountData. Of course, the number of arguments depends on your need.

More about database connection in a model, please see about how to create a model.

Post a Comment

0 Comments