2015-04-01 115 views
1

该应用使用Aura路由器进行路由。访问web应用程序的index.php只会返回一个“Route not found”错误消息。没有找到路由器使用灵气路由器的PHP

这里的(貌似)相关代码:

$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); 

$route = $router->match($path, $_SERVER); 

if (empty($route)) { 
    header("HTTP/1.0 404 Not Found"); 
    echo 'Route not found'; 
exit; 
    } 

我跑这个地方,使用wampserver。我有应用程序的路径是本地主机/网站

我错过了什么?

回答

0

您应该检查的$path值,并验证它是正确的。我有同样的问题,我意识到我正在使用一个错误的数据。

假设您的网站位于:example.com/path/sub/,并且您想要路由全部将位于sub/旁边。您的$path应该是/以便使用$router像这样:$router->add("home", "/")

如果​​那么这个值将是/path/sub/所以你的路由器将不得不指向路由:$router->add("home", "/path/sub/")

您还应该非常小心,结尾/,因为可以或不可以存在。

0

请按照文件“getting-started.md”中的步骤进行操作。

它帮助了我。

下面的代码段允许路径“/博客/(编号)”,其类似于以下URL在localhost:http://localhost/blog/42

$routerContainer = new RouterContainer(); 
$map = $routerContainer->getMap(); 
$map->get('blog.read', '/blog/{id}', function ($request, $response) { 
    $id = (int) $request->getAttribute('id'); 
    echo "You asked for blog entry {$id}.<br/>";   
    httperror(200); 
});  
// 
// ... Create more routes here ... 
// 
$request = Zend\Diactoros\ServerRequestFactory::fromGlobals(
    $_SERVER, $_GET, $_POST, $_COOKIE, $_FILES 
); 
$matcher = $routerContainer->getMatcher(); 
$route = $matcher->match($request); 
if (!$route) { 
    $failedRoute = $matcher->getFailedRoute(); 
    switch ($failedRoute->failedRule) { 
     case 'Aura\Router\Rule\Allows': 
      httperror(405); 
      break; 
     case 'Aura\Router\Rule\Accepts': 
      httperror(406); 
      break; 
     default: 
      httperror(404); 
      break; 
    } 
    die(); 
} 
foreach ($route->attributes as $key => $val) { 
    $request = $request->withAttribute($key, $val); 
} 
$response = new \Zend\Diactoros\Response; 
$callable = $route->handler; 
$response = $callable($request,$response);