MVC;任意路由路径级别和参数
我正在使用 PHP 开发一个(哦,不,不是另一个)MVC 框架,主要用于教育目的,但也很有趣和有利可图。
无论如何,我的路由器遇到了一些问题,特别是使用正确的参数路由到正确的路径。现在,我正在研究一个路由器(使用__autoload()
)允许任意长的路由路径:
"path/to/controller/action"
"also/a/path/to/a/controller/action"
路由从application
目录开始,路由路径是本质上与文件系统路径并行:
"/framework/application/path/to/controller.class.php" => "action()"
class Path_To_Controller{
public function action(){}
}
"/framework/application/also/a/path/to/a/controller.class.php" => "action()"
class Also_A_Path_To_A_Controller{
public function action(){}
}
这将允许模块配置文件在应用程序文件系统的不同级别上可用。当然,问题是,当我们引入路由路径参数时,很难区分路由路径的结束位置和路径参数的开始位置:
"path/to/controller/action/key1/param1/key2/param2"
显然会寻找文件:
"/framework/application/path/to/controller/action/key1/param1/key2.class.php"
=> 'param2()'
//no class or method by this name can be found
这不好。当然,现在这听起来像是一个设计问题,但我确信必须有一种干净的方法来规避这个问题。
我最初的想法是测试路由路径的每个级别是否存在目录/文件。
- 如果它命中 1+ 个目录,后跟一个文件,则附加路径组件是一个操作,后跟参数。
- 如果它到达 1+ 个目录并且没有找到文件,则对其进行 404。
但是,这仍然容易错误地查找文件。当然,这可以通过更严格的命名约定和保留某些单词来缓解,但如果可能的话,我想避免这种情况。
我不知道这是否是最好的方法。有没有人以优雅的方式解决了这样的问题?
I'm working on an (oh no, not another) MVC framework in PHP, primarily for education, but also fun and profit.
Anyways, I'm having some trouble with my Router
, specifically routing to the correct paths, with the correct parameters. Right now, I'm looking at a router that (using __autoload()
) allows for arbitrarily long routing paths:
"path/to/controller/action"
"also/a/path/to/a/controller/action"
Routing starts at the application
directory, and the routing path is essentially parallel with the file system path:
"/framework/application/path/to/controller.class.php" => "action()"
class Path_To_Controller{
public function action(){}
}
"/framework/application/also/a/path/to/a/controller.class.php" => "action()"
class Also_A_Path_To_A_Controller{
public function action(){}
}
This will allow for module configuration files to be available at varying levels of the application file system. The problem is of course, when we introduce routing path parameters, it becomes difficult differentiating where the routing path ends and the path parameters begin:
"path/to/controller/action/key1/param1/key2/param2"
Will obviously be looking for the file:
"/framework/application/path/to/controller/action/key1/param1/key2.class.php"
=> 'param2()'
//no class or method by this name can be found
This ain't good. Now this smells like a design issue of course, but I'm certain there must be a clean way to circumvent this problem.
My initial thoughts were to test each level of the routing path for directory/file existence.
- If it hits 1+ directories followed by a file, additional path components are an action followed by parameters.
- If it hits 1+ directories and no file is found, 404 it.
However, this is still susceptible to erroneously finding files. Sure this can be alleviated by stricter naming conventions and reserving certain words, but I'd like to avoid that if possible.
I don't know if this is the best approach. Has anyone solved such an issue in an elegant manner?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,用我自己的建议来回答我自己的问题:
我的自动加载器是最基本的:
它的效果出奇地好。我尚未实现某些检查,例如确保请求的控制器实际上是从我的 Flooid_System_ControllerAbstract 扩展的,但目前,这就是我正在运行的内容。
无论如何,我认为这种方法即使不是全面的改革,也可以从批评中受益。
此后我修改了这种方法,尽管它最终执行了相同的功能。它不实例化控制器,而是传回控制器类名、方法名和参数数组。这一切的核心都在
getVerifiedRouteData()
、verifyRouteParts()
和createParamArray()
中。 我想我想重构或修改这个类。我正在寻找有关在哪里可以优化可读性和可用性的见解。:Well, to answer my own question with my own suggestion:
My autoloader is about as basic as it gets:
This works, surprisingly well. I've yet to implement certain checks, like ensuring that the requested controller in fact extends from my
Flooid_System_ControllerAbstract
, but for the time being, this is what I'm running with.Regardless, I feel this approach could benefit from critique, if not a full blown overhaul.
I've since revised this approach, though it ultimately performs the same functionality. Instead of instantiating the controller, it passes back the controller class name, method name, and parameter array. The guts of it all are in
getVerifiedRouteData()
,verifyRouteParts()
andcreateParamArray()
. I'm thinking I want to refactor or revamp this class though. I'm looking for insight on where I can optimize readability and usability.: