Laravel HyperfLaravel Hyperf
Laravel Hyperf
Documentation
GitHub
Laravel Hyperf
Documentation
GitHub
  • Documentation

    • Prologue

      • Contributing Guide
    • Getting Started

      • Introduction
      • Installation
      • Directory Structure
      • Deployment
    • Architecture Concepts

      • Request Lifecycle
      • Service Container
      • Service Providers
      • Facades
    • The Basics

      • Routing
      • Middleware
      • CSRF Protection
      • Controllers
      • Requests
      • Responses
      • Views
      • Blade Templates
      • URL Generation
      • Session
      • Validation
      • Error Handling
      • Logging
    • Digging Deeper

      • Artisan Console
      • Broadcasting
      • Cache
      • Collections
      • Context
      • Coroutine
      • Contracts
      • Events
      • File Storage
      • Helpers
      • HTTP Client
      • Localization
      • Mail
      • Notifications
      • Package Development
      • Processes
      • Queues
      • Rate Limiting
      • Strings
      • Task Scheduling
    • Security

      • Authentication
      • Authorization
      • Encryption
      • Hashing
    • Database

      • Getting Started
      • Query Builder
      • Pagination
      • Migrations
      • Seeding
      • Redis
    • Eloquent ORM

      • Getting Started
      • Relationships
      • Collections
      • Mutators / Casts
      • API Resources
      • Serialization
      • Factories
    • Testing

      • Getting Started
      • HTTP Tests
      • Console Tests
      • Database
      • Mocking

Contracts

  • Introduction
    • Contracts vs. Facades
  • When to Use Contracts
  • How to Use Contracts
  • Contract Reference

Introduction

Laravel Hyperf's "contracts" are a set of interfaces that define the core services provided by the framework. For example, an LaravelHyperf\Auth\Contracts\Gate contract defines the methods needed for authorizing a resource, while the LaravelHyperf\Hashing\Contracts\Hasher contract defines the methods needed for generating a secure hash.

All of the contracts live separately in the Contracts directory in their belonging packages. This provides a quick reference point for all available contracts, as well as a single, decoupled package that may be utilized when building packages that interact with Laravel Hyperf services.

Contracts vs. Facades

Laravel Hyperf's facades and helper functions provide a simple way of utilizing Laravel Hyperf's services without needing to type-hint and resolve contracts out of the service container. In most cases, each facade has an equivalent contract.

Unlike facades, which do not require you to require them in your class' constructor, contracts allow you to define explicit dependencies for your classes. Some developers prefer to explicitly define their dependencies in this way and therefore prefer to use contracts, while other developers enjoy the convenience of facades. In general, most applications can use facades without issue during development.

When to Use Contracts

The decision to use contracts or facades will come down to personal taste and the tastes of your development team. Both contracts and facades can be used to create robust, well-tested Laravel Hyperf applications. Contracts and facades are not mutually exclusive. Some parts of your applications may use facades while others depend on contracts. As long as you are keeping your class' responsibilities focused, you will notice very few practical differences between using contracts and facades.

In general, most applications can use facades without issue during development. If you are building a package that integrates with multiple PHP frameworks you may wish to use the corresponding contracts to define your integration with Laravel Hyperf's services without the need to require Laravel Hyperf's concrete implementations in your package's composer.json file.

How to Use Contracts

So, how do you get an implementation of a contract? It's actually quite simple.

Many types of classes in Laravel Hyperf are resolved through the service container, including controllers, event listeners, middleware and even route closures. So, to get an implementation of a contract, you can just "type-hint" the interface in the constructor of the class being resolved.

For example, take a look at this event listener:

<?php

namespace App\Listeners;

use App\Events\OrderWasPlaced;
use App\Models\User;
use LaravelHyperf\Cache\Contracts\Factory;

class CacheOrderInformation
{
    /**
     * Create a new event handler instance.
     */
    public function __construct(
        protected Factory $cache,
    ) {}

    /**
     * Handle the event.
     */
    public function handle(OrderWasPlaced $event): void
    {
        // ...
    }
}

