• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I replace front-end views with JSONs in Codeigniter 3?

#8
(08-21-2019, 01:21 PM)ciadmin Wrote:
(08-20-2019, 09:54 PM)Ajax30 Wrote:
(08-20-2019, 11:21 AM)ciadmin Wrote: What about something like https://www.codeigniter.com/user_guide/l...ntent_type

Instead of loading any view at all, each controller method would build its "view data" as $data, and then
$this->output->set_content_type('application/json')->set_output(json_encode($data));

How would that code look exactly, in the context of my Posts controller? For the index() method, for instance.

In your Posts::index() ...

Old code
PHP Code:
public function index() {

   $data['something'] = 'whatever is appropriate';
   $this->load->view('myhomepage',$data);


would be exactly replaced by
PHP Code:
public function index() {
   $data['something'] = 'whatever is appropriate';
   $this->output->set_content_type('application/json')->set_output(json_encode($data));

 
Great. It works as desired. 

How can I make the json pretty, from the controller?
Reply


Messages In This Thread
RE: How do I replace front-end views with JSONs in Codeigniter 3? - by Ajax30 - 08-22-2019, 12:32 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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