Skip to content

Commit 6bd8899

Browse files
committed
updating docs for 5.11
1 parent b6795dc commit 6bd8899

6 files changed

Lines changed: 408 additions & 6 deletions

File tree

docs/controllers.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,26 @@ class InvoicesController extends Controller
310310
The above parameters will match the route the way it was defined.
311311

312312
## Events
313-
Controllers automatically act as listeners for [dispatcher][dispatcher] [events][events], implementing methods with those event names allowing you to implement hook points before/after the actions are executed:
313+
Controllers automatically act as listeners for [dispatcher][dispatcher] [events][events], implementing methods with those event names allowing you to implement hook points before/after the actions are executed.
314+
315+
Controllers also implement `Phalcon\Events\EventsAwareInterface`, which means you can attach an events manager directly to any controller instance using `setEventsManager()` and retrieve it with `getEventsManager()`:
316+
317+
```php
318+
<?php
319+
320+
use Phalcon\Events\Manager;
321+
use Phalcon\Mvc\Controller;
322+
323+
$controller = new InvoicesController();
324+
325+
$eventsManager = new Manager();
326+
$eventsManager->attach('controller', function ($event, $controller) {
327+
// custom listener logic
328+
});
329+
330+
$controller->setEventsManager($eventsManager);
331+
```
332+
314333

315334
```php
316335
<?php

docs/db-models.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2332,6 +2332,24 @@ Phalcon's resultsets emulate scrollable cursors. You can get any row just by acc
23322332

23332333
Storing large query results in memory will consume many resources. You can however instruct Phalcon to fetch data in chunks of rows, thus reducing the need to re-execute the request in many cases. You can achieve that by setting the `orm.resultset_prefetch_records` setup value. This can be done either in `php.ini` or in the model `setup()`. More information about this can be found in the [features](#model-features) section.
23342334

2335+
You can re-execute the underlying query at any time to update the resultset with fresh data from the database by calling `refresh()`. This is useful in long-running scripts or when you suspect the data may have changed since the resultset was last loaded.
2336+
2337+
```php
2338+
<?php
2339+
2340+
use MyApp\Models\Invoices;
2341+
2342+
$invoices = Invoices::find(['conditions' => 'inv_status = "new"']);
2343+
2344+
echo count($invoices); // current count
2345+
2346+
// ... time passes, other processes may have inserted rows ...
2347+
2348+
$invoices->refresh();
2349+
2350+
echo count($invoices); // updated count
2351+
```
2352+
23352353
Note that resultsets can be serialized and stored in a cache backend. [Phalcon\Cache\Cache][cache] can help with that task. However, serializing data causes [Phalcon\Mvc\Model][mvc-model] to retrieve all the data from the database in an array, thus consuming more memory while this process takes place.
23362354

23372355
```php
@@ -3820,7 +3838,7 @@ class Invoices extends Model
38203838
```
38213839

38223840
## Model Features
3823-
The ORM has several options that control specific behaviors globally. You can enable or disable these features by adding specific lines to your `php.ini` file or using the `setup` static method on the model. You can enable or disable these features temporarily in your code or permanently.
3841+
The ORM has several options that control specific behaviors globally. You can enable or disable these features by adding specific lines to your `php.ini` file, using the `setup` static method on the model, or using `Phalcon\Support\Settings` for a PHP-level override that does not call `ini_set()`.
38243842

38253843
```ini
38263844
phalcon.orm.column_renaming = false
@@ -3890,6 +3908,28 @@ The available options are:
38903908
; phalcon.db.force_casting = Off
38913909
```
38923910

3911+
**Using `Phalcon\Support\Settings`**
3912+
3913+
`Phalcon\Support\Settings` provides PHP-level overrides for all framework settings. Unlike `ini_set()`, changes made with `Settings::set()` are stored in a static array and never modify the underlying `php.ini` configuration, which prevents settings from one project from leaking into another sharing the same PHP process.
3914+
3915+
```php
3916+
<?php
3917+
3918+
use Phalcon\Support\Settings;
3919+
3920+
// Override a setting at the PHP level
3921+
Settings::set('orm.column_renaming', false);
3922+
Settings::set('orm.events', false);
3923+
3924+
// Read the effective value (PHP override → php.ini → compiled default)
3925+
var_dump(Settings::get('orm.events')); // bool(false)
3926+
3927+
// Clear all PHP-level overrides and revert to php.ini / compiled defaults
3928+
Settings::reset();
3929+
```
3930+
3931+
The `get()` method follows this resolution order: PHP-level override → `ini_get('phalcon.<key>')` → compiled-in default.
3932+
38933933
!!! warning "WARNING"
38943934

38953935
`Phalcon\Mvc\Model::assign()` (which is used also when creating/updating/saving model) is always using setters if they exist when data arguments are passed, even when it's required or necessary. This will add some additional overhead to your application. You can change this behavior by adding `phalcon.orm.disable_assign_setters = 1` to your ini file, it will just simply use `$this->property = value`.

docs/encryption-security.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,96 @@ $random = new Random();
478478
echo $random->uuid(); // 1378c906-64bb-4f81-a8d6-4ae1bfcdec22
479479
```
480480

