CodeIgniter Forums
Direct echo in Controller - bad parctice? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: Direct echo in Controller - bad parctice? (/showthread.php?tid=1680)



Direct echo in Controller - bad parctice? - ChrisRitson - 04-01-2015

I'm quite new to CodeIgniter and have been putting together a very small website which is directly copied from some static PHP that I maintain. Following the tutorial (version 2) and then moving on to the general topics led me to put a few direct echo statements in my default controller in order to attempt to add some CSS, metadata and third party JavaScript to the resulting page's
Code:
<head></head>

Everything started to come out in the wrong order. Other MVC tutorials found by Google imply that it is a bad idea for a controller to produce output in this way, and sure enough when I swapped over to using
Code:
$this->output->append_output()
everything started to work a lot more neatly. Is this the right approach? If so, I think it would be helpful to update the documentation for controllers to make this clear.

Chris.


RE: Direct echo in Controller - bad parctice? - gabe3886 - 04-01-2015

Given that CodeIgniter is a MVC framework implies that output code should generally be avoided in anything other than the V part of it all.

There are some cases where it might be okay to put output directly from the controller, but where possible it should all be done as part of a view.


RE: Direct echo in Controller - bad parctice? - gadelat - 04-01-2015

If you are new to this problematic, you need to read these pages of documentation: MVC and static pages.


RE: Direct echo in Controller - bad parctice? - CroNiX - 04-01-2015

CI's views are captured with output buffering and sent to the browser after the controller/method is totally done being processed. Echoing from controllers will bypass that and send data FIRST, followed by any views being shown. All output, except general debugging while in development of course, should be sent through a view, not echoing from a controller.

Really the only exception to that is sending back json or other data. Then you'd echo it from the controller and not use a view (in most cases)


RE: Direct echo in Controller - bad parctice? - gabe3886 - 04-02-2015

(04-01-2015, 06:54 AM)CroNiX Wrote: Really the only exception to that is sending back json or other data. Then you'd echo it from the controller and not use a view (in most cases)

I'm glad someone with more experience in CI than me said this. That's the only time I've echo'd directly from a controller, and was wondering if it really needed its own view for JSON output.


RE: Direct echo in Controller - bad parctice? - gadelat - 04-02-2015

Direct echoing is also done during debugging in traditional way.


RE: Direct echo in Controller - bad parctice? - RWCH - 04-02-2015

You can parse JSON data perfectly fine in a view, and there is no need to do it in a controller.  

Just pass your JSON string to the view like you normally do with data you are going to show.

In your controller:

PHP Code:
<?php 

$tennisArray 
= array('Djokovic' => 1'Federer' => 2'Nadal' => 3'Murray' => 4);
$data['tennisPlayers'] = json_encode($tennisArray);
$this->load->view("some_page"$data);

?>

In your view:

PHP Code:
<?php

echo $tennisplayers//your JSON encoded string 

?>

Which is similar to:

PHP Code:
<?php 

echo {"Djokovic":1,"Federer":2,"Nadal":3,"Murray":4};

?>

You can use this with AJAX or a normal page request and use it as you like.

If you want to send back XML or a PDF, you have to use another header (not HTML) and you cannot simply echo it in a view.


RE: Direct echo in Controller - bad parctice? - kenji - 04-02-2015

or use output class 

http://www.codeigniter.com/userguide3/libraries/output.html?highlight=output#CI_Output::set_output

http://php.net/manual/fr/json.constants.php


PHP Code:
$response = array('status' => 'OK');

$this->output
        
->set_status_header(200)
 
       ->set_content_type('application/json''utf-8')
 
       ->set_output(json_encode($responseJSON_PRETTY_PRINT JSON_UNESCAPED_UNICODE JSON_UNESCAPED_SLASHES))
 
       ->_display();
exit; 


result : 
Code:
{
   "status": "OK"
}



RE: Direct echo in Controller - bad parctice? - ChrisRitson - 04-03-2015

Thanks for confirming my error - all of you. Can someone with the appropriate authority update http://www.codeigniter.com/userguide3/general/controllers.html with this or a similar suitable warning, please?

Chris.

(04-01-2015, 06:54 AM)CroNiX Wrote: CI's views are captured with output buffering and sent to the browser after the controller/method is totally done being processed. Echoing from controllers will bypass that and send data FIRST, followed by any views being shown. All output, except general debugging while in development of course, should be sent through a view, not echoing from a controller.

...



RE: Direct echo in Controller - bad parctice? - CroNiX - 04-03-2015

Why does there need to be a warning? In MVC, you use VIEWS, so it should be understood. None of the docs or tutorials show echoing from a controller.