beikeshop 是一款基于laravel 开源的电商框架,由于php本身不是基于内存的运行模式,所以原生方式运行php框架本身就跑的很慢,再加上laravel的服务提供者的设计方式,在运行之前会加载大量的代码,导致laravel运行的更慢。
服务提供者的设计方式其实是很优秀的设计,这是nginx+php-fpm的运行方式,让每次请求都去加载服务者,这就让网站运行更慢。
swoole 是一个php的扩展,一种基于内存运行的方式来运行php的代码。
所以 laravel+swoole的方式会把服务提供者相关代码先加载到内存中,每次请求进来,就直接从内存中加载,这就让laravel运行的飞快。larvel-octane 扩展可以让无缝接入swoole.
beikeshop是基于lavavel 的,但是实际直接使用的时候会有一些坑。
# 现安装 octane扩展 composer require laravel/octane # install一下,会生成octane相关配置 php artisan octane:install # 启动网站 php artisan octane:start 启动网站后 前端页面是可以正常访问的。但是管理后台会有问题,需要做以下修改 beike/Admin/Providers/AdminServiceProvider.php 中注释代码 $adminName = admin_name(); if (! Str::startsWith($uri, "/{$adminName}")) { return; } admin的页面空白问题: 新增中间件: app/Http/Middleware/DebugResponse.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\StreamedResponse;
class DebugResponse
{
public function handle(Request $request, Closure $next)
{
$response = $next($request);
// 如果是视图响应,直接输出内容
if ($response->headers->get('content-type') === 'text/html; charset=UTF-8') {
return new StreamedResponse(function() use ($response) {
echo $response->getContent();
}, 200, [
'Content-Type' => 'text/html; charset=UTF-8'
]);
}
return $response;
}
}
app/Http/Kernel.php 中 $middlewareGroups 里面的admin里面注册中间件
<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
protected $middlewareGroups = [
'shop' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\App\Http\Middleware\SetLocaleFromSession::class,
\App\Http\Middleware\ShareViewData::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'admin' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\App\Http\Middleware\SetLocaleAdmin::class,
\App\Http\Middleware\ShareViewData::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\App\Http\Middleware\DebugResponse::class,
],
}
}
基本上按照上面的修改,swoole版本的beikeshop 就可以正常运行
简单测试对比: nginx+fpm模式下需要1.4s 的页面, swoole模式下只需要300ms
写了一个shell,可以把上面的操作自动化,在原来 beike项目目录下执行shell
#!/bin/bash
# 安装 Laravel Octane
composer require laravel/octane
# 安装 Octane 服务
php artisan octane:install
# 启动 Octane
#php artisan octane:start
# 修改 AdminServiceProvider.php 文件中的代码
ADMIN_PROVIDER_PATH="beike/Admin/Providers/AdminServiceProvider.php"
# 检查文件是否存在
if [ ! -f "$ADMIN_PROVIDER_PATH" ]; then
echo "文件不存在:$ADMIN_PROVIDER_PATH"
exit 1
fi
# 判断操作系统并选择正确的 sed 命令
if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS 的 sed
sed -i '' '/\$adminName = admin_name();/,/return;/{
s/^/\/\//
/return;/!b
n
s/^/\/\//
}' "$ADMIN_PROVIDER_PATH"
else
# Linux 的 sed
sed -i '/\$adminName = admin_name();/,/return;/{
s/^/\/\//
/return;/!b
n
s/^/\/\//
}' "$ADMIN_PROVIDER_PATH"
fi
# 创建 DebugResponse 中间件文件
DEBUG_RESPONSE_PATH="app/Http/Middleware/DebugResponse.php"
mkdir -p "$(dirname "$DEBUG_RESPONSE_PATH")"
cat > "$DEBUG_RESPONSE_PATH" << 'EOF'
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\StreamedResponse;
class DebugResponse
{
public function handle(Request $request, Closure $next)
{
$response = $next($request);
// 如果是视图响应,直接输出内容
if ($response->headers->get('content-type') === 'text/html; charset=UTF-8') {
return new StreamedResponse(function() use ($response) {
echo $response->getContent();
}, 200, [
'Content-Type' => 'text/html; charset=UTF-8'
]);
}
return $response;
}
}
EOF
echo "中间件文件已创建:$DEBUG_RESPONSE_PATH"
# 修改 Kernel.php 文件
KERNEL_PATH="app/Http/Kernel.php"
# 检查文件是否存在
if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS 的 sed
sed -i '' '/^[[:space:]]*'"'admin'"' *=> *\[/a\
\\\App\\Http\\Middleware\\DebugResponse::class,\
' "$KERNEL_PATH"
else
# Linux 的 sed
sed -i '/^[[:space:]]*'"'admin'"' *=> *\[/a\
\\\App\\Http\\Middleware\\DebugResponse::class,\
' "$KERNEL_PATH"
fi
echo "中间件已成功添加到 Kernel 文件:$KERNEL_PATH"
echo "操作完成!"