You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在Slim 3中用纯函数分离API路由与业务逻辑

Fixing Parameter Retrieval & Pure Functional Route Separation in Slim 3

Hey there, let's tackle your Slim 3 issue head-on. First up, the reason your parameter isn't being fetched correctly is a small version-specific syntax mix-up, and we can fix that while keeping your pure functional structure intact (no classes or __invoke required, just like you want).

Why Your Parameter Isn't Working

Slim 3 doesn't have a post() method on the Request object—that's a holdover from older Slim versions like Slim 2. To properly grab POST data in Slim 3, you need to use the getParsedBody() method instead. That's the root of your parameter problem.

Pure Functional Fixes (No Classes Needed)

We'll adjust your existing file structure to work correctly, while keeping everything split into separate files with plain functions.

1. Correct the Parameter Fetching in Your Handler

First, fix the registerParticipant function in src/middleware/registerParticipant.php (quick side note: calling this directory handlers might be more accurate since these are route handlers, not middleware, but you can keep the name if you prefer):

require '../lib/qrlib/vendor/qrlib.php';

function registerParticipant($request, $response, $args) {
    // Proper way to get POST params in Slim 3
    $postData = $request->getParsedBody();
    $foo = $postData['foo'] ?? null; // Use null coalescing to avoid undefined index errors
    
    // Instead of echo, use Slim's response object (follows framework best practices)
    $response->getBody()->write("foo= " . $foo);
    
    // Critical: Slim 3 requires route handlers to return a Response object
    return $response;
    
    // Add your QR code generation and other business logic here
}

Using echo might output content, but it bypasses Slim's response lifecycle—sticking to the Response object ensures your app behaves consistently with the framework's design.

2. Clean Up Route Definition (Small Optimization)

In src/routes/routes.php, you can use $this instead of passing $app into the closure—it's the standard Slim 3 way to reference the app inside a route group:

$app->group('/v1', function () {
    $this->post('/register', 'registerParticipant');
});

Your original code with use ($app) works, but this is cleaner and aligns with Slim's conventions.

3. Avoid Global Function Clutter (Optional)

If you're worried about too many global functions cluttering up your scope, you can wrap your handlers in a namespace—still purely functional, no classes required.

Update the Handler with a Namespace

Modify src/middleware/registerParticipant.php:

namespace App\Handlers;

require '../lib/qrlib/vendor/qrlib.php';

function registerParticipant($request, $response, $args) {
    $postData = $request->getParsedBody();
    $foo = $postData['foo'] ?? null;
    
    $response->getBody()->write("foo= " . $foo);
    return $response;
}

Update the Route to Use the Namespaced Function

Modify src/routes/routes.php:

$app->group('/v1', function () {
    $this->post('/register', '\App\Handlers\registerParticipant');
});

This keeps your functions organized and prevents naming collisions, all while staying true to your pure functional goal.


内容的提问来源于stack exchange,提问作者Qu4k3

火山引擎 最新活动