Skip to content

Callback values

$callbackValues (you may rename this to any variable name you prefer) represents the return value produced by each route callback. This value is what your controller logic generates and can be used for further processing, response formatting etc.

Callback values consist of:

PropertyDescription
queriesQuery string that available in the url.
urlDetails of url.
headersGet http headers.
keysplaceholder/named parameter of the route.
datadata passed in request body.
recordshold result of data fetched from model.

ℹ️ NOTE

The default of headers is all headers containing "HTTP" key. You can customize what headers to get by modifying it in config/app.php in the header section.

💡 Usage

callback values of each route & get the data

php
"/your-route" => function($callbackValues){
  $callbackValues::getData(string $key);
}

key

  String as dot notation (e.g: data.name.last)

get all callback values

php
"/your-route" => function($callbackValues){
  $callbackValues::all();
}

Example:

A route pattern /firstname/:first/lastname/:last with POST request & query string, its url is yourdomain.com/examples/firstname/john/lastname/doe?status=active and headers configuration in config/app.php is HTTP and the form data is:

json
{
  "email": "johndoe@domain.com",
  "birthdate": "1990-07-09",
  "name": {
    "firstname": "John",
    "lastname": "Doe"
  }
}

the details of $callbackValues will be:

php
Array
  (
    [queries] => Array
        (
            [status] => active
        )
    [url] => Array
        (
            [controller] => examples
            [endpoint] => /examples/firstname/john/lastname/doe
            [base] => /
            [method] => GET
            [url] => http://yourdomain.com/examples/firstname/john/lastname/doe
            [path] => /examples/firstname/john/lastname/doe
        )
    [headers] => Array
        (
            [HTTP_CACHE_CONTROL] => no-cache
            [HTTP_HOST] => yourdomain
            [HTTP_ACCEPT_ENCODING] => gzip, deflate, br
            [HTTP_CONNECTION] => keep-alive
        )
    [keys] => Array
        (
            [first] => john
            [last] => doe
        )
    [data] => Array
        (
            [email] => johndoe@domain.com
            [birthdate] => 1990-07-09
            [name] => [
                [firstname] => John
                [lastname] => Doe
            ]
        )
    [records] => Array()
  )

ℹ️ NOTE

Default structure of records value from fetched data is explained in model

Example:

From URL above

php
// controller/Examples.php

namespace Controller
use Core\Controller

class Examples extends Controller{
  public function post(){
    $this->map(array(
      "/firstname/:first/lastname/:last" => function( $callbackValues ) {
        // get status query string
        $callbackValues::getData("queries.status") // output: active

        // get named parameters
        $callbackValues::getData("keys.x") // output: john

        // get last name of post data
        $callbackValues::getData("data.name.last") // output: Doe
      }
    ));
  }
}