Controllers
Required Namespace
  • Ness
    • Controller

You can include this library to your code file by typing the below code;

use Ness\Controller


About Controllers #top

A controller is a class which responds to http requests. All your controller class files must be located in controller folder and must contain a Controller suffix. You can read more about directory structure Here.


Default Controller #top

By default in controller folder there is a indexController.php file. This is the default controller class for Ness PHP. The core framework file (Ness\system\boot.php) looks in controller folder and search's indexController.php file with a class named indexController and public function IndexAction if not specified in url.


Example Controller #top


class AuthController extends Ness\Controller
{

    public function indexAction($param=0)
    {
            $this->View->Render("startpage.php");
    }
}
Save this file to project_name\Application\Controller\AuthController .php Now when you navigate to : http://localhost/project_name/auth/index or http://localhost/project_name/auth you will see the startpage.php view run.
Controller File Names: Class name must be same with the file name in controller file. In this example you can see that Controller/AuthController.php has the class AuthController.

Action Suffix: All functions considered to use as action in controller must be public and must have the Action suffix. For example in the code above you can see that index action is called indexAction (but in url is only; index) and it is public function.


Owning & Versioning a Controller #top

If your team works on a big project you can use some commands to own and version the controller. This will make easy to the project leader to track which developer is developed the controller/area and whats is the version number of the controller.

$this->setVersion(integer $prm);
Call this method in __construct for setting a version number to your controller
Return void
Parameters $prm integer; Version Number.

You can use the command below for getting the version number of controller.

$this->getVersion();
Return Integer
Parameters None


For using version information of your controller in another controller class just create an instance of controller and call getVersion() function.


For setting an author for your controller.
$this->setAuthor(string $prm);
Call this method in __construct for setting an author to your controller
Return void
Parameters $prm string; Author name.


You can use the command below for getting the author name;
$this->getAuthor();
Call this method for getting the author name
Return String
Parameters None


Methods #top

Public Methods

If you want to declare a action result to controller your function must have the Action suffix.Other functions not considered to be used as actions, can be defined as private methods.




Private Methods

Private methods can be defined and used in controllers.


public function ResetAction()
{
    if($this->check())
    {
        $this->View->Render("Auth/Reset_Password.phtml");
    }
    echo "Hello World";
}
private function check(&$param)
{
    if(!$param== 1){
        return false;
    }else {return true;}
}
                                 




Constructor

You can define your own constructors in controllers, here is an example.


function __construct()
{
    parent::__construct();
    //other constructor code here;
    //load libraries;
    //etc;
}