laravel/framework · error · BindingResolutionException

Contextual binding attribute [{$attribute->getName()}] has n

Error message

Contextual binding attribute [{$attribute->getName()}] has no registered handler.

What it means

Thrown by Container::resolveFromAttribute when a constructor/method parameter is decorated with an attribute that implements ContextualAttribute but neither a handler was registered via whenHasAttribute() nor the attribute class itself defines a resolve() method. The container must know how to materialise the value for the attribute, otherwise resolution fails.

Source

Thrown at src/Illuminate/Container/Container.php:1412

    /**
     * Resolve a dependency based on an attribute.
     *
     * @return mixed
     *
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function resolveFromAttribute(ReflectionAttribute $attribute, ReflectionParameter $parameter)
    {
        $handler = $this->contextualAttributes[$attribute->getName()] ?? null;

        $instance = $attribute->newInstance();

        if (is_null($handler) && method_exists($instance, 'resolve')) {
            $handler = $instance->resolve(...);
        }

        if (is_null($handler)) {
            throw new BindingResolutionException("Contextual binding attribute [{$attribute->getName()}] has no registered handler.");
        }

        return $handler($instance, $this, $parameter);
    }

    /**
     * Throw an exception that the concrete is not instantiable.
     *
     * @param  string  $concrete
     * @return void
     *
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    protected function notInstantiable($concrete)
    {
        if (! empty($this->buildStack)) {
            $previous = implode(', ', $this->buildStack);

View on GitHub (pinned to bd6b5437e6)

Solutions

  1. Register a handler: $this->app->whenHasAttribute(MyAttribute::class, fn (MyAttribute $attr, Container $c, $param) => $value).
  2. Add a public resolve() method to your attribute class: public function resolve(self $attr, Container $container, ReflectionParameter $parameter): mixed { ... }.
  3. Remove the attribute from the parameter if you do not actually need contextual resolution.

Example fix

// before
#[Attribute] class CurrentUser implements ContextualAttribute {}
// used as: function handle(#[CurrentUser] User $user) {}

// after
class CurrentUser implements ContextualAttribute
{
    public function resolve(self $attr, Container $container, $parameter): User
    {
        return $container->make(Auth::class)->user();
    }
}
Defensive patterns

Strategy: try-catch

Validate before calling

$attrClass = $attribute->getName();
if (! isset($container->contextualAttributes[$attrClass])
    && ! method_exists($attrClass, 'resolve')) {
    throw new \LogicException("No handler for attribute {$attrClass}");
}

Type guard

function attributeHasHandler(string $attrClass, \Illuminate\Container\Container $c): bool
{
    return isset($c->contextualAttributes[$attrClass])
        || method_exists($attrClass, 'resolve');
}

Try / catch

use Illuminate\Contracts\Container\BindingResolutionException;
try {
    app()->make($class);
} catch (BindingResolutionException $e) {
    if (str_contains($e->getMessage(), 'has no registered handler')) {
        app()->whenHasAttribute(MyAttr::class, fn () => $default);
    }
    throw $e;
}

Prevention

When it happens

Trigger: Using a custom #[MyContextualAttribute] attribute that implements ContextualAttribute but has no resolve() method, and the developer forgot to call $container->whenHasAttribute(MyContextualAttribute::class, fn (...) => $value).

Common situations: Building a custom contextual-attribute system and forgetting the handler registration; renaming resolve() to something else; attribute class extends a base that lost its resolve method in an upgrade.

Related errors


AI-assisted analysis of laravel/framework@bd6b5437e6 (2026-08-06). Data as JSON: /data/errors/8bfd3e6c32d1c482.json. Report an issue: GitHub.