481+
## UUID
482+
[Phalcon\Encryption\Security\Uuid][security-uuid] is a factory that generates UUIDs (Universally Unique Identifiers) for versions 1 through 7 as defined by [RFC 4122][rfc-4122] and [RFC 9562][rfc-9562]. Each version adapter is instantiated once and cached for reuse.
483+
484+
### Namespace Constants
485+
[Phalcon\Encryption\Security\Uuid\UuidInterface][security-uuid-interface] defines the four standard RFC 4122 namespace UUIDs as constants:
486+
487+
| Constant | Value | Use |
488+
|-------------------|----------------------------------------|-----------------------------|
489+
| `NAMESPACE_DNS` | `6ba7b810-9dad-11d1-80b4-00c04fd430c8` | DNS names |
490+
| `NAMESPACE_URL` | `6ba7b811-9dad-11d1-80b4-00c04fd430c8` | URLs |
491+
| `NAMESPACE_OID` | `6ba7b812-9dad-11d1-80b4-00c04fd430c8` | ISO OIDs |
492+
| `NAMESPACE_X500` | `6ba7b814-9dad-11d1-80b4-00c04fd430c8` | X.500 distinguished names |
493+
494+
### Methods
495+
496+
```php
497+
public function v1(): string
498+
```
499+
Generates a version 1 (time-based) UUID using a 60-bit UUID timestamp and a random node/clock-sequence.
500+
501+
```php
502+
public function v3(string $namespaceName, string $name): string
503+
```
504+
Generates a version 3 (name-based MD5) UUID. The same namespace + name pair always produces the same UUID.
505+
506+
```php
507+
public function v4(): string
508+
```
509+
Generates a version 4 (random) UUID.
510+
511+
```php
512+
public function v5(string $namespaceName, string $name): string
513+
```
514+
Generates a version 5 (name-based SHA-1) UUID. The same namespace + name pair always produces the same UUID.
515+
516+
```php
517+
public function v6(): string
518+
```
519+
Generates a version 6 (reordered time-based) UUID. Fields are reordered so that UUIDs sort lexicographically in chronological order.
520+
521+
```php
522+
public function v7(): string
523+
```
524+
Generates a version 7 (Unix timestamp + random) UUID per RFC 9562. Monotonically increasing within the same millisecond window.
525+
526+
### Examples
527+
528+
```php
529+
<?php
530+
531+
use Phalcon\Encryption\Security\Uuid;
532+
use Phalcon\Encryption\Security\Uuid\UuidInterface;
533+
534+
$uuid = new Uuid();
535+
536+
// Version 1 - time-based
537+
echo $uuid->v1(); // e.g. 6ba7b810-9dad-11d1-80b4-00c04fd430c8
538+
539+
// Version 3 - name-based MD5 (deterministic)
540+
echo $uuid->v3(UuidInterface::NAMESPACE_DNS, 'phalcon.io');
541+
// always: aeb94720-3f71-35d3-9b9c-c0c7a6f55e1c
542+
543+
// Version 4 - random
544+
echo $uuid->v4(); // e.g. f47ac10b-58cc-4372-a567-0e02b2c3d479
545+
546+
// Version 5 - name-based SHA-1 (deterministic)
547+
echo $uuid->v5(UuidInterface::NAMESPACE_URL, 'https://phalcon.io');
548+
// always produces the same UUID for the same input
549+
550+
// Version 6 - reordered time-based (lexicographically sortable)
551+
echo $uuid->v6();
552+
553+
// Version 7 - Unix ms timestamp + random (lexicographically sortable)
554+
echo $uuid->v7();
555+
```
556+
557+
Because each adapter is instantiated lazily and cached, repeated calls to the same `v*()` method reuse the same adapter instance.
558+
559+
```php
560+
<?php
561+
562+
use Phalcon\Encryption\Security\Uuid;
563+
564+
$uuid = new Uuid();
565+
566+
// The v4 adapter is created once and reused
567+
$id1 = $uuid->v4();
568+
$id2 = $uuid->v4();
569+
```
570+
481571
## Dependency Injection
482572
If you use the [Phalcon\Di\FactoryDefault][factorydefault] container, the [Phalcon\Encryption\Security][security] is already registered for you. However, you might want to override the default registration in order to set your own `workFactor()`. Alternatively, if you are not using the [Phalcon\Di\FactoryDefault][factorydefault] and instead are using the [Phalcon\Di\Di][di] the registration is the same. By doing so, you will be able to access your configuration object from controllers, models, views, and any component that implements `Injectable`.
483573

