使用下划线(PEAR 样式)表示法从子文件夹自动加载 php 类
我对面向对象的 PHP 和 MVC 有点陌生,所以我真的需要一些帮助。
我有一个 MVC 风格的文件夹结构,文件系统中有子文件夹
- 例如 view/classes/subfolder/classname.php
我正在使用 mod_rewrite 来实现人类友好的 URL,如 /classname
或 /foldername/calssname
,然后作为下划线分隔值传递给页面加载器。
- 例如 foldername_classname
// Page Loader File require_once($_SERVER['DOCUMENT_ROOT'].'/local/classLoader.php'); session_start(); $page = new $_REQUEST['page'];
我以前一直使用 [if / else if / else] 块在每个可能的文件夹中进行测试,但这似乎效率低下,所以我正在寻找一种更好的方法来让自动加载器看起来在许多不同的地点。
这是我最近的失败,它没有找到任何请求的类,只是为每个类输出一个异常,最终导致致命错误!:
function classToPath($class) { $path = str_replace('_', '/', $class) . '.php'; return $path; } function autoloadController($class) { echo 'LoadController'.'
'; $root = '/controller/classes/'; $pathtoclass = $root.classToPath($class); try { if( file_exists($pathtoclass) ) require_once($pathtoclass); else throw new Exception('Cannot load controller '.$class); } catch(Exception $e) { echo 'Controller exception: ' . $e->getMessage() . "
"; } } function autoloadModel($class) { echo 'LoadModel'.'
'; $root = '/model/classes/'; $pathtoclass = $root.classToPath($class); try { if( file_exists($pathtoclass) ) require_once($pathtoclass); else throw new Exception('Cannot load model '.$class); } catch(Exception $e) { echo 'Model exception: ' . $e->getMessage() . "
"; } } function autoloadView($class) { echo 'LoadView'.'
'; $root = '/view/classes/'; $pathtoclass = $root.classToPath($class); try { if( file_exists($pathtoclass) ) require_once($pathtoclass); else throw new Exception('Cannot load view '.$class); } catch(Exception $e) { echo 'View exception: ' . $e->getMessage() . "
"; } } spl_autoload_register('autoloadController'); spl_autoload_register('autoloadModel'); spl_autoload_register('autoloadView');
I was also wondering exactly how the URL to folder/class mapping should work:
- i.e. URL:
/foldername/classname
mod_rewritten to foldername_classname
;with a class filename of
classname.php
under the foldername
folder;and a php class definition of
class foldername_classname extends another_class { etc.
这是正确的方法吗?
I'm a bit new to Object Oriented PHP and MVC's so I really need some help please.
I have an MVC style folder structure with subfolders in the filesystem
- e.g. view/classes/subfolder/classname.php
I'm using mod_rewrite for human friendly URL's, as /classname
or /foldername/calssname
, which are then passed to a page loader as underscore separated values.
- e.g. foldername_classname
// Page Loader File require_once($_SERVER['DOCUMENT_ROOT'].'/local/classLoader.php'); session_start(); $page = new $_REQUEST['page'];
I have previously been using an [if / else if / else] block to test in each possible folder but this seems inefficient, so I'm looking for a better way to have the autoloader look in many different locations.
Here's my latest failure, which doesn't manage to find any of the classes requested and just outputs an exception for each ending up with a fatal error!:
function classToPath($class) { $path = str_replace('_', '/', $class) . '.php'; return $path; } function autoloadController($class) { echo 'LoadController'.'
'; $root = '/controller/classes/'; $pathtoclass = $root.classToPath($class); try { if( file_exists($pathtoclass) ) require_once($pathtoclass); else throw new Exception('Cannot load controller '.$class); } catch(Exception $e) { echo 'Controller exception: ' . $e->getMessage() . "
"; } } function autoloadModel($class) { echo 'LoadModel'.'
'; $root = '/model/classes/'; $pathtoclass = $root.classToPath($class); try { if( file_exists($pathtoclass) ) require_once($pathtoclass); else throw new Exception('Cannot load model '.$class); } catch(Exception $e) { echo 'Model exception: ' . $e->getMessage() . "
"; } } function autoloadView($class) { echo 'LoadView'.'
'; $root = '/view/classes/'; $pathtoclass = $root.classToPath($class); try { if( file_exists($pathtoclass) ) require_once($pathtoclass); else throw new Exception('Cannot load view '.$class); } catch(Exception $e) { echo 'View exception: ' . $e->getMessage() . "
"; } } spl_autoload_register('autoloadController'); spl_autoload_register('autoloadModel'); spl_autoload_register('autoloadView');
I was also wondering exactly how the URL to folder/class mapping should work:
- i.e. URL:
/foldername/classname
mod_rewritten to foldername_classname
;with a class filename of
classname.php
under the foldername
folder;and a php class definition of
class foldername_classname extends another_class { etc.
Is this the correct method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
而是偶然,与 另一件事,我找到了解决我的问题的< /a>!
因此,回答我自己的问题(并希望对其他人有所帮助):
ie
.../not-this-part/but-this
例如
.../folder/subfolder/class
例如
classfolder_subfolder_class {
然后,我为每个类系统(模型、视图和控制器)编写了一个函数,并使用
spl_autoload_register()
将每个函数注册为 <代码>__autoload函数。因此...对于那些想了解 mod_rewrite 部分的人...
Rather by chance, relating to a different matter, I found the solution to my problem!
So to answer my own questions (and hopefully help others):
i.e.
.../not-this-part/but-this
e.g.
.../folder/subfolder/class
e.g.
class folder_subfolder_class {
I then wrote a function for each class system (model, view & controller) and used
spl_autoload_register()
to register each function as an__autoload
function. Thus...For those wondering about the
mod_rewrite
part...