2017-03-17 111 views
1

我正在從laracast(https://laracasts.com/series/php-for-beginners)繼續本教程,我在本集(16 - 製作路由器)中。其中顯示瞭如何構建基本路由器。如視頻所示,我已經完成了我的知識,但是我在構建路由器時遇到了問題。 我收到此錯誤信息:沒有路由定義爲這個uri

Fatal error: Uncaught exception 'Exception' with message 'No routes define for this uri' in C:\wamp64\www\todo\core\Router.php on line 23 Exception: No routes define for this uri in C:\wamp64\www\todo\core\Router.php on line 23

我如何通過這個錯誤?這裏是我的代碼

routes.php文件:

$router->define([ 
    '' => 'controllers/index.php', 
    'about' => 'controllers/about.php', 
    'contact' => 'controllers/contact.php' 
]); 

Router.php

class Router 
{ 

    protected $routes = []; 


    // this function defines our routes 
    public function define($routes) 
    { 
     # code... 
     $this->routes = $routes; 
    } 

    public function direct($uri){ 
     if (array_key_exists($uri, $this->routes)) { 
      # code... 
      return $this->routes[$uri]; 
     } 
     throw new Exception("No routes define for this uri"); 

    } 
} 

的index.php

$database = require 'core/bootstrap.php'; 

$router = new Router; 

require 'routes.php'; 

$uri = trim($_SERVER['REQUEST_URI'], '/'); 

require $router->direct($uri); 

如果您需要更多的信息通知我。

UPDATE 這是我的地盤結構wampserver WWW文件夾:

enter image description here

+1

更改您的錯誤消息,以提供更多信息。也許''沒有路線定義這個uri:$ uri「'會告訴你你錯過了什麼路線。 – miken32

+0

未捕獲異常'異常'消息'沒有路由爲此uri定義:todo' –

+0

這就是我得到的 –

回答

2

我有同樣的問題,在此過程中。我beleve你已經有htcaccess文件 和這些代碼裏面

RewriteEngine On 
RewriteBase /todo/ 
RewriteRule ^.*$ index.php [END] 

反正路線應該是這樣的

$router->define([ 
    'todo' => 'controllers/index.php', 
    'todo/about' => 'controllers/about.php', 
    'todo/contact' => 'controllers/contact.php' 
]); 

,或者您可以連接到PHP內置的CMD Web服務器,它會解決爲您的路線問題以及

關注