Welcome Guest, Not a member yet? Register   Sign In
Debug is breaking my JSON... How to disable it from some methods ?
#1

(This post was last modified: 10-07-2019, 07:12 PM by Poetawd.)

Hi everyone !

The debug is breaking my JSON return....

Since my app is heavily based on JSON returns, I decided to keep it in a view file.

The problem is that CI is adding a html comment on it !

PHP Code:
    public function jsonFormAdd()
    {

        
$data = array();

        
$returnView view('ExpAdmin/ExpData/Candidates/Json/formAdd'$data);

        return 
$this->response->setJSON($returnView);

    } 

 The json:

Code:
<!-- DEBUG-VIEW START 1 APPPATH/Config/../Views/ExpAdmin/ExpData/Candidates/Json/formAdd.php -->
[{"tabs":false,"titulo":"Adicionar Candidate","method":"post","botoes":true,"id":"novoCandidate","action":"candidates\/processa_formulario_adicao","csrf":0,"csrf_name":0,"classes":["form_input","input"],"sections":[{"fields":[{"tipo":1,"name":"stName","legenda":"Name","ajuda":"Full Name","classes":"","required":true},{"tipo":1,"name":"stEmail","legenda":"Email","ajuda":"","classes":"email ","email":1,"required":true},{"tipo":1,"name":"stAddressOne","legenda":"Address 1","ajuda":"","classes":"","required":true},{"tipo":1,"name":"stAddressTwo","legenda":"Address 2","ajuda":"","classes":""},{"tipo":1,"name":"stCity","legenda":"City","ajuda":"","classes":"","required":true},{"tipo":1,"name":"stPostalCode","legenda":"Postal Code","ajuda":"","classes":""},{"tipo":1,"name":"stCountry","legenda":"Country","ajuda":"","classes":""},{"tipo":1,"name":"stPhone","legenda":"Phone","ajuda":"","classes":""}]}]}]
<!-- DEBUG-VIEW ENDED 1 APPPATH/Config/../Views/ExpAdmin/ExpData/Candidates/Json/formAdd.php -->

How to disable the toolbar there ? Thank you !
Reply
#2

Interesting. That's being added by the debug toolbar. I think you can do 1 of 2 things:

1) Edit app/Config/Toolbar.php and comment out the views collector on line 23
2) You could edit app/Config/Filters.php and change how the toolbar is called. Currently it's a global setting. You could change that to ignore all routes starting in api/* for example.
Reply
#3

I tried to do that... as stated in the userguide....

But it didn't work... I had to change the CI_DEBUG variable in Boot/development.php


PHP Code:
<?php namespace Config;

use 
CodeIgniter\Config\BaseConfig;

class 
Filters extends BaseConfig
{
    
// Makes reading things below nicer,
    // and simpler to change out script that's used.
    
public $aliases = [
        
'csrf'     => \CodeIgniter\Filters\CSRF::class,
        
//'toolbar'  => \CodeIgniter\Filters\DebugToolbar::class,
        
'honeypot' => \CodeIgniter\Filters\Honeypot::class,
    ];

    
// Always applied before every request
    
public $globals = [
        
'before' => [
            
//'honeypot'
            // 'csrf',
        
],
        
'after'  => [
            
//'toolbar',
            //'honeypot'
        
],
    ];

    
// Works on all of a particular HTTP method
    // (GET, POST, etc) as BEFORE filters only
    //     like: 'post' => ['CSRF', 'throttle'],
    
public $methods = [];

    
// List filter aliases and any before/after uri patterns
    // that they should run on, like:
    //    'isLoggedIn' => ['before' => ['account/*', 'profiles/*']],
    
public $filters = [];


PHP Code:
/*
  |--------------------------------------------------------------------------
  | DEBUG MODE
  |--------------------------------------------------------------------------
  | Debug mode is an experimental flag that can allow changes throughout
  | the system. This will control whether Kint is loaded, and a few other
  | items. It can always be used within your own application too.
 */

defined('CI_DEBUG') || define('CI_DEBUG'0); 
Reply
#4

Did you try the other method I mentioned and comment out the view collector in the Toolbar config file? Pretty sure the View lib checks for the existence of that to determine whether to wrap the view in those comments or not.
Reply
#5

That worked as well...

Thank you!

I will turn it back on when I need debug...

BTW, the debug toolbar is AWESOME !
Reply
#6

Glad you like it! Still more I'd like to do with it, but it makes a great start already.
Reply
#7

(10-07-2019, 08:11 PM)kilishan Wrote: Glad you like it! Still more I'd like to do with it, but it makes a great start already.

Heart Heart Heart  LOVE IT !

Just one more quick question...

Do you think that it is a good practice to store and generate the JSON in the view ?

My app doesnt return any HTML, just json. And using a template engine I render the HTML in the frontend.
Reply
#8

I typically delegate views to just HTML. I suppose if it works well for you then great. Seems like it might be challenging to get the JSON returned well. I'd probably just build the array of data to return as JSON in the controller, or another library more likely, then use $this->response->setJSON() to return it. You might also look at the API Response Trait to help with responses.
Reply
#9

For others looking for solution, just define MIME/"Content-Type" for proper response with all content types if similar functions to "setJSON" doesn't exist.

CodeIgniter automatically disables debug bar in dev when you need to output something other than HTML.

PHP Code:
public function MyResponse(){

    
$this->response->setContentType('Content-Type: application/json');

    
// $my_json = ...

    
echo $my_json;


Reply
#10

I know this is old now, but I had a similar problem when creating a CSV output.
I used this code within the controller to remove the DEBUG comment lines:
Code:
$response = view("export/csv", $data);

// remove DEBUG comments from view
$response = preg_replace('#<!--.*-->[\r\n]#', '', $response);

$this->response->setHeader('content-type', 'text/plain;charset=UTF-8');
return $response;
Reply




Theme © iAndrew 2016 - Forum software by © MyBB