我正在使用角度js调用动作函数。我调用action函数并正确接收参数,但是当我尝试响应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" : "application\json",
"Content-Type" : "application\json"
},
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 \File\LogWriter();
$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
]);
}
答案 0 :(得分:3)
我找到了解决方案!如果您希望能够从动作函数返回ViewModel或JsonModel,则必须按照应用程序的每个模块中的后续步骤来响应ViewModel或JsonModel
<强>首先:强> 在/projectName/module/Application/config/module.config.php
中return [
//...
'view_manager' => [
//...
'strategies' => [
'ViewJsonStrategy',
],
],
];
第二名:在/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('Zend\View\View');
$jsonStrategy = $locator->get('ViewJsonStrategy');
// Attach strategy, which is a listener aggregate, at high priority
$jsonStrategy->attach($view->getEventManager(), 100);
}
最后,我必须说函数registerJsonStrategy(MvcEvent $ e)中的最后一行代码 $ jsonStrategy-&gt; attach($ view-&gt; getEventManager(),100); 是...
$view->getEventManager()->attach($jsonStrategy, 100);
处查看此信息
Bur这行代码返回给我这个错误:
[Sat Apr 29 00:23:53.416382 2017] [:error] [pid 21286] [客户 127.0.0.1:55362] PHP致命错误:未捕获TypeError:传递给Zend \ EventManager \ EventManager :: attach()的参数2必须是可调用的, 给定的整数,调用 第63行/var/www/html/31juegos/module/Application/src/Module.php 并定义于 /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-&GT;附加(对象(的Zend \视图\策略\ JsonStrategy), 100)\ n#1 /var/www/html/31juegos/vendor/zendframework/zend-eventmanager/src/EventManager.php(322): 应用程序\模块 - &GT; registerJsonStrategy(对象(的Zend \的mvc \ MvcEvent))\ N#2 /var/www/html/31juegos/vendor/zendframework/zend-eventmanager/src/EventManager.php(171): 的Zend \ eventmanager进行\ EventManager-&GT; triggerListeners(对象(的Zend \的mvc \ MvcEvent))\ N#3 /var/www/html/31juegos/vendor/zendframework/zend-mvc/src/Application.php(367): 的Zend \ eventmanager进行\ EventManager-&GT; triggerEvent(对象(的Zend \的mvc \ MvcEvent))\ N#4 /var/www/html/31juegos/vendor/zendframework/zend-mvc/src/Application.php(348): 泽在 /var/www/html/31juegos/vendor/zendframework/zend-eventmanager/src/EventManager.php 在第185行
所以,我必须改变这行代码
$view->getEventManager()->attach($jsonStrategy, 100);
通过另一行代码:
$jsonStrategy->attach($view->getEventManager(), 100);
错误已修复!!!
希望对某人有所帮助!!!