Home  >  Article  >  PHP Framework  >  Laravel integration Swoole tutorial

Laravel integration Swoole tutorial

藏色散人
藏色散人forward
2019-07-17 13:40:064420browse

Laravel integration Swoole tutorial

1. Preparation

Installing Laravel

laravel new laravel-swoole

Myself Use valet for development, you can use

laravel-swoole.test

to access

2.Introduce swoole

Please go to the official website for specific swoole installation Downloading, compiling, and installing are not explained here.

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

<?php
$http = new swoole_http_server(&#39;127.0.0.1&#39;, 9501);
$http->set([
    &#39;worker_num&#39; => 8,
    &#39;max_request&#39; => 5000,
//    &#39;document_root&#39; => &#39;/Users/apple/Code/Teacher_Project/swoole_live/resources/live/&#39;,
//    &#39;enable_static_handler&#39; => true,
]);
//工作进程启动
$http->on(&#39;WorkerStart&#39;, function ($serv, $worker_id) {
    //加载index文件的内容
    require __DIR__ . &#39;/../vendor/autoload.php&#39;;
    require_once __DIR__ . &#39;/../bootstrap/app.php&#39;;
});
//监听http请求
$http->on(&#39;request&#39;, function ($request, $response) {
    //server信息
    if (isset($request->server)) {
        foreach ($request->server as $k => $v) {
            $_SERVER[strtoupper($k)] = $v;
        }
    }
    //header头信息
    if (isset($request->header)) {
        foreach ($request->header as $k => $v) {
            $_SERVER[strtoupper($k)] = $v;
        }
    }
    //get请求
    if (isset($request->get)) {
        foreach ($request->get as $k => $v) {
            $_GET[$k] = $v;
        }
    }
    //post请求
    if (isset($request->post)) {
        foreach ($request->post as $k => $v) {
            $_POST[$k] = $v;
        }
    }
    //文件请求
    if (isset($request->files)) {
        foreach ($request->files as $k => $v) {
            $_FILES[$k] = $v;
        }
    }
    //cookies请求
    if (isset($request->cookie)) {
        foreach ($request->cookie as $k => $v) {
            $_COOKIE[$k] = $v;
        }
    }
    ob_start();//启用缓存区
    //加载laravel请求核心模块
    $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();//获取缓存区的内容
    ob_end_clean();//清除缓存区
    //输出缓存区域的内容
    $response->end($res);
});
$http->start();

Add routing to the routing file :

Route::get(&#39;/test1&#39;, &#39;UsersController@test&#39;);
Route::get(&#39;/test2&#39;,&#39;UsersController@test2&#39;);

Add method in the controller:

/**
     * 测试1
     * @param Request $request
     * @return string
     */
    public function test(Request $request)
    {
        return view(&#39;test&#39;);#在你的视图文件夹创建test.blade.php
    }
    /**
     * 测试2
     * @param Request $request
     * @return string
     */
    public function test2(Request $request)
    {
        return &#39;Hello World2:&#39; . $request->get(&#39;name&#39;);
    }

3. Start swoole

Enter in the 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

Laravel integration Swoole tutorial

Visit test2 Routing

Laravel integration Swoole tutorial

For more Laravel related technical articles, please visit the Laravel Framework Getting Started Tutorial column to learn!

The above is the detailed content of Laravel integration Swoole tutorial. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:LaravelChen. If there is any infringement, please contact admin@php.cn delete