Skip to content

ModelLocator

Hands out model instances.

Where ModelClassResolver answers “which class is this”, the locator owns everything that follows: bootstrapping the owning module, deciding whether an instance is shared, constructing it, and running the initialize() hand-off. Those are lifetime concerns, and they change for different reasons than the naming conventions do — which is why they are not the same class.

Singleton models are cached per context, and that cache is request-scoped state: it is dropped by ModelLocator::reset() at the worker request boundary, because a model holding request data would otherwise serve it to the next request.

final class ModelLocator implements ResetInterface

ImplementsResetInterface
Since4.0.0
SourceModel/ModelLocator.php

public function __construct(Context $context, ModelClassResolver $resolver): mixed

ParameterTypeDescription
$contextContext
$resolverModelClassResolver

Returns mixed

MethodDescription
get(string $modelName, ?string $moduleName = null, ?array<int, mixed> $parameters = null): ModelRetrieve a model instance.
reset(): voidDrop the shared singleton instances at the worker request boundary.

public function get(string $modelName, ?string $moduleName = null, ?array<int, mixed> $parameters = null): Model

Retrieve a model instance.

Passed to the constructor (when the class declares one) and to initialize().

ParameterTypeDescription
$modelNamestringA model name or fully qualified class name.
$moduleName?``stringA module name for a module model, null for a global one.
$parameters?``array``<``int``, ``mixed``>Passed to the constructor (when the class declares one) and to initialize().

Returns Model

ThrowsWhen
QuioteExceptionWhen no class exists for the name, or the class that does is not a Model.

public function reset(): void

Drop the shared singleton instances at the worker request boundary.

The resolution cache is deliberately kept — it holds class names, not request state.