Lexer::fromConfig() locates the file automatically without you hardcoding any paths. The fluent API is useful when you prefer to configure everything in code or when embedding Lex inside a framework.
- lex.config.json (recommended)
- Fluent API
Place Then call the factory from anywhere inside your project:You can still chain fluent calls after
lex.config.json at your project root (the same directory as composer.json). All path values may be relative — they are resolved against the config file’s own directory — or absolute.fromConfig() to register directives, override the escaper, or add component namespaces:Config fields
| Field | Type | Default | Description |
|---|---|---|---|
viewPaths | string[] | ["views"] | Directories scanned (in order) for .lex template files. Relative paths are resolved from the config file’s directory. |
production | bool | false | Enable production mode on startup — skips source-file mtime checks and disables LexDebugger. |
sandbox | bool | false | Enable secure sandbox mode — applies a function whitelist and forbids raw echo ({!! !!}) by default. |
The cache directory is always placed at
{projectRoot}/.lexer/ and cannot be changed in lex.config.json. To override the cache location in code, call ->cache(string $dir) on the returned Lexer instance.Factory method
$startDir (defaults to the current working directory) for a lex.config.json file — the same strategy used by tools like ESLint and Prettier. When it finds the file it instantiates a Lexer pre-configured with the values it contains and places the .lexer/ cache directory next to the config file.
Throw LexException if no lex.config.json is found anywhere in the directory tree.
Fluent API reference
Every method returnsstatic, so you can chain calls in any order. Calling a configuration method after the engine has been used resets the internal engine instance so the new settings take effect.
View paths
paths(array $paths): static
Replace the entire list of view directories. Pass absolute paths.
addPath(string $path): static
Append a single directory to the existing view path list without replacing it.
cache(string $dir): static
Override the default .lexer/ cache directory location. The directory is created automatically if it does not exist.
Production and sandbox
setProduction(bool $production = true): static
Enable or disable production mode. In production mode the engine skips source-file mtime checks and serves templates from the precompiled index. LexDebugger is disabled automatically. Pass false to revert to dev mode.
enableSandbox(?SandboxConfig $config = null): static
Enable sandbox mode. Defaults to SandboxConfig::secure() when no config is provided, which forbids raw echo and applies a strict function whitelist.
setSandboxConfig(SandboxConfig $config): static
Replace the active SandboxConfig without switching the security preset. Use this to update sandbox rules after calling enableSandbox().
Escaping
setEscaper(EscaperInterface $escaper): static
Replace the default HTML escaper used for {{ $expr }} output. The default uses htmlspecialchars(ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8').
Custom directives
directive(string $name, callable $handler): static
Register a custom template directive. The handler receives the expression string (everything inside the parentheses) and must return a PHP code snippet to embed in the compiled output. Directives are resolved at parse time — the callable runs once during compilation, not on every render.
Components
component(string $name, string $viewPath): static
Register a named component mapped to a specific .lex file. Use this when the file is outside the standard components/ subdirectory or when you want an explicit mapping.
registerComponentClass(string $name, string $class): static
Bind a component name to a PHP class that implements a mount() method. Props are injected into mount() via reflection, and all public properties of the class are available in the component template.
componentClassNamespace(string $namespace): static
Set the PHP namespace used for auto-discovering component classes. Given namespace App\View\Components, a <Card> tag auto-resolves to App\View\Components\CardComponent.
componentPath(string $path): static
Add an extra directory for component file discovery. Explicitly registered paths take priority because they are added before the automatic components/ subdirectory scanning.