validToken() validates a submitted security token.
Purpose
validToken() validates a submitted security token. It lets a controller customize Aksara Core behavior while keeping the request inside the built-in CRUD, rendering, permission, validation, and response pipeline.
When to Use
Use it near the beginning of a controller method to configure how Core handles the current request.
Reference
validToken(?string $token, string|array $allowedUris = [])
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
$token | ?string | Yes | - | Submitted token value. |
$allowedUris | string|array | No | [] | Additional URI or URI list whose token may be accepted. |
Return Value
bool
Returns true when the submitted token is accepted for the current route or allowed URI; otherwise returns false.
Behavior
validToken() stores request-level configuration on the controller. Call it before the permission, rendering, or form-processing step that depends on it.
[!CAUTION]
Do not bypass token validation for browser form submissions. API clients have their own token path, but normal POST requests should keep CSRF protection enabled.
Basic Usage
if (! $this->validToken($this->request->getPost('_token'), ['orders/create'])) {
return throw_exception(403, phrase('The security token is invalid or expired.'));
}
Advanced Usage
$this->setTitle(phrase('Orders'))
->setIcon('mdi mdi-cart-outline')
->setPermission();
Complete Example
namespace Modules\Orders\Controllers;
use Aksara\Laboratory\Core;
class Orders extends Core
{
public function index()
{
$this->setTitle(phrase('Orders'))
->validToken();
return $this->render('orders');
}
}
Result
The controller stores the configuration and applies it later in the current request lifecycle.
Notes
- Call configuration methods before
setPermission()orrender()when those steps depend on the configured value.
Common Mistakes
- Calling the method after the permission or render step that already needed it.
- Spreading related configuration across distant parts of the controller.
