首页 > 解决方案 > 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;

标签: phproutes

解决方案


由于您以“ http://localhost/ecommerce ”的形式访问应用程序的 URL,因此您需要添加“/ecommerce”。

通常,当您将应用程序托管到适当的域时,可以在没有“电子商务”部分的情况下访问该应用程序。在您的开发环境中使用虚拟主机来解决此问题总是更好。


推荐阅读