Framework tools
Page
<?php
$page = App::page();
The page method allows retrieving the current page object from anywhere.
<?php
$route = App::getRoute();
The getRoute method allows retrieving the current (cleaned) route name from anywhere (e.g. /contact.html).
/my-page.html = /my-page.html
/my-page/ = /my-page
/my-page/?q=1 = /my-page
/my-page?q=1 = /my-page
Encryption
<?php
$var = App::encryption()->crypt('message');
echo App::encryption()->decrypt($var);
The encryption key is stored in var/encryption (read-only 0400 file). If the key is lost, it will be impossible to decrypt the data ever again.
Pass true as the second argument to use a fixed initialization vector (same input always produces the same output). Only do this for values that are guaranteed unique.
<?php
$token = App::encryption()->crypt($uuid, true);
Cache
<?php
App::cache()->set('foo', 'bar');
echo App::cache()->get('foo');
The cache file is stored in var/cache. The default cache lifetime is 86400 seconds. Update it before assignment:
<?php
App::cache()->setLifetime(3600)->set('foo', 'bar');
Only scalar values and arrays can be cached. For security reasons, objects (enums and closures included) are rejected with an
InvalidArgumentException, even inside an array. Convert them to an array before caching (e.g.$object->toArray()).
| Method | Description |
|---|---|
| set | Store one value |
| bulk | Store several values at once (array) |
| get | Read one value (null if missing or expired) |
| all | Read every cached value |
| setLifetime / getLifetime | Lifetime in seconds (0 disables caching) |
| cleanByKey | Remove one key if it is expired |
| cleanExpired | Remove every expired key |
| cleanAll | Empty the cache |
Session
<?php
App::session()?->set('foo', 'bar');
echo App::session()?->get('foo');
Sessions are stored in var/session. The session cookie is named after the package and its lifetime, SameSite and HttpOnly flags come from the configuration (see Configuration).
| Method | Description |
|---|---|
| set | Store one value, or several with an array |
| get | Read one value (all values with no argument) |
| pull | Read a value and remove it |
| has | Check a value exists |
| id / regenerate | Current session id / regenerate it |
| destroy | Destroy the session and clear the cookie |
App::session(false) returns the session only if one already exists, without starting a new one.
Login and logout
Always regenerate the session id when a user logs in (or when their privileges change).
<?php
// Login
App::session()?->regenerate();
App::session()?->set('user', $user);
// Logout
App::session()?->destroy();
Log
<?php
App::log('message');
Logs are stored in var/log/app.log. A Throwable, an array or an object is formatted automatically.
The second parameter sets the level (default Logger::INFO):
<?php
App::log('message', Logger::WARN);
Logger::EMERG; // Emergency: system is unusable
Logger::ALERT; // Alert: action must be taken immediately
Logger::CRIT; // Critical: critical conditions
Logger::ERR; // Error: error conditions
Logger::WARN; // Warning: warning conditions
Logger::NOTICE; // Notice: normal but significant condition
Logger::INFO; // Informational: informational messages
Logger::DEBUG; // Debug: debug messages
App::getLogger() returns the Logger instance. Use trim() to cap the file size:
<?php
App::getLogger()?->trim(1000); // keep only the last 1000 lines
Database
<?php
$id = App::db()->insert('table', ['name' => 'John Doe']);
App::db()->query('UPDATE table SET name = ? WHERE id = ?', ["Jane Doe", 1]);
$result = App::db()->query('SELECT * FROM table')->fetchAll();
$result = App::db()->query('SELECT * FROM table WHERE id = ?', [1])->fetch();
Config
<?php
echo App::getConfig('app.session_lifetime');
The second argument is the value returned when the path does not exist:
<?php
$lifetime = App::getConfig('app.session_lifetime', 3600);
Escaper
<?php
App::escapeHtml('<a href="#">Escaped HTML</a>');
App::escapeHtmlAttr('my-class');
App::escapeUrl('https://www.example.com');
App::escaper()->escapeQuotes("that's true");
<a href="#" onclick="alert('<?= App::escaper()->escapeQuotes("that's true") ?>')">Alert</a>
Search
<?php
$indexes = [
1 => 'Phillip J. Fry',
2 => 'Leela Turanga',
3 => 'Bender Bending Rodriguez',
4 => 'Capitaine Zapp Brannigan',
5 => 'Amy Wong',
];
$search = new Search();
$result = $search->search('leilla captain', $indexes);
array(2) {
[2]=> string(13) "Leela Turanga"
[4]=> string(24) "Capitaine Zapp Brannigan"
}
The constructor tunes the matching: new Search($minChar, $minDistance) — minimum word length (default 3) and maximum Levenshtein distance ratio for a fuzzy match (default 0.4, lower is stricter).
<?php
$search = new Search(3, 0.2); // stricter matching
- Installation
- Configuration
- Add a new package
- Add a new HTML page
- Templating best practices
- Add a new block
- Assets
- Serve any type of file
- Rewrite a route
- Data assignment
- Objects and class fallback
- Models
- Database
- Forms
- Session messages
- Send emails
- Captcha
- Resize and compress images
- Framework tools
- Console Commands
- Hooks
- Custom shared libraries
- External libraries with composer
- Write content in Markdown
- Static Site Generator