\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 $module The module that this controller belongs to.
public $viewPath The root directory that contains view files for th…
protected $request  
protected $response  
protected $view  

Inherited Properties

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

Methods summary

Name Description
public getUrl Converts a given modular route into its correspond…
public handle  
protected create Create an object and resolve constructor dependenc…
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 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 Registers an event listener.
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 $module : \Piko\Module

The module that this controller belongs to.

public $viewPath : string

The root directory that contains view files for this controller.

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

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

protected $view : \Piko\View\ViewInterface


Methods

public getUrl(): string

public  getUrl(string  $route, string[]  $params = [], bool  $absolute = false): string

Converts a given modular route into its corresponding URL. This method is useful for generating URLs dynamically, based on the specified route and optional parameters. The route should follow the format: {moduleId}/{ControllerId}/{ActionId}.

Parameters

$route : The modular route in the format {moduleId}/{ControllerId}/{ActionId}. This string determines which module, controller, and action to target.

$params (default: []): An optional associative array of query parameters to append to the generated URL. Each key-value pair represents a parameter name and its corresponding value.

$absolute (default: false): An optional boolean flag indicating whether the generated URL should be absolute (including protocol and host) or relative. Defaults to false, meaning a relative URL will be returned.

Return:

string The resulting URL that corresponds to the provided route and parameters.


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 create(): object

protected  create(class-string  $class, array<string,mixed>  $overrides = []): object

Create an object and resolve constructor dependencies from application components.

Parameters

$class :

$overrides (default: []): Constructor argument overrides indexed by parameter name.

Return:

object


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 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(): \Psr\Http\Message\ResponseInterface

protected  redirect(string  $url): \Psr\Http\Message\ResponseInterface

Set a response redirection

Parameters

$url : The url to redirect

Return:

\Psr\Http\Message\ResponseInterface


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.