AthenaeumAthenaeum
Packages
  • next
  • current
  • v9.x
  • v8.x
  • v7.x
  • v6.x
  • v5.x
  • v4.x
  • v3.x
  • v2.x
  • v1.x
Changelog
GitHub
Packages
  • next
  • current
  • v9.x
  • v8.x
  • v7.x
  • v6.x
  • v5.x
  • v4.x
  • v3.x
  • v2.x
  • v1.x
Changelog
GitHub
  • Version 6.x

    • Release Notes
    • Upgrade Guide
    • New to this...
    • Contribution Guide
    • Security Policy
    • Code of Conduct
    • Origin
  • ACL

    • Introduction
    • How to install
    • Setup
    • Permissions
    • Roles
    • Users
    • Cached Permissions
  • Audit

    • Audit
    • How to install
    • Setup
    • Recording
    • Events
  • Circuits

    • Circuits
    • How to install
    • Setup
    • Usage
    • Events
  • Collections

    • Collections
    • How to install
    • Summation

      • Summation Collection
      • Items Processor
  • Config

    • Configuration Loader
    • How to install
    • Setup
    • Load Configuration Files
    • Custom File Parsers
  • Console

    • Command and Schedule Registration
    • How to install
    • Setup
    • Commands
    • Schedules
  • Container

    • IoC Service Container
    • How to install
    • Container
    • List Resolver
  • Core

    • Athenaeum Core Application
    • How to install
    • Setup
    • Usage

      • Configuration
      • Service Providers
      • Service Container
      • Events
      • Caching
      • Logging
      • Console
      • Task Scheduling
      • Exception Handling
      • Extending Core Application
      • Testing
  • Database

    • Introduction
    • How to install
    • Models

      • Instantiatable
      • Sluggable
    • Query

      • Criteria (Query Filter)
  • Dto

    • Data Transfer Object (DTO)
    • How to install
    • Create Interface
    • Implement DTO
    • How to use
    • Populate
    • Export
    • Json
    • Serialization
    • Nested DTOs
    • Array DTO
  • ETags

    • ETags
    • How to install
    • Setup
    • Usage
    • Generators

      • Default Generator
      • Custom Generator
    • Eloquent Models
    • Macros
  • Events

    • Register Listeners and Subscribers
    • How to install
    • Setup
    • Listeners
    • Subscribers
  • Filters

    • Search Filter Utilities
    • Prerequisites
    • How to install
    • Setup
    • Processor
    • Filters Builder
    • Predefined Resources

      • Search Processor
      • Sorting Processor
      • Constraints Processor
      • Matching Processor
    • Tip: Create a base builder
  • Flysystem

    • Introduction
    • Database Adapter

      • Introduction
      • How to install
      • Setup
      • Data Deduplication
      • MIME-Type Detection
  • Http

    • Api

      • Http API
      • How to install
      • Setup
      • Resources

        • Introduction
        • Timestamps
        • Self-Link
        • Relations
        • Registrar
      • Middleware

        • Introduction
        • Request Must Be Json
        • Capture Fields To Select
    • Clients

      • Http Clients
      • How to install
      • Setup
      • Basic Usage
      • Available Methods

        • Fluent Api
        • Protocol Version
        • Base Uri
        • Http Method and Uri
        • Headers
        • Accept & Content-Type
        • Authentication
        • Http Query
        • Payload Format
        • Payload
        • Attachments
        • Cookies
        • Response Expectations
        • Middleware
        • Conditions
        • Criteria
        • Redirects
        • Timeout
        • Debugging
        • Logging
        • Driver Options
        • Driver
      • Http Query Builder

        • Introduction
        • Select
        • Where
        • Dates
        • Include
        • Pagination
        • Sorting
        • Raw Expressions
        • Custom Grammar
    • Cookies

      • Http Cookies
      • How to install
      • Usage
    • Messages

      • Http Messages
      • How to install
      • Serializers
  • Maintenance

    • Modes

      • Maintenance Modes
      • How to install
      • Setup
      • Basic Usage
      • Available Drivers
  • Mime Types

    • MIME-Types
    • How to install
    • Setup
    • Usage
    • Drivers

      • Available Drivers
      • File Info
  • Properties

    • Properties Overload
    • How to install
    • Usage
    • Naming Convention
    • Properties Visibility
  • Redmine

    • Redmine Api Client
    • How to install
    • Setup
    • General Usage

      • Supported Operations
      • Fetch list of resources
      • Find
      • Fetch
      • Create new record
      • Update existing record
      • Delete existing record
      • Relations
    • Available Resources

      • Predefined Resources
      • Attachments
      • Enumerations
      • Issue Relations
      • Users
      • User Groups
      • Roles
      • Project Memberships
      • Versions (Milestones)
      • Issue Categories
      • Trackers
  • Service

    • Service Registrar
    • How to install
    • How to use
  • Streams

    • Streams
    • How to install
    • Setup
    • How to use

      • Introduction
      • Open and Close
      • Raw Resource
      • Seeking
      • Reading
      • Writing
      • Size
      • Truncate
      • Flush
      • Hash
      • MIME-Type
      • Output
      • Locking
      • Transactions
      • Meta
      • Misc
  • Support

    • Introduction
    • How to install
    • Laravel Aware-of Helpers

      • How to use
      • Enforce Via Interface
      • Custom Default
      • Pros and Cons
      • Available Helpers
    • Aware-of Properties

      • Generator
      • Available Aware-of Helpers
    • Live Templates
  • Testing

    • Introduction
    • How to install
    • Test Cases
    • Testing Aware-of Helpers
  • Utils

    • Introduction
    • How to install
    • Array
    • Duration
    • Json
    • Math
    • Memory
    • Method Helper
    • Invoker
    • Populatable
    • String
    • Version
  • Validation

    • Introduction
    • How to install
    • Setup
    • Rules

      • Alpha-Dash-Dot
      • Semantic Version
