VELOX API Docs

Controller
in package

An abstract class that serves as a base Controller that can be extended to make handlers for application router.

Example:

// create a controller (alternatively, you can create it as a normal class in "/app/Controller/")
$additionalVars = [1, 2, 3];
$controller = new class($additionalVars) extends Controller {
     public function someAction(string $path, ?string $match, $previous) {
         $this->data->set('page.title', 'Some Page');
         $someVar = $this->config->get('filename.someVar');
         return $this->view->render('some-page', $this->vars);
     }
};

// use the created action as a handler for a route
Router::handle('/some-route', [$controller, 'someAction'], ['GET', 'POST']);
Tags
since
1.0.0

Table of Contents

ON_CONSTRUCT  = 'controller.on.construct'
This event will be dispatched when a controller (or a subclass) is constructed.
$auth  : Auth
$config  : Config
$data  : Data
$database  : Database
$dumper  : Dumper
$event  : Event
$globals  : Globals
$html  : HTML
$misc  : Misc
$path  : Path
$router  : Router
$session  : Session
$view  : View
$model  : Model|null
$vars  : array<string|int, mixed>
The passed variables array to the Controller.
$crudRoutes  : array<string|int, mixed>
Preconfigured CRUD routes.
__construct()  : mixed
Class constructor.
__get()  : mixed
__isset()  : mixed
associateModel()  : string
Controls which model should be used by current controller.
registerRoutes()  : bool
Whether or not to automatically register controller routes.
doAssociateModel()  : void
Associates a model class to the controller.
doRegisterRoutes()  : void
Registers all public methods which are suffixed with `Action` or `Middleware` as `handler` or `middleware` respectively.

Constants

ON_CONSTRUCT

This event will be dispatched when a controller (or a subclass) is constructed.

public string ON_CONSTRUCT = 'controller.on.construct'

This event will not be passed any arguments, but its listener callback will be bound to the object (the controller class).

Properties

$vars

The passed variables array to the Controller.

protected array<string|int, mixed> $vars

$crudRoutes

Preconfigured CRUD routes.

private array<string|int, mixed> $crudRoutes = ['index' => ['expression' => '/{controller}', 'method' => 'GET'], 'create' => ['expression' => '/{controller}/create', 'method' => 'GET'], 'store' => ['expression' => '/{controller}', 'method' => 'POST'], 'show' => ['expression' => '/{controller}/([1-9][0-9]*)', 'method' => 'GET'], 'edit' => ['expression' => '/{controller}/([1-9][0-9]*)/edit', 'method' => 'GET'], 'update' => ['expression' => '/{controller}/([1-9][0-9]*)', 'method' => ['PUT', 'PATCH']], 'destroy' => ['expression' => '/{controller}/([1-9][0-9]*)', 'method' => 'DELETE']]
Tags
since
1.3.0

Methods

__construct()

Class constructor.

public __construct([array<string|int, mixed> $vars = [] ]) : mixed
Parameters
$vars : array<string|int, mixed> = []

[optional] Additional variables to pass to the controller.

Return values
mixed

__get()

public __get(string $property) : mixed
Parameters
$property : string
Return values
mixed

__isset()

public __isset(string $property) : mixed
Parameters
$property : string
Return values
mixed

associateModel()

Controls which model should be used by current controller.

protected associateModel() : string

This method should return a concrete class FQN of a model that extends Model::class.

This method returns null by default.

NOTE: If the model class does not exist, the controller will ignore it silently.

Tags
since
1.3.0
Return values
string

registerRoutes()

Whether or not to automatically register controller routes.

protected registerRoutes() : bool

NOTE: The controller class has to be instantiated at least once for this to work.

Only public methods suffixed with the word Action or Middleware will be registered. The suffix will determine the route type (*Action => handler, *Middleware => middleware). The route will look like /controller-name/method-name (names will be converted to slugs). The method will be GET by default. See also self::$crudRoutes. You can use the @route annotation to overrides the default Route and Method. The @route annotation can be used in DocBlock on a class method with the following syntax:

  • Pattern: @route("<path>", {<http-verb>, ...})
  • Example: @route("/some-route", {GET, POST})

This method returns false by default.

Tags
since
1.3.0
Return values
bool

doAssociateModel()

Associates a model class to the controller.

private doAssociateModel() : void
Tags
since
1.3.0
Return values
void

doRegisterRoutes()

Registers all public methods which are suffixed with `Action` or `Middleware` as `handler` or `middleware` respectively.

private doRegisterRoutes() : void
Tags
since
1.3.0
Return values
void

Search results