Welcome Guest, Not a member yet? Register   Sign In
Direct echo in Controller - bad parctice?
#1

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.
Reply
#2

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.
Developing mostly on Ubuntu 14.04 in a VM. Probably insane, but it works.
Reply
#3

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

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)
Reply
#5

(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.
Developing mostly on Ubuntu 14.04 in a VM. Probably insane, but it works.
Reply
#6

Direct echoing is also done during debugging in traditional way.
Reply
#7

(This post was last modified: 04-02-2015, 05:50 AM by RWCH.)

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.
Reply
#8

(This post was last modified: 04-02-2015, 07:17 AM by kenji.)

or use output class 

http://www.codeigniter.com/userguide3/li...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"
}
Reply
#9

Thanks for confirming my error - all of you. Can someone with the appropriate authority update http://www.codeigniter.com/userguide3/ge...llers.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.

...
Reply
#10

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




Theme © iAndrew 2016 - Forum software by © MyBB