首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Aura路由器AJAX路由失败-未找到路由

Aura路由器AJAX路由失败-未找到路由
EN

Stack Overflow用户
提问于 2019-02-22 10:54:33
回答 1查看 125关注 0票数 0

作为这个问题的前言,我正在转换一个演示应用程序,以使用RESTful,SEO友好的URL;除了用于AJAX请求的两个路由中的一个之外,所有的路由在用于web上的应用程序时都可以工作,并且所有的路由都使用Postman进行了完整的测试-使用一个普通的Nginx配置。

也就是说,下面是违规的路由定义-登录是失败的已定义路由:

代码语言:javascript
复制
$routing_map->post('login.read', '/services/authentication/login', [
    'params' => [
        'values' => [
            'controller' => '\Infraweb\Toolkit\Services\Authentication',
            'action' => 'login',
        ]
    ]
])->accepts([
    'application/json',
]);

$routing_map->get('logout.read', '/services/authentication/logout', [
    'params' => [
        'values' => [
            'controller' => '\Infraweb\Toolkit\Services\Authentication',
            'action' => 'logout',
        ]
    ]
])->accepts([
    'application/json',
]);

使用Postman & xdebug跟踪,我认为它(显然)没有通过我认为的路径规则中的REGEX检查,但我不能完全理解它。至少可以说是令人沮丧的。在这里发帖之前,我使用网络搜索寻找了所有我能找到的地方--谷歌的Auraphp群组最近似乎没有多少流量。可能我做错了什么,所以我想是时候向集体用户社区寻求一些方向了。任何和所有建设性的批评都是非常欢迎和赞赏的。

提前感谢,很抱歉在这个问题上浪费了任何人的带宽…

EN

回答 1

Stack Overflow用户

发布于 2019-02-25 14:00:33

让我把话说清楚。Aura.Router不做分派。它只与路由匹配。它不会处理您的路由是如何处理的。

查看完整的working example (在该示例中,假设处理程序是可调用的)

代码语言:javascript
复制
$callable = $route->handler;
$response = $callable($request);

在您的情况下,如果您匹配请求(请参阅matching request )

代码语言:javascript
复制
$matcher = $routerContainer->getMatcher();
$route = $matcher->match($request);

您将获得路由,现在您需要编写适当的方法来处理来自$route->handler的值。

这是我在var_dump /signin路由的$route->handler之后所做的事情。

代码语言:javascript
复制
array (size=1)
'params' => 
    array (size=1)
    'values' => 
        array (size=2)
        'controller' => string '\Infraweb\LoginUI' (length=17)
        'action' => string 'read' (length=4)

下面尝试了完整的代码。正如我之前提到的,我不知道你的路由处理逻辑。所以这是由你来决定是否正确地写东西。

代码语言:javascript
复制
<?php
require __DIR__ . '/vendor/autoload.php';

use Aura\Router\RouterContainer;

$routerContainer = new RouterContainer();

$map = $routerContainer->getMap();

$request = Zend\Diactoros\ServerRequestFactory::fromGlobals(
    $_SERVER,
    $_GET,
    $_POST,
    $_COOKIE,
    $_FILES
);

$map->get('application.signin.read', '/signin', [
    'params' => [
        'values' => [
            'controller' => '\Infraweb\LoginUI',
            'action' => 'read',
        ]
    ]
]);

$map->post('login.read', '/services/authentication/login', [
    'params' => [
        'values' => [
            'controller' => '\Infraweb\Toolkit\Services\Authentication',
            'action' => 'login',
        ]
    ]
])->accepts([
    'application/json',
]);


$matcher = $routerContainer->getMatcher();

// .. and try to match the request to a route.
$route = $matcher->match($request);
if (! $route) {
    echo "No route found for the request.";
    exit;
}

echo '<pre>';
var_dump($route->handler);
exit;

根据记录,这是composer.json

{ "require":{ "aura/router":"^3.1","zendframework/zend-diactoros":"^2.1“}}

和运行方式

代码语言:javascript
复制
php -S localhost:8000 index.php

和浏览http://localhost:8000/signin

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54819407

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档