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
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
$auth
public
Auth
$auth
Instance of the Auth
class.
$config
public
Config
$config
Instance of the Config
class.
$data
public
Data
$data
Instance of the Data
class.
$database
public
Database
$database
Instance of the Database
class.
$dumper
public
Dumper
$dumper
Instance of the Dumper
class.
$event
public
Event
$event
Instance of the Event
class.
$globals
public
Globals
$globals
Instance of the Globals
class.
$html
public
HTML
$html
Instance of the HTML
class.
$misc
public
Misc
$misc
Instance of the Misc
class.
$path
public
Path
$path
Instance of the Path
class.
$router
public
Router
$router
Instance of the Router
class.
$session
public
Session
$session
Instance of the Session
class.
$view
public
View
$view
Instance of the View
class.
$model
protected
Model|null
$model
$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
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
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
Return values
bool —doAssociateModel()
Associates a model class to the controller.
private
doAssociateModel() : void
Tags
Return values
void —doRegisterRoutes()
Registers all public methods which are suffixed with `Action` or `Middleware` as `handler` or `middleware` respectively.
private
doRegisterRoutes() : void