drnrxv9383
2017-03-17 17:25 阅读 38
已采纳

此uri没有定义路由

I’m following this tutorial from laracast(https://laracasts.com/series/php-for-beginners) and I’m at this episode(16 - Make a Router) in the series. Which shows how to build a basic router. I have done everthing to my knowledge as illustrated in the video, but I’m experiencing problems with building the router. I’m getting this error message:

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

How do I get passed this error? Here are my codes

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);

If you need more information inform me.

UPDATE This is my site structure in wampserver www folder:

enter image description here

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dtmjqyfz21793 dtmjqyfz21793 2017-07-02 19:41

    I had the same problem in this course. I beleve you already have htcaccess file and these codes inside

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

    Anyway the routes should be like this

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

    or you can connect to PHP built-in Web Server from cmd, it will solve route issue for you as well

    Regards

    点赞 评论 复制链接分享

相关推荐