• 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Debug is breaking my JSON... How to disable it from some methods ?

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);



 The json:

<!-- 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 !

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.

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;


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' => [
            // 'csrf',
'after'  => [

// 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 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); 

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.

That worked as well...

Thank you!

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

BTW, the debug toolbar is AWESOME !

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

(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.

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.

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;


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.