Testing and Tooling

Cache and about

Cache the control of each model with access:cache in every deployment, and read the state of the package in the Access Control section of php artisan about.

Caching the controls

php artisan access:cache
php artisan access:clear

access:cache writes the control of each discovered model, and the models that the reindexWhen() rules of the controls watch, into bootstrap/cache. access:clear removes both files.

  • php artisan optimize runs access:cache, and php artisan optimize:clear runs access:clear.
  • The cache is used as it is, the way Laravel uses its route and event caches: run it again in every deployment, after adding, moving or deleting a control, or adding a reindexWhen().
  • It runs once every service provider has booted, so it caches the controls of every directory passed to Access::discoverControlsIn().
  • A cached control class that was deleted since is skipped.
  • access:check reports a stale cache: stale_control_cache for the controls, stale_watch_cache for the watched models. See access:check.

Without the cache, the package discovers the controls when a model first asks for its control. With Laravel Scout installed, the first Eloquent write of a process also builds the control of every discovered ControlledSearchable model, to know which writes reindex which documents: the cache spares that work in production.

The about command

php artisan about prints an "Access Control" section:

php artisan about --only=access_control
  Access Control ................................................................
  Automatic policies ................................................. registered
  Cache .............................................................. not cached
  Controls ................................................................... 12
  Discovery paths .................................................. app/Access/Controls
  Scout ...................................................... installed, meilisearch driver
  Version ........................................................................ 1.0.0
  laravel-rest-api ............................... installed, AccessControlExtension enabled
RowValue
VersionThe installed version of the package.
ControlsThe number of controls, read from the cache when it exists, else from the discovery paths. No control is built. When discovery fails, the row reads discovery failed: and the first line of the error.
Discovery pathsEvery directory the controls are discovered in, relative to the base path: app/Access/Controls first, then the ones added with Access::discoverControlsIn().
Cachecached when access:cache wrote its file, not cached otherwise.
Automatic policiesregistered after Access::registerPolicies(), not registered otherwise, or when the application replaced the policy guesser afterwards.
Scoutnot installed, or the driver of the environment.
laravel-rest-apinot installed, AccessControlExtension enabled, or AccessControlExtension listed in rest.disabled_extensions.
  • --json gives the same rows under the access_control key.
  • The section is registered when the application runs in the console.
  • An empty value prints none, and a row that cannot be read prints unavailable: with the first line of the error: the section never makes about fail.

Add this section when you report an issue.