路由

时间:2017-10-07 12:54:25

标签: php routing

我今天开始使用PHRoute,我在加载课程时遇到了问题。

我有以下目录结构:

/
   public
      index.php
   app
      controllers
         Home.php
      models
      views

我想在index.php

中制作路线
use Phroute\Phroute\RouteCollector;
use Phroute\Phroute\Dispatcher;

// Autoloader for Composer
if (file_exists(ROOT . 'vendor/autoload.php')) {
    require ROOT . 'vendor/autoload.php';
}
// load application config (error reporting etc.)
require APP . 'config/config.php';

function _autoload($class)
{
    $path = APP . "controllers/" . $class . ".php";
    if(file_exists(stream_resolve_include_path($path)))
        require($path);
}

spl_autoload_register("_autoload");

$router = new RouteCollector();

$router->any('/', ['Home','displayUser']);

$dispatcher = new Dispatcher($router->getData());

try {
    $response = $dispatcher->dispatch($_SERVER['REQUEST_METHOD'], parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
} catch (Exception $e) {
    echo $e->getMessage();
    die();
}

echo $response;

Home.php看起来像:

class Home {   
   public function displayUser()
   {
    return 'This is the default page and will respond to /controller and /controller/index';
   }    
}

我仍然收到错误“未捕获的错误:未找到”主要'主页...“。哪里弄错了?

0 个答案:

没有答案