drnrxv9383 2017-03-17 17:25
浏览 44
已采纳

此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 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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条