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:
| Property | Description |
|---|---|
queries | Query string that available in the url. |
url | Details of url. |
headers | Get http headers. |
keys | placeholder/named parameter of the route. |
data | data passed in request body. |
records | hold 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
"/your-route" => function($callbackValues){
$callbackValues::getData(string $key);
}key
String as dot notation (e.g: data.name.last)
get all callback values
"/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:
{
"email": "johndoe@domain.com",
"birthdate": "1990-07-09",
"name": {
"firstname": "John",
"lastname": "Doe"
}
}the details of $callbackValues will be:
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
// 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
}
));
}
}