- PVSM.RU - https://www.pvsm.ru -

PHP microservice framework — `Hello world` of Swoft

PHP microservice framework — `Hello world` of Swoft - 1

Overview

What is Swoft?

Swoft is a PHP high performance microservice coroutine framework. It has been published for many years and has become the best choice for php. It can be like Go, built-in coroutine web server and common coroutine client and is resident in memory, independent of traditional PHP-FPM. There are similar Go language operations, similar to the Spring Cloud framework flexible annotations.

Through three years of accumulation and direction exploration, Swoft has made Swoft the Spring Cloud in the PHP world, which is the best choice for PHP's high-performance framework and microservices management.

Github

https://github.com/swoft-cloud/swoft [1]

Article

This is a set of Swoft tutorials, which will be continuously updated. You are welcome to discuss and learn together.

Tutorial

Install

Use composer to install Swoft

swoft:/www# composer create-project swoft/swoft swoft
Installing swoft/swoft (v2.0.5)
  - Installing swoft/swoft (v2.0.5): Loading from cache
Created project in swoft
> @php -r "file_exists('.env') || copy('.env.example', '.env');"
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 84 installs, 0 updates, 0 removals
  - Installing swoft/stdlib (v2.0.5): Loading from cache
  - Installing swoft/server (v2.0.5): Loading from cache
  - Installing nikic/php-parser (v4.2.4): Downloading (100%)         
  ......
toolkit/cli-utils suggests installing inhere/php-validate (Very lightweight data validate tool)
toolkit/cli-utils suggests installing inhere/console (a lightweight php console application library.)
......
Writing lock file
Generating autoload files

Start

After install, go to the Swoft project root directory and start Swoft like below.

root@MyServer:/tmp/swoft# php bin/swoft http:start
2019/09/14-10:29:34 [INFO] SwoftSwoftApplication:setSystemAlias(485) Set alias @base=/tmp/swoft
2019/09/14-10:29:34 [INFO] SwoftSwoftApplication:setSystemAlias(486) Set alias @app=@base/app
......
2019/09/14-10:29:35 [INFO] SwoftProcessorConsoleProcessor:handle(39) Console command route registered (group 14, command 42)
                            Information Panel
  ***********************************************************************
  * HTTP     | Listen: 0.0.0.0:18306, type: TCP, mode: Process, worker: 3
  * RPC      | Listen: 0.0.0.0:18307, type: TCP
  ***********************************************************************

HTTP server start success !
2019/09/14-10:29:35 [INFO] SwoftServerServer:startSwoole(492) SwooleRuntime::enableCoroutine
2019/09/14-10:29:35 [INFO] 
......

Start success, You can see the port off Http and Rpc. Then you access [http://127.0.0.1:18306/ [2]] (http://127.0.0.1:18306/ [2]) address in the browser. The following page will appear.

PHP microservice framework — `Hello world` of Swoft - 2

Hello world

Create a new HelloWorldController.php file under the Swoft controller directory ( app/Http/Controller ) like this.

<?php declare(strict_types=1);

namespace AppHttpController;

use SwoftHttpServerAnnotationMappingController;
use SwoftHttpServerAnnotationMappingRequestMapping;

/**
 * Class HelloWorldController
 *
 * @since 2.0
 *
 * @Controller(prefix="hello-world")
 */
class HelloWorldController
{
    /**
     * @RequestMapping()
     *
     * @return string
     */
    public function index(): string
    {
        return 'Hello World !';
    }
}

The controller and router used here will be described in detail in the subsequent article, restart the service, browser access [http://127.0.0.1:18306/hello-world/index [3]] (http://127.0.0.1:18306/hello [4] — World/index), you will get the following page.

PHP microservice framework — `Hello world` of Swoft - 3

Bench

Simply test Swoft under the Apache Bench tool and the results are as follows:

PHP microservice framework — `Hello world` of Swoft - 4

Github

https://github.com/swoft-cloud/swoft [1]

Автор: stelin

Источник [5]


Сайт-источник PVSM.RU: https://www.pvsm.ru

Путь до страницы источника: https://www.pvsm.ru/php-2/330040

Ссылки в тексте:

[1] https://github.com/swoft-cloud/swoft: https://github.com/swoft-cloud/swoft

[2] http://127.0.0.1:18306/: http://127.0.0.1:18306/

[3] http://127.0.0.1:18306/hello-world/index: http://127.0.0.1:18306/hello-world/index

[4] http://127.0.0.1:18306/hello: http://127.0.0.1:18306/hello

[5] Источник: https://habr.com/ru/post/467413/?utm_campaign=467413&utm_source=habrahabr&utm_medium=rss