Quick Guide: Swoole Installation and Hello World in Hyperf.

Sharif
4 min readJan 30, 2024

--

swoole php model
Swoole PHP model

Step 1: Installing the Swoole Extension

Installation Preparation

Before installation, you must ensure that the following software has been installed on the system

  • 4.8Version requires php-7.2or higher
  • 5.0Version requires php-8.0or higher

Swoole serves as the backbone of Hyperf, laying the groundwork for asynchronous and coroutine-based PHP development. Let’s kick things off by installing the Swoole extension using one of the following methods:

Linux:

The Swoole project has been included in the PHP official extension library. In addition to manual download and compilation, you can also download pecland install it with one click through the command officially provided by PHP.

pecl install swoole

Add Swoole to php.ini

extension=swoole.so
You can check the Swoole Extension using the following command in your Linux terminal:

Use php -mto check whether it is loaded successfully swoole.so. If not, php.inithe path may be wrong.
You can use php --ini to locate php.inithe absolute path. Loaded Configuration FileOne item displays the loaded php.ini file. If the value is, noneit means that no php.inifile is loaded at all and you need to create it yourself.

Windows:

On Windowsthe platform, you can use WSL(Windows Subsystem for Linux)or CygWin.

Step 2: Create a project via Composer

Installation Preparation

  • PHP >= 8.1
  • Any of the following network engines
  • Swoole PHP extension >= 5.0, with swoole.use_shortname set to Off in your php.ini
  • Swow PHP extension >= 1.4
  • JSON PHP extension
  • Pcntl PHP extension (Only on Swoole engine)
  • OpenSSL PHP extension (If you need to use the HTTPS)
  • PDO PHP extension (If you need to use the MySQL Client)
  • Redis PHP extension (If you need to use the Redis Client)
  • Protobuf PHP extension (If you need to use the gRPC Server or Client)

Based on the Swoole engine:

composer create-project hyperf/hyperf-skeleton helloWorld

Based on the Swow engine:

composer create-project hyperf/swow-skeleton helloWorld

Define routes via file configuration

The routes file is located in config/routes.php. Below are some common usage examples:

<?php
use Hyperf\HttpServer\Router\Router;

// The code example here provides three different binding definitions for each example. In practice, you only need to define one of them.

// Set the route for a GET request, bind the access address '/get' to App\Controller\IndexController::get()
Router::get('/get', 'App\Controller\IndexController::get');
Router::get('/get', 'App\Controller\IndexController@get');
Router::get('/get', [\App\Controller\IndexController::class, 'get']);

// Set the route for a POST request, bind the access address '/post' to App\Controller\IndexController::post()
Router::post('/post', 'App\Controller\IndexController::post');
Router::post('/post', 'App\Controller\IndexController@post');
Router::post('/post', [\App\Controller\IndexController::class, 'post']);

// Set a route that allows GET, POST, and HEAD requests, bind the access address '/multi' to App\Controller\IndexController::multi()
Router::addRoute(['GET', 'POST', 'HEAD'], '/multi', 'App\Controller\IndexController::multi');
Router::addRoute(['GET', 'POST', 'HEAD'], '/multi', 'App\Controller\IndexController@multi');
Router::addRoute(['GET', 'POST', 'HEAD'], '/multi', [\App\Controller\IndexController::class, 'multi']);

Since Hyperf has a built-in coroutine server, Hyperf will run as a CLI process. After defining our routes and writing the application logic code, we can start the server by entering the root directory of the project and executing the command php bin/hyperf.php start.

php bin/hyperf.php start
After running the start command

Navigate to the project stage and open the app/Controller/IndexController.php file. Set the stage by replacing the existing script with this eloquent composition:

<?php
declare(strict_types=1);
namespace App\Controller;

class IndexController extends AbstractController
{
public function index()
{
return "Hello, world!";
}
}
Running in the port:9501

Open http://127.0.0.1:9501: Witness the magic! Your “Hello, World!” message should appear, powered by the asynchronous might of Swoole and Hyperf.

Reloading the code

Hyperf is a persistent CLI application. Once the process starts, the parsed PHP code will remain unchanged while the process is running, so changes to the PHP code after the server starts will have no effect. If you want the server to reload your code, you need to terminate the process by typing CTRL + C in the console and then re-execute the command php bin/hyperf.php start.

Alternatively, check this solution for hot reload https://hyperf.wiki/3.1/#/en/watcher

Conclusion:

With Swoole and Hyperf in your arsenal, you’ve unlocked a new level of performance and efficiency for your PHP projects. Remember, this is just the beginning! Dive deeper, experiment, and unleash the true potential of asynchronous PHP development. Let’s conquer the web together, one line of code at a time!

--

--

Sharif
Sharif

Written by Sharif

Tech enthusiast and solo developer focused on simplifying complex ideas. I write about tech, entrepreneurship, and building impactful digital solutions.

No responses yet