Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在CodeIgniter或其他MVC中处理错误的方法(控制器)参数_Php_Codeigniter_Error Handling - Fatal编程技术网

Php 在CodeIgniter或其他MVC中处理错误的方法(控制器)参数

Php 在CodeIgniter或其他MVC中处理错误的方法(控制器)参数,php,codeigniter,error-handling,Php,Codeigniter,Error Handling,我正在用CodeIgnitier编写代码,但这个问题同样适用于任何MVC框架 我如何抽象逻辑来确定控制器方法的参数是否在采用相同参数的多个方法之间有效 编辑:更多详细信息 例如,在我正在构建的应用程序中,有多个“站点”,不同表中的几乎所有数据都属于一个站点,用户只能查看站点的子集。所以我的很多方法都是从方法($site\u id,…)开始的,所以如果我能有一个pre\u控制器钩子,它可以查看方法的参数,检测参数中是否有$site\u id,以及是否有权限检查,那就太好了 问题是,这些控制器中的所

我正在用CodeIgnitier编写代码,但这个问题同样适用于任何MVC框架

我如何抽象逻辑来确定控制器方法的参数是否在采用相同参数的多个方法之间有效

编辑:更多详细信息

例如,在我正在构建的应用程序中,有多个“站点”,不同表中的几乎所有数据都属于一个站点,用户只能查看站点的子集。所以我的很多方法都是从
方法($site\u id,…)
开始的,所以如果我能有一个pre\u控制器钩子,它可以查看方法的参数,检测参数中是否有$site\u id,以及是否有权限检查,那就太好了


问题是,这些控制器中的所有方法并非都有$site\u id参数,因此我无法在我的CI\u控制器扩展的构造函数中自动执行检查,无法扩展同一控制器,也无法在preDispatch或init方法中执行一些验证


或者将验证抽象到控制器帮助器中,并在两个控制器中使用它

对于CodeIgniter,抽象逻辑的一种方法是创建一个超级类:

然后,所有要共享相同逻辑的控制器都应该扩展MY_控制器而不是CI_控制器

现在,在MY_控制器中,您可以创建“跨多个方法处理错误参数”的函数

更新

Extending the controller would only work if my parameters were the same for each method. I'd like to detect automatically the parameters and perform checks. I suppose a call to a helper might be the best I can do, but Id just like to be able to completely abstract it away from the method. I suppose one option would be to have an array of which controllers and methods have $site_id as their first parameter and have a pre_controller hook to do the checking. But this would mean keeping the array up-to-date which kind of defeats the object of my question which is to make things as DRY as possible!
您的控制器功能参数来自您的路线。e、 g.
mydomain.com/mycontroller/myfunction/param1/param2/
将转到
mycontroller
公共函数myfunction($param1,$param2)

$site\u id
只是变量命名。你不能说出它的名字,即使你可以,我也怀疑,根据变量的命名方式来建立逻辑是不是一个好主意

一个简单的方法就是在每个需要验证的控制器函数中显式调用验证函数(在基本控制器、库或助手中存储一次),从而提供细粒度的控制

更新

Extending the controller would only work if my parameters were the same for each method. I'd like to detect automatically the parameters and perform checks. I suppose a call to a helper might be the best I can do, but Id just like to be able to completely abstract it away from the method. I suppose one option would be to have an array of which controllers and methods have $site_id as their first parameter and have a pre_controller hook to do the checking. But this would mean keeping the array up-to-date which kind of defeats the object of my question which is to make things as DRY as possible!
一般来说,为了获得自动化,你必须灵活地放弃并遵循惯例。 所以,不要走“使用助手”的方式,而要走自动检测的方式。。。创建自己的约定


在基本控制器中,使用$this->uri->segment(n)检查某个URL模式是否存在,如果您知道站点id参数正在传入,并且您可以对其运行自动验证。

mikes方式也有效-您可以使用CI库或帮助器来保留共享参数验证逻辑。是,我扩展了CI_控制器来定义一些默认方法,例如
delete()
。但并非所有方法的参数顺序都相同,或者根本不。。。请参阅我对上述问题的编辑,了解更多详细信息是的,我想你是对的-我只是想变得太懒,但事实上,如果我只调用一个helper函数,我的代码会更清晰,实际上可以通过传递func_get_args()使其变得非常简单还有一个字符串数组,告诉验证助手哪些参数是扩展控制器的参数,只有在每个方法的参数相同的情况下,这些参数才起作用。我想自动检测参数并执行检查。我想对助手的调用可能是我能做的最好的了,但我只是希望能够将它从方法中完全抽象出来。我认为一种选择是使用一个数组,其中控制器和方法的第一个参数是$site\u id,并使用一个pre\u控制器钩子进行检查。但这意味着要保持数组的最新状态,哪种类型的数组会破坏我问题的目标,即使事情尽可能干燥@rgvcorley我对CI的了解还不够,但在确定控制器/操作后,您可能可以连接到路由器的关闭例程,并在其发送之前使用插件进行验证。我知道我可以在调用控制器方法之前连接到核心,但我仍然无法知道哪些方法参数是哪些,因为我无法使用反射来查找参数变量名。我想一种选择是为我的所有db id使用范围,但这似乎很疯狂,并且会导致url出现问题,因为url中包含的int实际上不是记录id