Welcome Guest, Not a member yet? Register   Sign In
View will not display but will execute if no index() in controller


I've just found this out:

If there is no index() function in a controller, even though it will never be called or there is no need to ever call it, whatever view is loaded will execute PHP but will not display anything.


- Make a controller
- Make it accessible by routes if necessary
- Do not give it an index()
- Do give it some other method that you call from the controller's constructor
- In that method, do a $this->load->view('yourviewfile');
- In yourviewfile.php , put some PHP like header("Content-type: text/xml");
- Put any kind of content you like after that.

What happens?

- The controller will fire
- The constructor will call the method
- The view in the method will be loaded
- The PHP in the view will be executed (browser sees XML header)
- The content in the view will never show up in the browser, even in Firebug or whatever debugger you use

Now, I can understand that it might have been illogical on my part not to have an index() in a controller. I was choosing what function in the controller to call based on a $_POST routine. I guess I should've put that in the index() instead of the controller's constructor.

But I do think its very illogical for the code in the controller to able to call a function, which in turn does a load->view, of which the PHP does execute but the markup or text does not show.

Agreed? Is this a bug?



[eluser]Sean Gates[/eluser]
So, what you're saying is that the controller's someOther() method, although never called, and if index() is missing, still generates output?

-- Sean

No, I am calling the method that loads the view, from the constructor.

The view's output doesn't show but my header("Content-type: text/xml"); in that view file does work, as the browser (Safari and Firefox) tries to interpret XML.

[eluser]Sean Gates[/eluser]
Well, yeah. If you're calling that view it will run and should display.

You actually do want the output to show, correct?

(With no code examples it's hard to visualize).

Theme © iAndrew 2016 - Forum software by © MyBB