ZF3:如何从动作函数响应JSON



我正在使用Angular JS来调用动作函数。我调用操作功能并正确接收参数,但是当我尝试响应JSONMODEL时,我不知道为什么Zend Framework响应ViewModel。我认为,因为Zend Framework无法检测到Angular JS的Ajax调用。那么,我如何打电话给我的动作功能并Zend Framework像Ajax调用一样检测到此调用?

Angular JS:

self.sendData = function(url, data){
    var promise = $q.defer();
    console.log("Dentro de senDAta!!!");
    var config = {
        headers : {
            "Accept"        :   "applicationjson",
            "Content-Type"  :   "applicationjson"
        },
        resposeType : "json"
    };
    $http.post(url, data, config).success(function(response, status, headers, config){
                console.log("dentro de success!!!");
                promise.resolve(response);
            }).error(function(data){
                //Error de sistemas
                console.log("Error en sendData: " + data);
            });
    return promise.promise;        
};  

/application/config/module.config.php

return [
    //...
    'view_manager' => [
        //...
        'strategies' => [
            'ViewJsonStrategy',
        ],
    ],
];

/controller/logincontroller.php

public function loginAction(){
    $request = $this->getRequest();
    $log = new FileLogWriter();
    $log->writeLog(get_class($this) . "::" . __FUNCTION__ . ": Dentro de loginAction()");
    if ($this->getRequest()->isXmlHttpRequest() === true){
        $log->writeLog(get_class($this) . "::" . __FUNCTION__ . ": Llamada hecha por Ajax");
    }else{
        $log->writeLog(get_class($this) . "::" . __FUNCTION__ . ": Llamada no hecha por ajax");
    }
        $params = json_decode(file_get_contents('php://input'),true);
        $email = $params["email"];
        $password = $params["password"];
        $log->writeLog(get_class($this) . "::" . __FUNCTION__ . ": email: " . $email . " password: " . $password);
        $user = new User($email);
        return new JsonModel([
            "result"    => 0
        ]);             
}

我找到了解决方案!!!如果您想从一个操作函数中返回ViewModel或JSONMODEL,则必须在应用程序的每个模块中遵循要响应ViewModel或JSONMODEL

的下一步的步骤

首先:in/projectName/module/application/config/module.config.php

return [
    //...
    'view_manager' => [
        //...
        'strategies' => [
            'ViewJsonStrategy',
        ],
    ],
];

第二: in/projectName/module/application/src/module.php:

public function onBootstrap(MvcEvent $e)
{
    // Register a "render" event, at high priority (so it executes prior
    // to the view attempting to render)
    $app = $e->getApplication();
    $app->getEventManager()->attach('render', [$this, 'registerJsonStrategy'], 100);
}
public function registerJsonStrategy(MvcEvent $e)
{
    $app          = $e->getTarget();
    $locator      = $app->getServiceManager();
    $view         = $locator->get('ZendViewView');
    $jsonStrategy = $locator->get('ViewJsonStrategy');
    // Attach strategy, which is a listener aggregate, at high priority
    $jsonStrategy->attach($view->getEventManager(), 100);
}

最后,我不得不说,代码的最后一行 $ jSonstrategy-> attact($ view-> getEventManager((,100(; in Function regissne jopplaySjsonstrategy(mvcevent $ e(最初是。..

$view->getEventManager()->attach($jsonStrategy, 100);

您可以在https://docs.zendframework.com/zend-view/quick-start/#creating-and-registering-antertering-alternate-mandering-and-rendering-and-response-strategies bur这条代码返回我这个错误:

[SAT 29 00:23:53.416382 2017] [:错误] [PID 21286] [客户 127.0.0.1:55362] PHP致命错误:uncoverch typeError:参数2传递给zend eventManager eventManager :: actact(( 整数给了,呼叫 /var/www/html/31juegos/module/application/src/module.php on Line 63 并定义 /var/www/html/31juegos/vendor/zendframework/zend-eventmanager/src/eventmanager.php:185 nstack 跟踪: n#0 /var/www/html/31juegos/module/application/src/module.php(63(: zend eventManager eventManager-> actact(object(zend view stragity jsonstrategy(,(, 100( n#1 /var/www/html/31juegos/vendor/zendframework/zend-eventmanager/src/eventmanager.php(322(: 应用程序 module-> registerjSonstrategy(object(zend mvc mvcevent(( n#2 /var/www/html/31juegos/vendor/zendframework/zend-eventmanager/src/eventmanager.php(171(: zend eventManager eventManager-> triggerListeners(object(zend mvc mvcevent((( n#3 /var/www/html/31juegos/vendor/zendframework/zend-mvc/src/application.php(367(: zend eventManager eventManager-> triggerevent(object(zend mvc mvcevent(( n#4 /var/www/html/31juegos/vendor/zendframework/zend-mvc/src/application.php(348(: ze in /var/www/html/31juegos/vendor/zendframework/zend-eventmanager/src/eventmanager.php 在第185行

所以,我必须更改此代码

$view->getEventManager()->attach($jsonStrategy, 100);

通过另一项代码:

$jsonStrategy->attach($view->getEventManager(), 100);

,错误已修复!!!

希望它对某人有帮助!

最新更新