将 Aura 路由器与 Aura 调度程序一起使用

Using Aura router with Aura dispatcher

是否有任何 sample/tutorial 同时适用于 Aura 路由器和调度程序?我在文档页面上找到了示例代码:

// dispatch the request to the route handler.
// (consider using https://github.com/auraphp/Aura.Dispatcher
// in place of the one callable below.)
$callable = $route->handler;
$response = $callable($request);

// emit the response
foreach ($response->getHeaders() as $name => $values) {
    foreach ($values as $value) {
        header(sprintf('%s: %s', $name, $value), false);
    }
}
http_response_code($response->getStatusCode());
echo $response->getBody();

我想知道如何将 Aura 调度程序与此示例代码集成。

第二个问题是当我们想要使用 Aura 路由器检索 GET 请求时,我们使用这样的东西:

// add a route to the map, and a handler for it
$map->get('blog.read', '/blog/{id}', function ($request) {
    $id = (int) $request->getAttribute('id');
    $response = new Zend\Diactoros\Response();
    $response->getBody()->write("You asked for blog entry {$id}.");
    return $response;
});

POST方法怎么样?我尝试了以下代码,但它无法以类似的方式检索名字:

$map->post('profile', '/profile', function ($request) {
    $firstname = $request->getAttribute('firstname');

    $response = new Zend\Diactoros\Response();
    $response->getBody()->write("first name is {$firstname}");
    return $response;
});

输出缺少 $firstname 值:

first name is 

您可以通过多种方式使用 Aura.Dispatcher。下面提供的示例是一种方法。

$route = $matcher->match($request);

所以一旦匹配请求,就可以有路由或者null。

如果有路线,可以得到$route->handler;。这可以是可调用的或字符串。

您的实现说明了 Dispatcher can be invoked. From https://gist.github.com/harikt/8671136

    <?php
require dirname(__DIR__) . '/vendor/autoload.php';

use Aura\Dispatcher\Dispatcher;
use Aura\Router\RouterContainer;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequest;

class Blog
{   
    public function browse(ServerRequest $request)
    {
        $response = new Response();
        $response->getBody()->write("Browse all posts!");

        return $response;
    }

    public function read(ServerRequest $request, $id)
    {
        $id = (int) $request->getAttribute('id');
        $response = new Response();
        $response->getBody()->write("Read blog entry $id");

        return $response;
    }

    public function edit(ServerRequest $request, $id)
    {
        $response = new Response();
        $response->getBody()->write("Edit blog entry $id");

        return $response;
    }
}

$dispatcher = new Dispatcher;
$dispatcher->setObjectParam('controller');
$dispatcher->setMethodParam('action');
$dispatcher->setObject('blog', new Blog());

$routerContainer = new RouterContainer();
$map = $routerContainer->getMap();

// NB : You can use @ sign as in Laravel. So blog@browse
$map->get('blog.browse', '/blog', 'blog::browse');
$map->get('blog.read', '/blog/{id}', 'blog::read');

$request = Zend\Diactoros\ServerRequestFactory::fromGlobals(
    $_SERVER,
    $_GET,
    $_POST,
    $_COOKIE,
    $_FILES
);
$matcher = $routerContainer->getMatcher();
$route = $matcher->match($request);

if ($route) {
    foreach ($route->attributes as $key => $val) {
        $request = $request->withAttribute($key, $val);
    }
    // Take special attention, how I am using the handler.
    // Do what you want with the handler
    list($controller, $action) = explode('::', $route->handler);

    $params = [
        'controller' => $controller,
        'action' => $action,
        'request' => $request,
        // This is not needed, just showing for demo purpose
        'id' => $request->getAttribute('id'),
    ];

    $response = $dispatcher($params);

    // emit the response
    foreach ($response->getHeaders() as $name => $values) {
        foreach ($values as $value) {
            header(sprintf('%s: %s', $name, $value), false);
        }
    }
    http_response_code($response->getStatusCode());
    echo $response->getBody();
} else {
    echo "No route found";
}

我再说一遍,这不是唯一的方法。还有其他更好的方法,如果您真的有兴趣了解更多,请阅读 https://github.com/auraphp/Aura.Web_Kernel

关于您关于从 POST 获取价值的问题。不,没有别的办法。路由器不处理 POST 值。我认为 PSR-7 可能在这些方面有所改进 :-) .