New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json view is not rendered if debug is false #6122

Closed
rrd108 opened this Issue Mar 20, 2015 · 6 comments

Comments

Projects
None yet
4 participants
@rrd108
Copy link

rrd108 commented Mar 20, 2015

CakePHP 3

I have an autocompleter input whih calls a controller method via json (jquery)

If debug is on it works perfectly, if debug is set to false it fails with 500 error.
Request headers are the same.

It seems if debug set to true cake renders /src/Template/Exampes/json/example.ctp but if it set to false cake tries to render /src/Template/Exampes/example.ctp - so not the json view. As this view is not exist I get the 500 error.

The response header gives an application/json type if debug is true and text/html if debug is false.

@Xety

This comment has been minimized.

Copy link
Contributor

Xety commented Mar 20, 2015

I had an error like that also.
Can you post the code from your controller please ?

@rrd108

This comment has been minimized.

Copy link
Author

rrd108 commented Mar 20, 2015

@Xety this is the controller

public function search(){
   $query = $this->Zips->find()
         ->select(['id', 'zip', 'name'])
         ->where(['zip LIKE "'.$this->request->query('term').'%"'])
         ->orWhere(['name LIKE "'.$this->request->query('term').'%"']);

   foreach($query as $row){
      $result[] = array('value' => $row->id,
                    'label' => $row->zip . ' ' . $row['name']
                    );
   }

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

This comment has been minimized.

Copy link
Member

ceeram commented Mar 20, 2015

Are you including RequestHandler component in your (App)Controller and have Router::extensions(['json']); in your routes.php?

@Xety

This comment has been minimized.

Copy link
Contributor

Xety commented Mar 20, 2015

Why you don't use the _serialize option ?

In config\routes.php (After all use statements) add Router::extensions(['json']);:

<?php

use Cake\Core\Plugin;
use Cake\Routing\Router;

Router::extensions(['json']);

Router::scope('/', function ($routes) {
//...
public function search() {
    $query = $this->Zips->find()
        ->select(['id', 'zip', 'name'])
        ->where(['zip LIKE "' . $this->request->query('term') . '%"'])
        ->orWhere(['name LIKE "' . $this->request->query('term') . '%"']);

    $json = [];

    foreach ($query as $row) {
        $json[] = [
            'value' => $row->id,
            'label' => $row->zip . ' ' . $row['name']
        ];
    }

    $this->set(compact('json'));

    $this->set('_serialize', 'json');
}

With that you don't need to create the view search.ctp and you need to add the component RequestHandler in your controller :

public $components = [
    'RequestHandler'
];

//Or

public function initialize() {
    parent::initialize();
    $this->loadComponent('RequestHandler');
}

Documentation about it : http://book.cakephp.org/3.0/en/views/json-and-xml-views.html

@dereuromark dereuromark added this to the 3.0.0 milestone Mar 20, 2015

@ceeram

This comment has been minimized.

Copy link
Member

ceeram commented Mar 20, 2015

@rrd108 add extensions as i described to your routes.php and it will work:
https://github.com/rrd108/sanga/blob/master/config/routes.php
I noticed you do have RH component enabled.
Alternatively you could indeed skip view files using _serialize, which is often easier, although not required

@rrd108

This comment has been minimized.

Copy link
Author

rrd108 commented Mar 20, 2015

@ceeram @Xety Thanks. Now it works. Still this behaviour is strange.

@rrd108 rrd108 closed this Mar 20, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment