php - phroute 无法正常工作,说路由未定义
问题描述
我正在使用 phroute 。
当我使用项目名称作为路由时,它适用于根路由。但如果我使用“/”或其他东西。然后路线不起作用。
这条路线就像根路线一样工作。
/*this is working as root route like / */
$router->controller('/ecommerce', \App\Controllers\HomeController::class);
这条路线行不通。并说通过告诉这个项目名称,这条路线没有定义。比如“未定义电子商务路线”
/* If i run this route it shows , that ecomeerce route not found. */
$router->controller('/', \App\Controllers\HomeController::class);
我的完整代码在这里。
<?php
use Phroute\Phroute\Dispatcher;
use Phroute\Phroute\Exception\HttpMethodNotAllowedException;
use Phroute\Phroute\Exception\HttpRouteNotFoundException;
use Phroute\Phroute\RouteCollector;
use Phroute\Phroute\RouteParser;
require_once 'vendor/autoload.php';
$router = new RouteCollector(new RouteParser());
/*this is working as root route like / */
$router->controller('/ecommerce', \App\Controllers\HomeController::class);
/* If i run this route it shows , that ecomeerce route not found. */
$router->controller('/', \App\Controllers\HomeController::class);
$router->controller('/ecommerce/users', \App\Controllers\UsersController::class);
$dispatcher = new Dispatcher($router->getData());
try {
$response = $dispatcher->dispatch($_SERVER['REQUEST_METHOD'],
parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
} catch (HttpRouteNotFoundException $e) {
echo $e->getMessage();
die();
} catch (HttpMethodNotAllowedException $e) {
echo $e->getMessage();
die();`enter code here`
}
echo $response;
解决方案
由于您以“ http://localhost/ecommerce ”的形式访问应用程序的 URL,因此您需要添加“/ecommerce”。
通常,当您将应用程序托管到适当的域时,可以在没有“电子商务”部分的情况下访问该应用程序。在您的开发环境中使用虚拟主机来解决此问题总是更好。
推荐阅读
- laravel - 将 Laravel 表绑定到其他数据库连接
- assembly - FASM align 32 部分对齐不够
- text - 如何在颤动中显示文本以使 RTL 语言右对齐而 LTR 左对齐?
- debugging - 为什么我的 admob 广告没有在最终版本的 apk(签名)上显示,但在调试 apk(未签名)上完美显示?
- angular - ng bootstrap typeahead 不适用于角度反应形式
- python - 如何从列中的每个组中选择前 100 名?
- r - 使用函数 randomForest 时,分类变量的类型应该是什么?
- swift - 使用 NSKeyedArchiver 保存不可编码的 Swift 字典
- c++ - 查找给定序列是单调的还是严格单调的
- python - Python 自动化,关注 Chrome 浏览器,获取 URL