@@ -547,10 +637,13 @@ Also in your views (Volt syntax)
547637
[rainbow-tables]: https://en.wikipedia.org/wiki/Rainbow_table
548638
[rfc-3548]: https://www.ietf.org/rfc/rfc3548.txt
549639
[rfc-4122]: https://www.ietf.org/rfc/rfc4122.txt
640+
[rfc-9562]: https://www.rfc-editor.org/rfc/rfc9562
550641
[secure-random]: https://ruby-doc.org/stdlib-2.2.2/libdoc/securerandom/rdoc/SecureRandom.html
551642
[security]: api/phalcon_encryption.md#encryptionsecurity
552643
[security-exception]: api/phalcon_encryption.md#encryptionsecurityexception
553644
[security-random]: api/phalcon_encryption.md#encryptionsecurityrandom
645+
[security-uuid]: api/phalcon_encryption.md#encryptionsecurityuuid
646+
[security-uuid-interface]: api/phalcon_encryption.md#encryptionsecurityuuiduuidinterface
554647
[sha1]: https://php.net/manual/en/function.sha1.php
555648
[wiki-csrf]: https://en.wikipedia.org/wiki/Cross-site_request_forgery
556649
[di]: di.md

docs/filter-validation.md

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,11 @@ Appends a message to the messages list
8080
```php
8181
public function bind(
8282
object $entity,
83-
array | object $$data
83+
array | object $data,
84+
array $whitelist = []
8485
): ValidationInterface
8586
```
86-
Assigns the data to an entity. The entity is used to obtain the validation values
87+
Assigns the data to an entity. The entity is used to obtain the validation values. When `$whitelist` is supplied, only the fields listed in it will be assigned to the entity; all other fields are skipped.
8788

8889
```php
8990
public function getEntity(): object
@@ -173,16 +174,35 @@ Adds labels for fields
173174
```php
174175
public function validate(
175176
array | object $data = null,
176-
object $entity = null
177+
object $entity = null,
178+
array $whitelist = []
177179
): Messages
178180
```
179-
Validate a set of data according to a set of rules
181+
Validate a set of data according to a set of rules. When `$whitelist` is supplied, only the listed fields are bound to `$entity`; validation itself still runs over all configured fields.
180182

181183
```php
182184
public function fails(): bool
183185
```
184186
Verify if the validation has failed or not. Returns `true` when validation fails, `false` when validation succeeds.
185187

188+
```php
189+
<?php
190+
191+
use Phalcon\Filter\Validation;
192+
use Phalcon\Filter\Validation\Validator\PresenceOf;
193+
194+
$validation = new Validation();
195+
$validation->add('name', new PresenceOf(['message' => 'Name is required']));
196+
197+
$validation->validate(['name' => '']);
198+
199+
if ($validation->fails()) {
200+
foreach ($validation->getMessages() as $message) {
201+
echo $message, PHP_EOL;
202+
}
203+
}
204+
```
205+
186206
## Activation
187207
Validation chains can be initialized in a direct manner by just adding validators to the [Phalcon\Filter\Validation][validation] object. You can put your validations in a separate file for better code reuse and organization.
188208

@@ -1876,6 +1896,31 @@ if (count($messages)) {
18761896
}
18771897
```
18781898

1899+
## Whitelist
1900+
When validating data that will be applied to an entity (e.g. a model), you can restrict which fields are assigned to the entity by passing a `$whitelist` array. Only the fields listed in the whitelist will be bound; all other incoming fields are ignored. Validators still run over all configured fields regardless of the whitelist.
1901+
1902+
```php
1903+
<?php
1904+
1905+
use Phalcon\Filter\Validation;
1906+
use Phalcon\Filter\Validation\Validator\PresenceOf;
1907+
1908+
$validation = new Validation();
1909+
1910+
$validation->add('name', new PresenceOf(['message' => 'Name is required']));
1911+
$validation->add('email', new PresenceOf(['message' => 'Email is required']));
1912+
$validation->add('role', new PresenceOf(['message' => 'Role is required']));
1913+
1914+
$entity = new stdClass();
1915+
1916+
// Only 'name' and 'email' are assigned to $entity even though 'role' is in the data
1917+
$messages = $validation->validate(
1918+
['name' => 'Phalcon', 'email' => 'team@phalcon.io', 'role' => 'admin'],
1919+
$entity,
1920+
['name', 'email']
1921+
);
1922+
```
1923+
18791924
## Filtering of Data
18801925
Data can be filtered prior to the validation ensuring that malicious or incorrect data is not validated.
18811926

0 commit comments

Comments
 (0)