When the event listener is resolved, the service container will read the type-hints on the constructor of the class, and inject the appropriate value. To learn more about registering things in the service container, check out its documentation.

Contract Reference

This table provides a quick reference to all of the Laravel Hyperf contracts and their equivalent facades:

ContractReferences Facade
LaravelHyperf\Auth\Contracts\Authorizable 
LaravelHyperf\Auth\Contracts\GateGate
LaravelHyperf\Auth\Contracts\Authenticatable 
LaravelHyperf\Auth\Contracts\FactoryContractAuth
LaravelHyperf\Auth\Contracts\GuardAuth::guard()
LaravelHyperf\Auth\Contracts\StatefulGuard 
LaravelHyperf\Auth\Contracts\SupportsBasicAuth 
LaravelHyperf\Auth\Contracts\UserProvider 
LaravelHyperf\Bus\Contracts\DispatcherBus
LaravelHyperf\Bus\Contracts\QueuingDispatcherBus::dispatchToQueue()
LaravelHyperf\Broadcasting\Contracts\FactoryBroadcast
LaravelHyperf\Broadcasting\Contracts\BroadcasterBroadcast::connection()
LaravelHyperf\Broadcasting\Contracts\ShouldBroadcast 
LaravelHyperf\Broadcasting\Contracts\ShouldBroadcastNow 
LaravelHyperf\Cache\Contracts\FactoryCache
LaravelHyperf\Cache\Contracts\Lock 
LaravelHyperf\Cache\Contracts\LockProvider 
LaravelHyperf\Cache\Contracts\RepositoryCache::driver()
LaravelHyperf\Cache\Contracts\Store 
LaravelHyperf\Config\Contracts\RepositoryConfig
LaravelHyperf\Container\Contracts\ContainerApp
LaravelHyperf\Foundation\Exceptions\Contracts\ExceptionHandler 
LaravelHyperf\Encryption\Contracts\EncrypterCrypt
LaravelHyperf\Event\Contracts\DispatcherEvent
LaravelHyperf\Filesystem\Contracts\CloudStorage::cloud()
LaravelHyperf\Filesystem\Contracts\FactoryStorage
LaravelHyperf\Filesystem\Contracts\FilesystemStorage::disk()
LaravelHyperf\Foundation\Contracts\ApplicationApp
LaravelHyperf\Foundation\Console\Contracts\Application 
LaravelHyperf\Foundation\Console\Contracts\KernelArtisan
LaravelHyperf\Hashing\Contracts\HasherHash
LaravelHyperf\Mail\Contracts\MailQueueMail::queue()
LaravelHyperf\Mail\Contracts\Mailable 
LaravelHyperf\Mail\Contracts\MailerMail
LaravelHyperf\Notifications\Contracts\DispatcherNotification
LaravelHyperf\Notifications\Contracts\FactoryNotification
LaravelHyperf\Queue\Contracts\EntityResolver 
LaravelHyperf\Queue\Contracts\FactoryQueue
LaravelHyperf\Queue\Contracts\Job 
LaravelHyperf\Queue\Contracts\MonitorQueue
LaravelHyperf\Queue\Contracts\QueueQueue::connection()
LaravelHyperf\Queue\Contracts\QueueableCollection 
LaravelHyperf\Queue\Contracts\QueueableEntity 
LaravelHyperf\Queue\Contracts\ShouldQueue 
LaravelHyperf\Foundation\Console\Contracts\ScheduleSchedule
LaravelHyperf\Foundation\Exceptions\Contracts\ExceptionHandler 
LaravelHyperf\Foundation\Exceptions\Contracts\ExceptionRenderer 
LaravelHyperf\Foundation\Http\Contracts\ExceptionRenderer 
LaravelHyperf\Cookie\Contracts\CookieCookie
LaravelHyperf\Http\Contracts\RequestContractRequest
LaravelHyperf\Http\Contracts\ResponseContractResponse
LaravelHyperf\Router\Contracts\UrlGeneratorURL

Info

The contracts in Hyperf can refer to hyperf/contract package

Edit this page
Last Updated:
Contributors: Albert Chen
Prev
Coroutine
Next
Events