当我安装 composer 并使用自动加载选项时,路由不起作用?
Routes won't work when I install composer and use autoloading option?
当我安装 composer 并设置自动加载选项,然后单击主页以外的 links 时,出现此错误“致命错误:未捕获错误:Class 'homeController' not在 C:\xampp\htdocs\gacho\app\core\Application.php:13 中找到堆栈跟踪:#0 C:\xampp\htdocs\gacho\public\index.php(16): Application->__construct() # 1 {main} 在第 13 行的 C:\xampp\htdocs\gacho\app\core\Application.php 中抛出,但是 link 主页工作正常。这是我的代码结构和代码:
代码结构
gacho
|-app
|- controller
|- core
|- model
|- view
|-public
|-vendor
|-composer
|- autoload_classmap.php
|- autoload.php
|- .htaccess
|- composer.json
|- index.php
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ index.php [QSA,L]
composer.json
{
"autoload": {
"classmap":[
"../app"
]
}
}
autoload_classmap
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'App\Controller\HomeController' => $baseDir .
'/../app/controller/HomeController.php',
'App\Core\Application' => $baseDir . '/../app/core/Application.php',
'App\Core\Controller' => $baseDir . '/../app/core/Controller.php',
'App\Core\Database' => $baseDir . '/../app/core/Database.php',
'App\Core\View' => $baseDir . '/../app/core/View.php',
'App\Model\User' => $baseDir . '/../app/model/User.php',
);
index.php
define('ROOT', dirname(__DIR__) . DIRECTORY_SEPARATOR);
define('APP', ROOT . 'app' . DIRECTORY_SEPARATOR);
define('CONTROLLER', ROOT . 'app' . DIRECTORY_SEPARATOR . 'controller' .
DIRECTORY_SEPARATOR);
define('VIEW', ROOT . 'app' . DIRECTORY_SEPARATOR . 'view' .
DIRECTORY_SEPARATOR);
define('MODEL', ROOT . 'app' . DIRECTORY_SEPARATOR . 'model' .
DIRECTORY_SEPARATOR);
define('CORE', ROOT . 'app' . DIRECTORY_SEPARATOR . 'core' .
DIRECTORY_SEPARATOR);
$modules = [ROOT, APP, CORE, CONTROLLER];
require_once __DIR__ . '\vendor\autoload.php';
new Application;
Application.php
class Application
{
protected $controller = 'HomeController';
protected $action = 'index';
protected $params = [];
public function __construct()
{
$this->prepareURL();
if (file_exists(CONTROLLER. $this->controller . '.php')) {
$this->controller = new $this->controller;
if (method_exists($this->controller, $this->action)) {
call_user_func_array([$this->controller, $this->action], $this->params);
}
}
}
好吧,classmap 显示 /../app/controller/HomeController.php
而您的错误消息显示 Fatal error: Uncaught Error: Class 'homeController
。
所以请确保您的控制器文件名和 Class 名称相同。
(按照 OP 的要求写下答案)
当我安装 composer 并设置自动加载选项,然后单击主页以外的 links 时,出现此错误“致命错误:未捕获错误:Class 'homeController' not在 C:\xampp\htdocs\gacho\app\core\Application.php:13 中找到堆栈跟踪:#0 C:\xampp\htdocs\gacho\public\index.php(16): Application->__construct() # 1 {main} 在第 13 行的 C:\xampp\htdocs\gacho\app\core\Application.php 中抛出,但是 link 主页工作正常。这是我的代码结构和代码:
代码结构
gacho
|-app
|- controller
|- core
|- model
|- view
|-public
|-vendor
|-composer
|- autoload_classmap.php
|- autoload.php
|- .htaccess
|- composer.json
|- index.php
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ index.php [QSA,L]
composer.json
{
"autoload": {
"classmap":[
"../app"
]
}
}
autoload_classmap
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'App\Controller\HomeController' => $baseDir .
'/../app/controller/HomeController.php',
'App\Core\Application' => $baseDir . '/../app/core/Application.php',
'App\Core\Controller' => $baseDir . '/../app/core/Controller.php',
'App\Core\Database' => $baseDir . '/../app/core/Database.php',
'App\Core\View' => $baseDir . '/../app/core/View.php',
'App\Model\User' => $baseDir . '/../app/model/User.php',
);
index.php
define('ROOT', dirname(__DIR__) . DIRECTORY_SEPARATOR);
define('APP', ROOT . 'app' . DIRECTORY_SEPARATOR);
define('CONTROLLER', ROOT . 'app' . DIRECTORY_SEPARATOR . 'controller' .
DIRECTORY_SEPARATOR);
define('VIEW', ROOT . 'app' . DIRECTORY_SEPARATOR . 'view' .
DIRECTORY_SEPARATOR);
define('MODEL', ROOT . 'app' . DIRECTORY_SEPARATOR . 'model' .
DIRECTORY_SEPARATOR);
define('CORE', ROOT . 'app' . DIRECTORY_SEPARATOR . 'core' .
DIRECTORY_SEPARATOR);
$modules = [ROOT, APP, CORE, CONTROLLER];
require_once __DIR__ . '\vendor\autoload.php';
new Application;
Application.php
class Application
{
protected $controller = 'HomeController';
protected $action = 'index';
protected $params = [];
public function __construct()
{
$this->prepareURL();
if (file_exists(CONTROLLER. $this->controller . '.php')) {
$this->controller = new $this->controller;
if (method_exists($this->controller, $this->action)) {
call_user_func_array([$this->controller, $this->action], $this->params);
}
}
}
好吧,classmap 显示 /../app/controller/HomeController.php
而您的错误消息显示 Fatal error: Uncaught Error: Class 'homeController
。
所以请确保您的控制器文件名和 Class 名称相同。 (按照 OP 的要求写下答案)