2015-04-01 113 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);