Note that there are some explanatory texts on larger screens.

plurals
  1. POCodeIgniter - Replace redunant JSON conversion
    primarykey
    data
    text
    <p>I recently started using Codeigniter after having a structural problem in one of my Ajax-heavy applications. (You can read up on it if you want in <a href="https://stackoverflow.com/questions/16437102/best-way-to-set-up-my-programs-code">my previous question</a>)</p> <p>I have a fairly short question. Currently I am making a lot of Ajax requests to different controllers. I open the controllers like this:</p> <pre><code>public function __construct() { parent::__construct(); $this-&gt;output-&gt;set_content_type('application/json'); } </code></pre> <p>And at the end of every function I do the following:</p> <pre><code>$this-&gt;returnValue['result'] = "ReturnedInfo"; $this-&gt;returnValue = json_encode($this-&gt;returnValue); $this-&gt;output-&gt;set_output($this-&gt;returnValue); </code></pre> <p>The code is pretty clear in itself, but I don't want to keep repeating myself. The codeigniter manual says to do the following:</p> <pre><code>$this-&gt;output -&gt;set_content_type('application/json') -&gt;set_output(json_encode(array('foo' =&gt; 'bar'))); </code></pre> <p>But I would still be repeating myself. Also, I don't want to add a function to every controller that does this, even if it does decrease redundancy.</p> <p>Since all of my controllers return JSON, is there a way to set this globally in a config file maybe, or in any other way?</p> <p><strong>TL;DR</strong> I have this same piece of code in every controller/function. Since the output type is always the same, just not the result, is there a way to automate this process across every controller/function?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload