\Piko\Controller

Controller is the base class for classes containing controller logic.

Properties summary

Name Description
public $id The controller identifier.
public $layout The name of the layout to be applied to this contr…
public $viewPath The root directory that contains view files for th…
protected $module The module that this controller belongs to.
protected $request  
protected $response  
protected $view  

Inherited Properties

Name Description
public $behaviors Behaviors container.
protected $eventDispatcher  
protected $listenerProvider  

Methods summary

Name Description
public __construct Constructor
public handle  
protected forward Forward the given route to another module
protected getView Returns the application View component
protected getViewPath Returns the directory containing view files for th…
protected init Method called at the end of the constructor. This …
protected isAjax Check if the request is AJAX
protected jsonResponse Convenient method to return a JSON response
protected redirect Set a response redirection
protected render Render a view.
private getMethodArguments  
private runAction Runs an action within this controller with the spe…

Inherited Methods

Name Description
public __call Magic method to call a behavior.
public attachBehavior Attach a behavior to the class instance.
public detachBehavior Detach a behavior.
public on  
public trigger Trigger an event that may be listen by event liste…

Properties

public $id : string

The controller identifier.

public $layout : null|string|false

The name of the layout to be applied to this controller’s views. This property mainly affects the behavior of render(). Defaults to null, meaning the actual layout value should inherit that from module’s layout value. If false, no layout will be applied.

public $viewPath : string

The root directory that contains view files for this controller.

protected $module : \Piko\Module

The module that this controller belongs to.

protected $request : \Psr\Http\Message\ServerRequestInterface

protected $response : \Psr\Http\Message\ResponseInterface

protected $view : \Piko\View\ViewInterface


Methods

public __construct(): mixed

public  __construct(\Piko\Module  $module): mixed

Parameters

$module :

Return:

mixed


public handle(): \Psr\Http\Message\ResponseInterface

public  handle(\Psr\Http\Message\ServerRequestInterface  $request): \Psr\Http\Message\ResponseInterface

Parameters

$request :

Return:

\Psr\Http\Message\ResponseInterface


protected forward(): string

protected  forward(string  $route, string[]  $params = []): string

Forward the given route to another module

Parameters

$route : The route to forward

$params (default: []): An array of params (name-value pairs) associated to the route.

Return:

string


protected getView(): \Piko\View\ViewInterface|null

protected  getView(): \Piko\View\ViewInterface|null

Returns the application View component

Return:

\Piko\View\ViewInterface|null


protected getViewPath(): string

protected  getViewPath(): string

Returns the directory containing view files for this controller. The default implementation returns the directory named as controller id under the module’s viewPath directory.

Return:

string the directory containing the view files for this controller.


protected init(): void

protected  init(): void

Method called at the end of the constructor. This could be overriden in inherited classes.


protected isAjax(): bool

protected  isAjax(): bool

Check if the request is AJAX

Return:

bool


protected jsonResponse(): \Psr\Http\Message\ResponseInterface

protected  jsonResponse(mixed  $data): \Psr\Http\Message\ResponseInterface

Convenient method to return a JSON response

Parameters

$data :

Return:

\Psr\Http\Message\ResponseInterface


protected redirect(): void

protected  redirect(string  $url): void

Set a response redirection

Parameters

$url : The url to redirect


protected render(): string

protected  render(string  $viewName, array  $data = []): string

Render a view.

Parameters

$viewName : The view file name.

$data (default: []): An array of data (name-value pairs) to transmit to the view.

Return:

string


private getMethodArguments(): array

private  getMethodArguments(string  $methodName, array  $data = []): array

Parameters

$methodName : The method to analyse

$data (default: []): A key-value paired array to bind into the method arguments.

Return:

array


private runAction(): \Psr\Http\Message\ResponseInterface

private  runAction(string  $id, array  $params = []): \Psr\Http\Message\ResponseInterface

Runs an action within this controller with the specified action ID.

Parameters

$id : the ID of the action to be executed.

$params (default: []): An array of request parameters.

throws \RuntimeExceptionif the requested action ID cannot be resolved into an action successfully.

Return:

\Psr\Http\Message\ResponseInterface the result of the action.