You are viewing documentation for an outdated version. It is no longer supported!

Registrar

The Api Resource Registrar is responsible for keeping track of each Eloquent Model's corresponding ApiResource. This component is, amongst other things, used to automatically find a relation's corresponding ApiResource.

  • Registration of Api Resources
  • Obtain Registra instance
  • Register single Api Resource
  • Register multiple Api Resources
  • Determine if Model has an Api Resource
  • Get Api Resource
  • Find Api Resource by Type
  • Find Model by Resource Type
  • Remove Api Resource

Registration of Api Resources

Your registration should primarily be undertaken inside your config/api-resources.php. See how to register Api Resource introduction.

Nevertheless, the remaining of this sections will illustrate how to work with the Registrar, so that you may use it for more advanced approaches, if such is needed.

Obtain Registra instance

The Registrar component is bound as a singleton in the Service Container of your application. To obtain the instance, you can use ApiResourceRegistrarTrait.

use Aedart\Http\Api\Traits\ApiResourceRegistrarTrait;
use Illuminate\Http\Request;

class UsersController {
    use ApiResourceRegistrarTrait;
    
    public function index(Request $request){
        $registrar = $this->getApiResourceRegistrar();
        
        // ...remaining not shown...
    }
}

Register single Api Resource

If you wish to manually register a single Api Resource, for an Eloquent Model, use the set() method.

use App\Http\Resources\AddressResource;
use App\Models\Address;

$registrar->set(Address::class, AddressResource::class);

Register multiple Api Resources

To register a list of Api Resources, use register(). The method accepts an array of key-values;

  • key = class path to your eloquent model
  • value = class path to the model's corresponding Api Resource
$registrar->register([
    Address::class => AddressResource::class,
    User::class => UserResource::class,
    Book::class => BookResource::class
]);

Determine if Model has an Api Resource

The has() method can be used to determine if a model has an Api Resource registered. It accepts either a class path to an Eloquent Model or a Model instance.

echo $registrar->has(Address::class); // 1

Get Api Resource

The get() method returns a string class path to the registered Api Resource. If no Api Resource was registered for given model, then the method returns null.

echo $registrar->get(Address::class); // \App\Http\Resources\AddressResource

Find Api Resource by Type

To find an Api Resource (class path) by its type, use findResourceByType(). Method accepts a resource's string type in either singular or plural form. If an Api Resource exists for the given type, then its class path is returned. Null is returned if no Api Resource was found.

// Type in singular form
echo $registrar->findResourceByType('address'); // \App\Http\Resources\AddressResource

// Type in plural form
echo $registrar->findResourceByType('users'); // \App\Http\Resources\UserResource

Find Model by Resource Type

It is also possible to find the Eloquent Model of an Api Resource, by searching for the resource type via the findModelByType() method.

// Type in singular form
echo $registrar->findModelByType('address'); // \App\Models\Address

// Type in plural form
echo $registrar->findModelByType('users'); // \App\Models\User

Remove Api Resource

Should you be required to remove an Api Resource for a given model, then you can do so via the forget() method. The method will return true when Api Resource and successfully removed, for the given model. If not, false is returned.

echo $registrar->forget(Address::class); // 1
Edit page
Last Updated: 16/02/2023, 09:10
Contributors: Alin Eugen Deac, alin
Prev
Relations