Skip to content

Facades

The facade component provides static proxies for commonly used Hyperf services.

Installation

shell
composer require friendsofhyperf/facade

The base package requires friendsofhyperf/support, hyperf/context, and hyperf/di. Most facades need an optional package. Install only the packages required by the facades that your application uses, for example:

shell
composer require hyperf/config hyperf/logger

Supported Facades

FacadeContainer accessorAdditional package
AMQPHyperf\Amqp\Producerhyperf/amqp
App, DIPsr\Container\ContainerInterfaceIncluded (hyperf/di)
AsyncQueueHyperf\AsyncQueue\Driver\DriverFactoryhyperf/async-queue
CachePsr\SimpleCache\CacheInterfacehyperf/cache
ConfigHyperf\Contract\ConfigInterfacehyperf/config
CookieHyperf\HttpMessage\Cookie\CookieJarInterfacehyperf/framework
CryptFriendsOfHyperf\Encryption\Encrypterfriendsofhyperf/encryption
EventPsr\EventDispatcher\EventDispatcherInterfacehyperf/framework
File, FilesystemLeague\Flysystem\Filesystemhyperf/filesystem
KafkaHyperf\Kafka\ProducerManagerhyperf/kafka
LogHyperf\Logger\LoggerFactoryhyperf/logger
PipelineFriendsOfHyperf\Support\Pipeline\HubIncluded (friendsofhyperf/support)
RedisHyperf\Redis\Redishyperf/redis
RequestHyperf\HttpServer\Contract\RequestInterfacehyperf/framework
ResponseHyperf\HttpServer\Contract\ResponseInterfacehyperf/framework
SessionHyperf\Contract\SessionInterfacehyperf/session
TranslatorHyperf\Contract\TranslatorInterfacehyperf/translation
ValidatorHyperf\Validation\Contract\ValidatorFactoryInterfacehyperf/validation
ViewHyperf\View\RenderInterfacehyperf/view

App is an alias of DI, and File is an alias of Filesystem.

Configuration And Resolution

The package is discovered by Hyperf, but its ConfigProvider does not publish or merge any configuration. Configure each underlying Hyperf component normally.

Except for the component-specific methods below, a static facade call is forwarded to the object resolved from ApplicationContext by the accessor shown above. The first resolved object is cached by the facade base class. If the container does not contain the accessor, a Hyperf\Di\Exception\NotFoundException is thrown.

Use getFacadeRoot() when the underlying object itself is required:

php
use FriendsOfHyperf\Facade\Config;

$config = Config::getFacadeRoot();
$name = Config::get('app_name', 'hyperf');

The methods accepted by each proxy are the public methods of its underlying accessor. Refer to the corresponding component documentation for their signatures.

Component-Specific Methods

These public methods are implemented by the facade classes themselves:

FacadeMethodBehavior
AMQPdispatch(ProducerMessageInterface $producerMessage): PendingAmqpProducerMessageDispatchInstance method that creates a pending AMQP dispatch.
AsyncQueuedispatch(Closure|JobInterface $job): PendingAsyncQueueDispatchInstance method that creates a pending async-queue dispatch.
AsyncQueuepush(JobInterface $job, int $delay = 0, ?string $pool = null)Pushes through the selected pool, or through $job->getPoolName() when the pool is null; documented to return bool.
Cookiehas($key)Checks the current request cookie, not the cookie jar; documented to accept string and return bool.
Cookieget($key, $default = null)Reads the current request cookie; documented to accept a string key and return mixed.
Kafkadispatch(ProduceMessage $produceMessage): PendingKafkaProducerMessageDispatchInstance method that creates a pending Kafka dispatch.
Kafkasend(ProduceMessage $produceMessage, ?string $pool = null): voidSends one message as a batch through the selected pool, defaulting to default.
KafkasendBatch($produceMessages, ?string $pool = null): voidSends a documented ProduceMessage[] batch through the selected pool, defaulting to default.
Logchannel(string $name = 'hyperf', string $channel = 'default')Gets a LoggerInterface from LoggerFactory. Other static Log calls use these defaults.

AMQP::dispatch, AsyncQueue::dispatch, and Kafka::dispatch are declared as instance methods, not static facade proxy methods.

php
use FriendsOfHyperf\Facade\AsyncQueue;
use FriendsOfHyperf\Facade\Cookie;
use FriendsOfHyperf\Facade\Kafka;
use FriendsOfHyperf\Facade\Log;

AsyncQueue::push($job, delay: 5, pool: 'default');

Kafka::send($message);
Kafka::sendBatch([$firstMessage, $secondMessage], 'default');

$token = Cookie::get('token');
$hasToken = Cookie::has('token');

Log::channel('hyperf', 'default')->info('Using an explicit logger channel');
Log::info('Uses the hyperf/default logger');