Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Php Attribute Reader Laravel Package

spatie/php-attribute-reader

Lightweight PHP 8+ utility to read native attributes from classes, methods, properties, and parameters using reflection. Designed for simple, fast attribute discovery in frameworks and libraries, with an API that fits common annotation-style workflows.

View on GitHub
Deep Wiki
Context7

Getting Started

Start by installing the package via Composer:

composer require spatie/php-attribute-reader

Then, create a simple attribute-driven class to test basic functionality. For example, define an attribute like #[Route('/users')] on a class method, and use the AttributeReader class to retrieve it:

use Spatie\AttributeReader\AttributeReader;

$reader = new AttributeReader();
$methods = $reader->getMethods(new MyClass());
$routes = $reader->getAttributeInstances($methods['index'], Route::class);

First use case: reading route attributes from controller methods to build a routing table at runtime — ideal for lightweight frameworks or middleware builders.

Implementation Patterns

  • Attribute scanning: Use getAttributeInstances() to retrieve all attribute instances of a given class from a reflection target (class, method, property, parameter). Useful for tag-based configurations (e.g., #[Cacheable('hourly')], #[Loggable('debug')]).
  • Filtering by name or class: Pass the attribute class (e.g., Route::class) or a string name ('Route') to filter. For multi-attribute scenarios (e.g., a method with #[Route('/api'), #[Authorize('admin')]), chain filters or retrieve all at once.
  • Hybrid docblock support: Enable useAttributeAnnotations() for fallback reading of docblock tags (e.g., @Route("/legacy")), easing migration from docblock-based tools to native attributes.
  • Service integration: Inject AttributeReader into services like validation validators, command handlers, or event dispatchers to drive behavior declaratively (e.g., #[Validates(type: User::class)]).

Gotchas and Tips

  • Performance: Reflection is expensive — cache results (e.g., in a registry or static cache) when processing many classes repeatedly (e.g., in CLI tools or bootstrapping).
  • Attribute visibility: The package respects visibility, but note that private properties/methods still can be read — use caution if consuming attributes from third-party or extended classes.
  • Custom attribute handling: If an attribute’s constructor is readonly and you need to override values (e.g., in tests), instantiate manually or use getAttributeInstances(..., $useConstructor: false) if supported.
  • Fallback docblock parsing: Enable docblock parsing only where needed ($reader->useAttributeAnnotations(true)) — it’s slower and may cause false positives if docblocks are noisy.
  • Extension point: You can subclass AttributeReader to add custom behavior (e.g., logging all reads), or wrap it in a domain-specific facade (e.g., RouteReader::fromClass(RequestHandler::class)).
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport