Как стать автором
Обновить

PHP microservice framework — `Hello world` of Swoft

Время на прочтение3 мин
Количество просмотров2.7K


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


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] Swoft\SwoftApplication:setSystemAlias(485) Set alias @base=/tmp/swoft
2019/09/14-10:29:34 [INFO] Swoft\SwoftApplication:setSystemAlias(486) Set alias @app=@base/app
......
2019/09/14-10:29:35 [INFO] Swoft\Processor\ConsoleProcessor: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] Swoft\Server\Server:startSwoole(492) Swoole\Runtime::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/] (http://127.0.0.1:18306/) address in the browser. The following page will appear.



Hello world


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


<?php declare(strict_types=1);

namespace App\Http\Controller;

use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;

/**
 * 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] (http://127.0.0.1:18306/hello — World/index), you will get the following page.



Bench


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



Github


https://github.com/swoft-cloud/swoft

Теги:
Хабы:
Всего голосов 12: ↑9 и ↓3+6
Комментарии0

Публикации

Истории

Работа

PHP программист
104 вакансии

Ближайшие события