【发布时间】:2015-09-29 09:51:32
【问题描述】:
Routes.php
use MyMVC\Core\Route;
$route = new Route;
$route->add('/', 'HomeController@index');
$route->add('about', 'AboutController@index');
$route->add('contact', 'ContactController@index');
Index.php
<?php
/**
* Define Constants
*/
define('BASE_PATH', dirname(realpath(__FILE__)));
define('APP_PATH', BASE_PATH . "/app");
/**
* Including the Composer's autoloader
*/
require_once 'vendor/autoload.php';
/**
* Load the routes declarations
*/
require_once 'app/routes.php';
/**
* Bootstrap our application
*/
require_once 'app/init.php';
/**
* Initialize our beautiful framework
*/
$application = new \MyMVC\Application($route);
composer.json
"autoload" : {
"psr-4" : {
"MyMVC\\" : "app/"
},
"classmap": [
"app/Controllers",
"app/Helpers"
],
"files": ['app/routes.php'] // already removed this line
},
当使用require_once 时,它会给出undefined variable route error,而如果我只使用require,它会显示路由对象。
为什么会这样?
【问题讨论】:
-
一定是其他原因导致了这个问题,如
The require_once statement is identical to require except PHP will check if the file has already been included, and if so, not include (require) it again. -
可能它早先包含在脚本中,因此不再需要它?
-
@chriz 更新我的代码。这就是
index.php的全部内容。 -
添加您的
composer.json。 -
你跑
composer dump-autoload了吗?
标签: php