如何获取CakePHP中的控制器名称列表以验证用户数据? [英] How do I get a list of Controller Names in CakePHP to validate user data against?
问题描述
我正在为客户端构建一个CakePHP网站,我有一个有趣的问题。他们想添加一个新功能,用户可以使用此类型的网址在网站内创建自己的着陆页: http://mainsite.com/username
。由于这是一个蛋糕网站,有很多值 username
会导致各种各样的路由疯狂,特别是如果他们碰巧选择一个用户名是相同的
I'm building a CakePHP site for a client and I've got a bit of an interesting problem. They want to add a new feature whereby users can create their own landing pages within the site using this type of URL: http://mainsite.com/username
. Since this is a Cake site, there are plenty of values for username
that would result in all sorts of routing craziness, especially if they happened to pick a username that was the same as a controller within the site.
我的做法是这样的:在Users控制器的 beforeValidate
函数中,请根据所有控制器名称的列表检查提供的用户名,如果匹配,则返回 false
。我的问题是 - 我怎么能得到所有的控制器名称的列表(实际上循环的 / app / controllers
中的文件)?
My approach is something like this: in the beforeValidate
function of the Users controller, check the supplied username against a list of all of the controller names and return false
if there's a match. My question is - how can I get a list of all of the controller names (short of actually looping over the files in /app/controllers
)? Is it even possible to do this?
推荐答案
您可以使用
CakePHP 1.x
You could probably use CakePHP 1.x
$Controllers = Configure::listObjects('controller')
CakePHP 2.x
CakePHP 2.x
$Controllers = App::objects('controller');
这篇关于如何获取CakePHP中的控制器名称列表以验证用户数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!