Overview

Namespaces

  • Core
    • Auth
    • Exceptions
    • Helpers
    • Providers
  • Kernel
    • Dispatch
    • Providers
  • None
  • Vendor
    • ExampleVendor

Classes

  • AbstractClass
  • ErrorController
  • Language
  • ParentController
  • ParentModel
  • Overview
  • Namespace
  • Class
  • Download

Class ParentController

This is the parent class for all controllers in /src/controllers, and indirectly for all controllers that extend VendorController (which, in turn, extend this).
ParentController is the class that manages assets, paths, $_GET and $_POST, views, etc.

Direct known subclasses

Controller, Core\Auth\AuthenticateController, Core\ErrorController, Core\Exceptions\Exception, Vendor\VendorController

Indirect known subclasses

ErrorController, HomeController, LoginController, Vendor\ExampleVendor\ExampleVendorController
Namespace: Core
Author: Nico Kupfer nico@kupfer.es
Hooks:
('include_show_view', ['vars' => $this->vars]) // Called just before including the view file \Core\ParentController::show
('include_get_view', ['vars' => $_vars]) // Called just before including a view file  \Core\ParentController::get
('extra_params_path', ['route' => $route]) // If extra parameters are required when creating a url \Core\ParentController::path

Located at app/controllers/ParentController.php
Methods summary
public
# __construct( )

Every Controller that extends ParentController must explicitly

Every Controller that extends ParentController must explicitly

public static Core\ParentController
# singleton( )

Returns a ParentController instance, creating it if it did not exist.

Returns a ParentController instance, creating it if it did not exist.

Returns

Core\ParentController
public Core\ParentModel
# getModel( )

Returns the instance of the model for this controller

Returns the instance of the model for this controller

Returns

Core\ParentModel
public mixed
# getGet( $key, $fallback = NULL )

Filters an input (GET) variable and returns it. If key doesn't exist or the value equals NULL, returns $fallback

Filters an input (GET) variable and returns it. If key doesn't exist or the value equals NULL, returns $fallback

Returns

mixed
$_GET[$key] or $fallback

Var

string $key The key
mixed $fallback if not null, would be taken as the default value. Otw the parameter is required.
public mixed
# getPost( $key, $fallback = NULL )

Filters an input (POST) variable and returns it. If key doesn't exist or the value equals NULL, returns $fallback

Filters an input (POST) variable and returns it. If key doesn't exist or the value equals NULL, returns $fallback

Returns

mixed
$_POST[$key] or $fallback

Var

string $key The key
mixed $fallback if not null, would be taken as the default value. Otw the parameter is required.
public
# show( string $view, string $template = 'default' )

Shows a view inside a specified template Remember that both $view and $template can be absolute or relative paths

Shows a view inside a specified template Remember that both $view and $template can be absolute or relative paths

Parameters

$view
The path to the file, being '/' the 'views' directory
$template
The name of the template

Throws

Exception
specifying the type of missing view.
public string
# get( string $view, array $_vars = array(), string $template = '' )

Renders a view and returns it

Renders a view and returns it

Parameters

$view
The path to the file, being '/' the 'views' directory
$_vars
The $vars to include in the view
$template
The template name

Returns

string
The requested view

Throws

Exception
specifying the type of missing view.
public string
# asset( string $path = '', boolean $return = FALSE )

Returns the web path for a given URL

Returns the web path for a given URL

Parameters

$path
$return

Returns

string
$webPath
public mixed
# path( string $path = '', array $params = array(), boolean $return = FALSE )

Returns the web path for a given Route name

Returns the web path for a given Route name

Parameters

$path
The path identifier ('Home', 'Login', ...)
$params
The parameters of the URL (':id', ':name', ...)
$return
= FALSE. Whether to return or echo

Returns

mixed
If $return = FALSE, the path. NULL otherwise

Throws

Exception
If $path does not exist in the routing.ini
public string
# url( string $path, array $params = array() )

Alias for ::path with $return = TRUE. Use it for controllers

Alias for ::path with $return = TRUE. Use it for controllers

Parameters

$path
The path identifier ('Home', 'Login', ...)
$params
The parameters of the URL (':id', ':name', ...)

Returns

string
The formatted URL
public object
# redirect( string $path = '' )

Performs a header redirection and a die();

Performs a header redirection and a die();

Parameters

$path

Returns

object
A JSON object with redirect instructions (if it's an ajax call) or a Location header.
public Core\ParentController
# add( string $varName, mixed $value )

Adds a var to the $this->show() renderer.

Adds a var to the $this->show() renderer.

Parameters

$varName
$value

Returns

Core\ParentController
public Core\ParentController
# addJSVar( string $var, mixed $value, boolean $encoded = TRUE )

Adds a JS variable to the $this->show() renderer.

Adds a JS variable to the $this->show() renderer.

Parameters

$var
$value
$encoded

Returns

Core\ParentController
public Core\ParentController
# addStyle( string $path, boolean|true $local = TRUE, boolean|true $minimize = TRUE )

Adds a script snippet to the $this->show() renderer.

Adds a script snippet to the $this->show() renderer.

Parameters

$path
The path to the asset (local or absolute)
$local
whether it's a relative (local) or absolute URL
$minimize
Use Minify library for the asset

Returns

Core\ParentController
public Core\ParentController
# addScript( string $path, boolean|true $local = TRUE, boolean|true $minimize = TRUE )

Adds a style snippet to the $this->show() renderer.

Adds a style snippet to the $this->show() renderer.

Parameters

$path
The path to the asset (local or absolute)
$local
whether it's a relative (local) or absolute URL
$minimize
Use Minify library for the asset

Returns

Core\ParentController
public
# minimize( array $files, string $type = 'js' )

Adds a script with the minified URL of the $files

Adds a script with the minified URL of the $files

Parameters

$files
the files to add to the minified asset
$type
The type of minimized files
public string
# minimizeURL( array $files = [], string $type = 'js' )

Get the minified URL for the assets. This does NOT minimize files, just provide the minified url.

Get the minified URL for the assets. This does NOT minimize files, just provide the minified url.

Parameters

$files
The assets that you want to minify
$type
The type of minimized files

Returns

string
public Core\ParentController
# addHeader( array $vars = [], string|boolean $headerLocation = FALSE )

Adds the $_header variable to the view

Adds the $_header variable to the view

Parameters

$vars
$vars|[] The vars to pass to the header
$headerLocation
an (optional) alternative route for the header

Returns

Core\ParentController
public Core\ParentController
# addFooter( array $vars = [], string|boolean $footerLocation = FALSE )

Adds the $_footer variable to the view

Adds the $_footer variable to the view

Parameters

$vars
$vars|[] The vars to pass to the footer
$footerLocation
an (optional) alternative route for the footer

Returns

Core\ParentController
public Core\ParentController
# setTitle( string $title )

Sets a title for the website (different from the default one)

Sets a title for the website (different from the default one)

Parameters

$title

Returns

Core\ParentController
public Core\ParentController
# setFavicon( string $favicon )

Sets a favicon for the website (different from the default one)

Sets a favicon for the website (different from the default one)

Parameters

$favicon
Route to the favicon asset

Returns

Core\ParentController
public string
# json( mixed $obj, integer $flags = 0 )

Returns $obj in a JSON format

Returns $obj in a JSON format

Parameters

$obj
$flags
Flags to parse to json_encode

Returns

string
JSON_encoded $obj

Usage

This function transforms a single-level array into a 2+ level array for the response

['success' => true, 'message' => 'Saved!', 'foo' => 'bar']
['message' => 'Saved!', 'foo' => 'bar']

Get transformed into

['success' => 1 // true is always default, and the value gets cast to integer (int)
 'responseData' => [
     'message' => 'Saved!', // by default is empty
     'content' => ''        // by default is empty
     'foo' => 'bar' // custom
]]

You are free to add as many variables as you like. They'll be added under 'responseData' key.


public string
# cache( string $path, array $vars = [] )

Gets the cache content for a given path

Gets the cache content for a given path

Parameters

$path
$vars
Parameters to which render the file

Returns

string
rendered file

Throws

Exception
When the $path does not correspond (remember, there's a / -> _ conversion) to a chached file
public
# compressWithMinify( )

Compresses $this->vars['script'] or $this->vars['style'] into a unique minified asset

Compresses $this->vars['script'] or $this->vars['style'] into a unique minified asset

public Core\ParentController
# header( integer $code = 200 )

Outputs the correspondent header for the given HTTP code, on the form header('HTTP/1.0 404 Not Found'); See description of the status codes in the provided url

Outputs the correspondent header for the given HTTP code, on the form header('HTTP/1.0 404 Not Found'); See description of the status codes in the provided url

Parameters

$code
= 200 The status code

Returns

Core\ParentController

Url

http://www.w3.org/Protocols/HTTP/1.0/spec.html#Status-Codes
Properties summary
protected string $path

The path where the framework is located: <framework_url>/<path>/app.php.

The path where the framework is located: <framework_url>/<path>/app.php.

#
protected Core\Providers\Config $config

The instance of the Config class.

The instance of the Config class.

#
protected array $vars

The required variables to show a view.

The required variables to show a view.

See

ParentController::show
#
protected boolean $ajax

If the request is an AJAX call. Set automatically by reading $_SERVER['HTTP_X_REQUESTED_WITH']

If the request is an AJAX call. Set automatically by reading $_SERVER['HTTP_X_REQUESTED_WITH']

#
protected Core\ParentModel $model

The instance of ParentModel.

The instance of ParentModel.

#
protected string $viewPath

Default path for files. For example, {view} = "home/index".

Default path for files. For example, {view} = "home/index".

#
protected string $templatePath

Default path for templates. For example, {template} = "default" or "email".

Default path for templates. For example, {template} = "default" or "email".

#
protected Core\Hooks $hooks

The instance of the Hooks class.

The instance of the Hooks class.

#
Ribosome API documentation generated by ApiGen