Home > Article > PHP Framework > 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('127.0.0.1', 9501); $http->set([ 'worker_num' => 8, 'max_request' => 5000, // 'document_root' => '/Users/apple/Code/Teacher_Project/swoole_live/resources/live/', // 'enable_static_handler' => true, ]); //工作进程启动 $http->on('WorkerStart', function ($serv, $worker_id) { //加载index文件的内容 require __DIR__ . '/../vendor/autoload.php'; require_once __DIR__ . '/../bootstrap/app.php'; }); //监听http请求 $http->on('request', 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('/test1', 'UsersController@test'); Route::get('/test2','UsersController@test2');
Add method in the controller:
/** * 测试1 * @param Request $request * @return string */ public function test(Request $request) { return view('test');#在你的视图文件夹创建test.blade.php } /** * 测试2 * @param Request $request * @return string */ public function test2(Request $request) { return 'Hello World2:' . $request->get('name'); }
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
Visit test2 Routing
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!