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

Context

  • Introduction
  • Interacting with Context
    • Get Context
    • Set Context
    • Check If Context Key Exists
    • Override Context
    • Destroy Context
    • Destroy All Context
    • Get or Set Context
    • Copy Context
    • Copy Context From Non Coroutine Environment

Introduction

Context is an important feature in Laravel Hyperf. It is used to store states within coroutines. Contexts in different coroutines are isolated, and when a coroutine terminates, its context will be destroyed automatically. You don't need to worry about memory leaks problem in context.

If you're not familiar with the context management in coroutines, you can read Isolating global variables with a coroutine context manager in Swoole for more detailed information.

Important

Do not mutate static variables in coroutines unless you know what you're doing, because static variables are shared by all coroutines. It will cause states pollution and unpredictable bugs.

Interacting with Context

Get Context

The get method will return the value of the key, if not exists, it will return the default value.

use LaravelHyperf\Context\Context;

$foo = Context::get('foo', 'bar');

$foo = Context::get('foo', 'bar', $coroutineId);

Set Context

The set method will set the value of the key, and return the value of the key.

use LaravelHyperf\Context\Context;

// $foo is bar
$foo = Context::set('foo', 'bar');

$foo = Context::set('foo', 'bar', $coroutineId);

Check If Context Key Exists

The has method will return true if the key exists, otherwise false.

use LaravelHyperf\Context\Context;

$exists = Context::has('foo');

$exists = Context::has('foo', $coroutineId);

Override Context

Sometimes we need to check if a specific key exists. If the key exists, override the value of a key in the context. You may do so using the override method.

use LaravelHyperf\Context\Context;

$request = Context::override(ServerRequestInterface::class, function (ServerRequestInterface $request) {
    return $request->withAddedHeader('foo', 'bar');
});

Note

You can pass coroutineId to the third parameter to override the context in a specific coroutine.

Destroy Context

The destroy method will delete the value of the key in coroutine context.

use LaravelHyperf\Context\Context;

Context::destroy('foo');

Context::destroy('foo', $coroutineId);

Destroy All Context

The destroyAll method will delete all the values in coroutine context.

use LaravelHyperf\Context\Context;

Context::destroyAll('foo');

Context::destroyAll('foo', $coroutineId);

Get or Set Context

The getOrSet method will return the value of the key, if not exists, it will set the value of the key and return.

use LaravelHyperf\Context\Context;

$foo = Context::getOrSet('foo', 'bar');

$foo = Context::getOrSet('foo', 'bar', $coroutineId);

Copy Context

The copy method will copy the context from another coroutine to the current coroutine.

use LaravelHyperf\Context\Context;

Context::copy('foo', $fromCoroutineId, $onlyKeys);

Copy Context From Non Coroutine Environment

For compatibility,Context also works in non-coroutine environment. All the non-coroutine environment will share the same context copy.

The copyFromNonCoroutine method will copy the context from non-coroutine environment to current coroutine.

use LaravelHyperf\Context\Context;

Context::copy('copyFromNonCoroutine', $keys);

Context::copy('copyFromNonCoroutine', $keys, $coroutineId);
Edit this page
Last Updated:
Contributors: Albert Chen
Prev
Collections
Next
Coroutine