Php _自动加载问题
我对自动加载功能有问题,以下是场景: 注意:这不是我自己的。我只是用它来学习更多关于OOP和MVC的知识 首先是相关文件 .htaccess:Php _自动加载问题,php,templates,autoload,Php,Templates,Autoload,我对自动加载功能有问题,以下是场景: 注意:这不是我自己的。我只是用它来学习更多关于OOP和MVC的知识 首先是相关文件 .htaccess: <IfModule mod_rewrite.c> RewriteEngine On RewriteBase /phoenix/ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /phoenix/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?%{QUERY_STRING} [NE,L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
</IfModule>
libs/libs_Boot.php:
class Boot {
public function __construct() {
$this->controller = $GLOBALS['C']->INCPATH . 'controllers/';
$this->request = array();
}
public function LOAD() {
$this->_parse_input();
$this->_load_controller();
$this->load_template();
}
private function _parse_input() {
/* Here is the logic to get the controller name. */
$request = explode('/', ...);
$this->request = $request[2];
}
private function _load_controller() {
require_once( $this->controller.$this->request.'.php' );
$controller = new $this->request;
}
public function load_template($name) {
global $C, $D;
require 'view/header.php';
require 'view/' . $name . '.php';
require 'view/footer.php';
}
}
controllers/index.php:
<?php require_once('./application/LOADER.php'); ?>
<?php $this->load_template('index');?>
因此,在controller/index.php中,我可以编写:
<?php
if (/* the user is not logged in */) {
$this->redirect('signin');
}
$this->load_template('index');
?>
我不能使用它,因为load\u template
不是index
的方法,而是Boot
类的方法
发生了什么?
\uu autoload
在尝试创建尚未定义的类的实例时将被调用。错误消息表示代码试图在某处创建名为“index”的类。如果它出现在示例代码中,则可能是$controller=new$this->request代码>。您需要在此行之前包含一个定义类“index”的文件。紧跟在,require_once($this->controller.$this->request..php)前面的行正如您所确定的,code>,是一个执行此操作的地方
至于让index.php中的调用$this->load_template('index')
不起作用,在任何情况下都不应该这样做。一方面,类的实现应该包含在单个文件中;否则,代码的内聚性就不强。另一方面,$this
是文件中的一个自由变量,在代码清晰性方面几乎与全局变量一样糟糕。相反,应该有一个标准的控制器方法,您的Boot
调度器将调用它;load_template
的主体是一个很好的实现候选者
如果您无法更改控制器基类(您可能无法控制框架代码,但您可以创建自己的fork,具体取决于许可证),您可以创建一个丑陋的hack并定义一个索引类,并调用类之外的任何其他代码:
<?php
class index {
}
$this->load_template('index');
不是有效的PHP代码。您是否可以更改触发器错误,然后粘贴完整代码。是否尝试包含正确的路径?libs文件夹中是否存在index.php dosent。Index.php是控制器,文件位于controllers文件夹中!。那么为什么autoload要尝试加载它呢?这是一个很好的做法,可以用于自动加载功能,因为它可以避免将来与其他可能的自动加载程序发生冲突,从而发生致命错误:找不到类“index”。但是如何修复呢?哈哈,谢谢@outis,你能给我一个好的MVC教程吗implementation@MarianPetrov:没有具体的教程建议。谷歌能找到什么就有什么。我唯一的建议是,你永远不会只从一个教程中学习。有这么多不好的信息,最好研究多个来源,努力将黄金从渣滓中分离出来。
class Boot {
public function __construct() {
$this->controller = $GLOBALS['C']->INCPATH . 'controllers/';
$this->request = array();
}
public function LOAD() {
$this->_parse_input();
$this->_load_controller();
$this->load_template();
}
private function _parse_input() {
/* Here is the logic to get the controller name. */
$request = explode('/', ...);
$this->request = $request[2];
}
private function _load_controller() {
require_once( $this->controller.$this->request.'.php' );
$controller = new $this->request;
}
public function load_template($name) {
global $C, $D;
require 'view/header.php';
require 'view/' . $name . '.php';
require 'view/footer.php';
}
}
<?php $this->load_template('index');?>
public function redirect($loc, $abs=FALSE) {
global $C;
if( ! $abs && preg_match('/^http(s)?\:\/\//', $loc) ) {
$abs = TRUE;
}
if( ! $abs ) {
if( $loc{0} != '/' ) {
$loc = $C->SITE_URL.$loc;
}
}
if( ! headers_sent() ) {
header('Location: '.$loc);
}
echo '<meta http-equiv="refresh" content="0;url='.$loc.'" />';
echo '<script type="text/javascript"> self.location = "'.$loc.'"; </script>';
exit;
}
<?php
if (/* the user is not logged in */) {
$this->redirect('signin');
}
$this->load_template('index');
?>
class index {
function __construct(){
$this->load_template('index');
}
}
<?php
class index {
}
$this->load_template('index');
class index {
function - construct(){
$this->load_template('index');
}