First Controller

The Controller ๐ŸŽฎ

Up and Running ๐Ÿ‘Ÿ

Every route you specify in your application will end in the static method of a controller. Controller are PHP classes that are derived from the Core/Controller base class.

The general naming format enforced by FMVC is <name>Controller. This type of naming will be expected by the routing logic and serves to identify associated views and assets that should be included automatically.

This will look something like this:

#Controllers/MyController.php
use Core\Controller;
class MyController extends Controller {
# Do your magic!
}

Now, after you got your fancy new controller, we need to populate it with some methods. These methods have to be public, static and need to get a reference on a Core/Request class instance. Like this:

#Controllers/MyController.php
use Core\Controller;
class MyController extends Controller {
public function handler(&$request) {
return $this->Ok(new PageView(
$request,
'Profile',
$request->session['user'])
);
}
}

To easily handle common HTTP response types, the Controller baseclass utilizes a module of quick actions that prepare the response and render the given input as string. One of these methods can be seen in the above example rendering a view and returning it with the HTTP status Ok.

The parameter passed is an instance of the class PageView.

Next to Ok there are also the following methods available:

  • Created
  • Accepted
  • NoContent
  • BadRequest
  • NotFound
  • Teapot

All of the examples mentioned above can receive an implementation of the serializeable interface as parameter or nothing if there's nothing to render.

The return value of the controller method registered to handle the method will be printed onto the resulting document. Just as you are used from standard PHP applications. So if you want to do your own thing, feel free to just return a string. ๐Ÿ˜