EasySwoole Documentation

repository·3.x·Indexed 26 days ago

https://github.com/easy-swoole/easyswoole

A high-performance, distributed, and persistent memory PHP framework built on the Swoole extension. Optimized for APIs, it supports HTTP, TCP, WebSocket, and UDP protocols. Key features include a global dependency injection container, PSR-7 based HTTP messages, coroutine clients for MySQL and Redis, and built-in management commands for servers, processes, and crontab tasks.

Tokens
1.7K
Snippets
3
Records
21
Agent score
89%

What's inside EasySwoole

  1. Overview of EasySwoole Features

    3.x

    EasySwoole is a high-performance, memory-resident distributed PHP framework built on Swoole Server. It is designed specifically for APIs and provides several core capabilities:

    • Server Support: Built-in HTTP, TCP, WebSocket, and UDP coroutine servers.
    • Dependency Injection: Global dependency injection container.
    • Messaging: PSR-7 based HTTP message implementation.
    • Middleware: Support for HTTP, TCP, WebSocket, and UDP middleware.
    • Database & Clients: Database ORM, and coroutine clients for MySQL, Redis, RPC, and HTTP.
    • Concurrency: Support for coroutines, asynchronous tasks, and user-defined processes.
    • Routing & Validation: High-performance routing and flexible parameter validation.
    • Utilities: Logging component, general connection pools, Crontab/timer support, and remote console support.
  2. Quick Start with EasySwoole

    3.x

    To set up a new EasySwoole project, install the framework via Composer, run the installation command to scaffold the project, and then start the server.

    1. Install the framework: composer require easyswoole/easyswoole=3.7.x
    2. Scaffold the project: php vendor/bin/easyswoole.php install
    3. Start the server: php easyswoole.php server start
    composer require easyswoole/easyswoole=3.7.x
    php vendor/bin/easyswoole.php install
    php easyswoole.php server start
  3. Run EasySwoole using Docker

    3.x

    You can run EasySwoole in a Docker container. First, pull the desired image, then run the container with a volume mount for your project directory and port mapping for the server.

    1. Pull the image:

    docker pull easyswoolexuesi2021/easyswoole:php8.1.22-alpine3.16-swoole4.8.13

    2. Run the container:

    docker run --name easyswoole \
    -v /workspace/project:/var/www/project \
    -p 9501:9501 -it \
    --privileged -u root \
    --entrypoint /bin/sh \
    easyswoolexuesi2021/easyswoole:php8.1.22-alpine3.16-swoole4.8.13

    Note:

    • The WorkerDir is set to ***/var/www***.
    • Once inside the container, follow the standard Quick Start commands to install and start the server.
  4. Create an HTTP Controller in EasySwoole

    3.x

    To handle HTTP requests, extend the EasySwoole\Http\AbstractInterface\Controller class. You can use the $this->response() method to write content to the response body.

    <?php
    
    namespace App\HttpController;
    
    use EasySwoole\Http\AbstractInterface\Controller;
    
    /**
     * Class Index
     * @package App\HttpController
     */
    class Index extends Controller
    {
        public function index()
        {
            $this->response()->write('Hello World');
        }
    }
  5. Kill EasySwoole processes

    3.x

    Use the kill action to terminate processes. You can target a specific process by its PID or an entire group of processes.

    Options:

    • --mode: Specify the run mode (e.g., --mode=dev).
    • --pid: Kill a specific process by its PID (e.g., --pid=9501).
    • --group: Kill all processes belonging to a specific group (e.g., --group=Crontab).
    • --force: If provided, uses SIGKILL instead of the default SIGTERM to force the process to terminate immediately.