没有路由为此uri定义(No routes define for this uri)

编程入门 行业动态 更新时间:2024-10-19 11:43:06
没有路由为此uri定义(No routes define for this uri)

我正在从laracast( https://laracasts.com/series/php-for-beginners )关注本教程,我正在本系列中的这一集(16 - Make a Router)。 其中显示了如何构建基本路由器。 我已经完成了我的知识,如视频中所示,但我遇到了构建路由器的问题。 我收到此错误消息:

致命错误:第23行的C:\ wamp64 \ www \ todo \ core \ Router.php消息'没有为此uri定义路由'的未捕获异常'异常'例外:在C:\ wamp64 \中没有为此uri定义路由第23行的www \ todo \ core \ Router.php

我如何通过此错误? 这是我的代码

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

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

更新这是我在wampserver www文件夹中的网站结构:

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:

最满意答案

我在这门课程中遇到了同样的问题。 我觉得你已经有了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' ]);

或者您可以从cmd连接到PHP内置Web服务器,它也将为您解决路由问题

问候

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

更多推荐

本文发布于:2023-07-26 15:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1277495.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   定义   define   uri   routes

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!