Laravel integrated swoole tutorial

Time:2019-12-2

1. Preparation

Install laravel

laravel new laravel-swoole

 

   

I use Valet for development, and can use

laravel-swoole.test

 

   

Visit

2. Introduction of swoole

For specific swoole installation, please go to the official website to download, compile and install, which is not described here.

1. Create a server folder in the project directory, and then create the http_server.php file in the folder. The specific contents are as follows

set([

    'worker_num' => 8,

    'max_request' => 5000,

//    'document_root' => '/Users/apple/Code/Teacher_Project/swoole_live/resources/live/',

//    'enable_static_handler' => true,

]);

//Worker process start

$http->on('WorkerStart', function ($serv, $worker_id) {

    //Load the contents of the index file

    require __DIR__ . '/../vendor/autoload.php';

    require_once __DIR__ . '/../bootstrap/app.php';

});

//Listen for HTTP requests

$http->on('request', function ($request, $response) {

    //Server information

    if (isset($request->server)) {

        foreach ($request->server as $k => $v) {

            $_SERVER[strtoupper($k)] = $v;

        }

    }

    //Header header information

    if (isset($request->header)) {

        foreach ($request->header as $k => $v) {

            $_SERVER[strtoupper($k)] = $v;

        }

    }

    //get request

    if (isset($request->get)) {

        foreach ($request->get as $k => $v) {

            $_GET[$k] = $v;

        }

    }

    //post request

    if (isset($request->post)) {

        foreach ($request->post as $k => $v) {

            $_POST[$k] = $v;

        }

    }

    //File request

    if (isset($request->files)) {

        foreach ($request->files as $k => $v) {

            $_FILES[$k] = $v;

        }

    }

    //Cookies request

    if (isset($request->cookie)) {

        foreach ($request->cookie as $k => $v) {

            $_COOKIE[$k] = $v;

        }

    }

    Ob_start(); // enable cache

    //Load laravel request core module

    $kernel = app()->make(Illuminate\Contracts\Http\Kernel::class);

    $laravelResponse = $kernel->handle(

        $request = Illuminate\Http\Request::capture()

    );

    $laravelResponse->send();

    $kernel->terminate($request, $laravelResponse);

    $res = ob_get_contents(); // get the contents of the cache

    Ob_end_clean(); // clear the cache

    //Output the contents of the cache area

    $response->end($res);

});

$http->start();

 

   

Join route in route file:

Route::get('/test1', '[email protected]');

Route::get('/test2','[email protected]');

 

   

Add method to controller:

/**

     * test 1

     * @param Request $request

     * @return string

     */

    public function test(Request $request)

    {

        Return view ('test '); (create test.blade.php in your view folder)

    }

    /**

     * test 2

     * @param Request $request

     * @return string

     */

    public function test2(Request $request)

    {

        return 'Hello World2:' . $request->get('name');

    }

 

   

3. Start swoole

Enter under terminal:

php server/http_server.php

 

   

Access browser:

http://127.0.0.1:9501/test1

http://127.0.0.1:9501/test2?name=Jelly

 

   

The corresponding results are as follows:

Access test1 route

Access test2 route