【问题标题】:CakePHP 3: Global variable in beforeRender() overwriting others variables in JSON viewCakePHP 3:beforeRender() 中的全局变量覆盖 JSON 视图中的其他变量
【发布时间】:2016-11-29 22:27:01
【问题描述】:

我使用的是 CakePHP 3.3.9。好吧,我的问题是:当我向操作发送 ajax 获取请求时,它不起作用。我正在使用“前缀路由”。我发现了很多关于如何在 CakePHP 3 中处理 Ajax 的帖子,但就我而言,没有任何效果!

这是我的代码:

// routes.php

Router::extensions('json');
...
Router::prefix('myagendas', function ($routes) {
 $routes->connect('/agendas', ['controller' => 'agendas', 'action' => 'test']);
 $routes->fallbacks(DashedRoute::class);  
});  

// AppController.php  

$this->loadComponent('RequestHandler');  

.

// AgendasAppController.php  

namespace App\Controller\Agendas;  
use App\Controller\AppController;
use Cake\Event\Event;  

class AgendasAppController extends AppController {
    // there's no code here yet.. I'm just extending the AppController that has all the configs..
}

.

// AgendasController.php  
...Extending AgendasAppController.php...

public function test() {                
    $test = 'not ok';
    if ($this->request->is('ajax')) {
        $test = 'ok';
    }

    $this->set(compact('test', $test));
    $this->set('_serialize', ['test']);
}  

.

// scripts.js  

$('#btnAdd').click(function () {        
    $.ajax({
        type: 'GET',
        url: './myagendas/agendas/test', //myagenda is a prefix
        dataType: 'json',
        success: function (data) {
            console.log(data);
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log(jqXHR);
        }
    });
});  

我在 JS 控制台中收到此错误:

Fatal error</b>:  Call to a member function config() on a non-object in AppController.php   

更新:
我发现了错误!
在 beforeRender() 的 AppController 中,我设置了一个全局变量,检查用户是否已登录。问题是:当我在 Ajax 中接收数据时,无法从请求中获取变量,因为 beforeRender 中的全局变量当我访问 .json 扩展名时正在覆盖其他变量。
.

// AppController.php
if ($this->request->session()->read('Auth.User.role') == 'admin') {
            $this->set('loggedIn', true);
            $this->set('_serialize', ['loggedIn']);
        } else {
            $this->set('loggedIn', false);
            $this->set('_serialize', ['loggedIn']);
        } 

我需要什么:
现在我需要找到一种方法来从视图中访问 .json 扩展名并从视图中获取全局变量 + 其他变量。全局变量不应覆盖其他变量。

谢谢!

【问题讨论】:

  • 每当收到错误时,请始终发布complete错误,即包括full堆栈跟踪(最好是从日志中以正确可读的方式复制),即使问题对于熟悉 CakePHP 的人来说可能很明显!
  • @ndm 这是日志控制台中出现的唯一错误。它出现了两次,仅此而已。我真的不知道发生了什么。
  • CakePHP 中的所有错误都被记录,包括堆栈跟踪(假设 Error.trace 选项已启用,这是 CakePHP 应用程序模板中的默认选项),所有 PHP 错误都显示完整的文件路径和行号。
  • @ndm 问题已更新!你有什么想法吗?

标签: javascript php jquery ajax cakephp


【解决方案1】:

你可能在这里做错了:

class AgendasAppController extends AppController {
// there's no code here yet.. I'm just extending the AppController that has      all the configs..
}

应该是这样的:

class AgendasController extends AppController {

}

【讨论】:

  • 将您的网址更改为 './myagendas/agendas/test.json' 并检查 if($this->request->is("get")) 而不是控制器中的 ajax;
  • 仍然无法正常工作,因为 AppController 中的全局变量 beforeRender() 方法正在覆盖页面中对其他变量的访问。
【解决方案2】:

问题是 AppController 中的变量“loggedIn”正在使用“_serialize”并覆盖其他变量,因为它在 beforeRender() 方法中是全局的。我只需要设置一个没有'_serialize'的普通变量,现在它工作正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-13
    • 1970-01-01
    • 1970-01-01
    • 2011-11-20
    相关资源
    最近更新 更多