Crater PHP Framework

fast, lightweight and modular

Controllers

Controllers are the 'heart' of the framework they control when a model is used and equally when to include a view for output. A controller is a class with methods, these methods are the outputted pages when used in conjunction with routes.

A method can be used for telling a view to show a page or outputting a data stream such as XML or a JSON array. Put simply they are the logic behind your application. More about MVC/Controller can be read here: Wikipedia

Controllers are created inside the controllers folder.
To create a controller, create a new file, the convention is to keep the filename in "uppercase first letter" without any special characters or spaces.
Controllers will always use a namespace of controllers, if the file is directly located inside the Controllers folder. If the file is in another folder that folder name should be part of the name space.
For instance a controller called Product located in Controllers/Product would have a namespace of Controllers/Product
Controllers need to use the main core Controller; they extend it, the syntax is:

namespace Controllers;
use Core\Controller;

class Product extends Controller
{

    public function viewAction()
    {

    }
}

Controllers will need to access methods and properties located in the parent controller (Core/Controller.php) in order to do this they need to call the parent constructor inside a construct method.

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

The construct method is called automatically when the controller is instantiated once called the controller can then call any property or method in the parent controller that is set as public or protected.

Actions

Declaring an action is as follows:

public function helloAction()
{

}

Methods

In controller and in action, you can call many methods. In the example below we have a good part of them.

public function helloAction()
{
    $controllerName = $this->getControllerName();
    $this->setControllerName('product');

    $cookie = $this->getCookie()


    if ($this->isPost()) {
        echo 'request made by post';
    }

    if ($this->isGet()) {
        echo 'request made by get';
    }

    $postParameters = $this->getPost();
    $userPostParameter = $this->getPost('user');

    $getParameters = $this->getParam();
    $userGetParameter = $this->getParam('user');

    $server = $this->getServer();
    $clientIP = $this->getClientIp();

    // Redirect
    $this->redirect("http://google.com");

}

For more about supported methods please read API Documentation