简体   繁体   English

从Cakephp中非关联的控制器访问变量

[英]Access a variable from an non associated Controller in Cakephp

I am developing a quiz using a 'form' in cakephp. 我正在使用cakephp中的“表单”来开发测验。 I have declared a variable within my CourseModules controller ($passMark) where the HR developing the quiz can set the pass percentage the user needs to successfully complete the quiz. 我已经在CourseModules控制器($ passMark)中声明了一个变量,开发测验的HR可以设置用户成功完成测验所需的通过百分比。 I have declared the variable like so: 我已经像这样声明了变量:

            case "Quiz":
            $quiz = $this->CourseModules->FormTemplates->find('list')->where(['active'=>true,'type'=>'Quiz']);
            $passMark = [100=>'100%',90=>'90%',80=>'80%',70=>'70%',60=>'60%',
            50=>'50%',40=>'40%',30=>'30%',20=>'20%',10=>'10%',0=>'0%'];
            $this->set('passMark',$passMark);

            $this->set('quiz',$quiz);
            break;

I then need to access the variable $passMark within my FormsController so that I can check it against another variable ($percCorrect). 然后,我需要在FormsController中访问变量$ passMark,以便可以对照另一个变量($ percCorrect)进行检查。 $percCorrect is declared as so in my Forms Controller: $ percCorrect在我的窗体控制器中声明为:

        $percCorrect = $numberCorrect / $numberOfQuizQuestions * 100;
        $this->set('percCorrect', $percCorrect);

I want to do an if statement so check in if $percCorrect is < $passMark but I'm unsure how to access $passMark because CourseModules isn't associated with Forms Controller. 我想执行一条if语句,因此请检查$ percCorrect是否<$ passMark,但是我不确定如何访问$ passMark,因为CourseModules与Forms Controller没有关联。

I do have another controller called CoursesEnrolledModules that is related so I'm wondering if I can somehow access it through there? 我确实有另一个名为CoursesEnrolledModules的控制器,该控制器与此相关,所以我想知道是否可以通过那里访问它吗?

I have the following code in my FormsController to load the CoursesEnrolledModules: 我在FormsController中有以下代码来加载CoursesEnrolledModules:

        //Check if courses_enrolled_module_id is set
    $courses_enrolled_module_id = $this->request->getQuery('courses_enrolled_module_id');

    //If so make sure it is valid
    if($courses_enrolled_module_id){
        $this->loadModel('CoursesEnrolledModules');
        $coursesEnrolledModule = $this->CoursesEnrolledModules->get($courses_enrolled_module_id,
            ['contain'=>[],
        ]);
        //Pass variable to view so we can show correct back button
        $this->set('coursesEnrolledModule', $coursesEnrolledModule);
        //Also after save we will redirect.
    }

Any time you ask yourself "how do I access one controller from another controller", just stop yourself right there and think about a different option. 每当您问自己“如何从另一个控制器访问一个控制器”时,只需将自己停在那里并考虑其他选择即可。 In this case, you should put your array in a central location accessible by everything that might need it. 在这种情况下,应将阵列放置在中央位置,所有可能需要它的地方都可以使用它。 Include it in the config in your app.php, perhaps, and then reference it with Configure::read(...) anywhere you need it. 可能将其包含在app.php的配置中,然后在需要的任何位置使用Configure :: read(...)进行引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM