ZF2:在对象上下文中使用$ this当访问控制器内的其他方法时 [英] ZF2: Using $this when not in object context When accessing other method inside controller

查看:154
本文介绍了ZF2:在对象上下文中使用$ this当访问控制器内的其他方法时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 ZF2:将事件附加到另一个控制器的Action并获取服务定位器,我将一个事件附加到控制器的操作上,如下所示

Based on reply from ZF2: Attach event to another Controller's Action and get Service Locator, I am attaching an event to a controller's action like this

$loader = $sl->get('ControllerLoader');
$loginController = $loader->get('B\Controller\LoginController');

$sharedEventManager->attach('A\Controller\LoginController',
                            'checkme.post',
                            array($loginController, 'loginAction'),
                            100); 

当事件触发时,它进入loginAction的B控制器。当我尝试调用B\LoginController的其他方法时,在loginAction里面,我收到一个错误

When event fire, It goes inside loginAction of "B"'s controller. Inside loginAction when I try to call other method of B\LoginController, then I get an error of


致命错误:使用$ this不在
中的对象上下文C:\wampp\www\Zend2\module\B\src\B\Controller\LoginController.php
Blockquote
第60行

Fatal error: Using $this when not in object context in C:\wampp\www\Zend2\module\B\src\B\Controller\LoginController.php Blockquote on line 60

控制代码:

<?php

namespace B\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Soap\Client;

class LoginController extends AbstractActionController {

    public function loginAction($e) {
       $user_id = $e->getParam('user_id');
       $config = $e->getParam('config');

       if($user_id){
            $client = $this->getSoapClient($config);
            $params = $this->enrolmentRequestParams($user_id);
            $response = $client->getEnrolment($params);
            $course_enrolment = $response->enrolment_list->value->course_enrolment_list->course_enrolment;
            $this->processEnrolmentData($course_enrolment);
       }
    }


    public function getSoapClient($config){
        $options = array(
                    'soap_version'=> $config['webservice_config']['soap_version'],
                    'location' => $config['webservice_config']['location']
                   );

        $wsdl = $config['webservice_config']['wsdl'];

        $client = new \Zend\Soap\Client($wsdl, $options);
        return $client;
    }

    public function processEnrolmentData($course_enrolment){

        foreach($course_enrolment as $ce){
                // some code here

            }

    }

    public function enrolmentLookUp($e){

      //some code here

    }

    public function enrolmentRequestParams($sid){
        //some code here
    }

    public function  emptyObject($theObject){
        $tmp = (array) $theObject;

        if(empty($tmp)) {
            return true;
        }else{
            return false;
        }
    }

    public function validVal($uos){

        if(!empty($uos) && !is_null($uos) && isset($uos)){
            return true;
        }else{
            return false;
        }
    }

   public function getSemesterNo($value){
        return preg_replace("/[^0-9]/","",$value);
    }

}

?>

请在这方面帮忙。

推荐答案

如果您将数组作为回调传递,则该类方法称为静态方式(即相当于您执行的LoginController ::则loginAction(e)中)。没有 $ this ,因为该类的实例上没有调用该函数。

If you pass an array as the callback, the class method is called statically (i.e. equivalent to you doing LoginController::loginAction(e)). There is no $this, because the function is not being called on an instance of the class.

它看起来像你在控制器类中的大部分逻辑一样不应该在控制器中。我建议将其移动到一个单独的类,然后您可以调整以更好地处理静态方法调用。

It looks like most of the logic you have in the controller class shouldn't be in a controller at all. I'd suggest moving it to a separate class which you can then adjust to better handle static method calls.

这篇关于ZF2:在对象上下文中使用$ this当访问控制器内的其他方法时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