蛋糕php ajax布局缓存

时间:2014-04-15 14:29:12

标签: php ajax cakephp caching layout

在任何ajax请求之后,所有后续非ajax请求都返回ajax布局而不是默认布局。

观测值:

  • 仅限于生产环境。
  • Configure :: write('Cache.disable',true); //没有任何效果!
  • Cake Version 2.4.4
  • 在20~30秒后,将呈现布局(默认)。
  • config / core.php完全相同。

我不是没有原因,我放松了8个小时,任何提示?

控制器(但任何控制器上的任何ajax都会导致问题:

<?php

App::uses('AppController', 'Controller');

class NewslettersController extends AppController
{

public $paginate = array(
    'limit' => 20,
    'paramType' => 'querystring',
    'order' => array(
        'Newsletter.created' => 'DESC'
    )
);

public function beforeFilter()
{
    parent::beforeFilter();
    $this->Auth->allow(array('add','push'));
}

public function admin_index()
{
    $this->set('dataGrid', $this->paginate('Newsletter'));
}

public function admin_view($id = null)
{
    $this->Newsletter->id = $id;
    $result = $this->Newsletter->read();
    if (!$result) {
        $this->setFlashMessage('Cadastro não encontrado', 'error', array('action' => 'index'));
        return false;
    }

    $this->set('dataGrid', $result);
}

public function admin_delete($id = null)
{
    $this->Newsletter->id = $id;

    if (!$this->Newsletter->exists()) {
        $this->Session->setFlash('O item solicitado não foi encontrado!', 'alert_error');
        $this->setFlashMessage('O item solicitado não foi encontrado!', 'error', array('action' => 'index'));
    }

    try {
        if ($this->Newsletter->delete($id, false)) {
            $this->setFlashMessage('Item excluído com sucesso!', 'success', array('action' => 'index'));
        }
    } catch (Exception $e) {
        $this->setFlashMessage('Não foi possivel excluir este item pois existem itens atrelados a ele', 'error', array('action' => 'index'));
    }
}

public function admin_search()
{
    $this->autoRender = false;
    $conditions = null;
    if (isset($this->request->query['search']) && !empty($this->request->query['search'])) {
        $conditions[] = array(
            'OR' => array(
                'Newsletter.name LIKE' => '%' . $this->request->query['search'] . '%',
                'Newsletter.email LIKE' => '%' . $this->request->query['search'] . '%',
            )
        );
        $this->paginate['conditions'] = $conditions;
        $this->set('dataGrid', $this->paginate());
        $this->render('admin_index');
    }
}

//######################
//# FRONTEND           #
//######################

public function push()
{

    if($this->Newsletter->save($this->request->data))
    {
        $response = array("result"=>true,"id"=>$this->Newsletter->id);
    }
    else
    {
        $response = array("result"=>false,"errors"=>$this->Newsletter->validationErrors);
    }

    return new CakeResponse(array("body" => json_encode($response),"type" => "json"));

}

}

?>

1 个答案:

答案 0 :(得分:0)

您必须在方法中区分ajax的布局,请在上面的代码中进行以下更改:

...
class NewslettersController extends AppController
{

   public layout = 'default';
...

   public function push() // if this is ajax function
   {
      $this->layout = 'ajax'; //put this for ajax functions only
      ....
   }

...
}

希望它有所帮助!