CodeIgniter Forums
[SOLVED] My_Controller ? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: [SOLVED] My_Controller ? (/showthread.php?tid=54576)

Pages: 1 2


[SOLVED] My_Controller ? - El Forum - 09-14-2012

[eluser]solid9[/eluser]
Hi guys

Is creating My_Controller is good only for extending Native Libraries?

Or can I use it as ordinary controller?

What's the right way to name the My_Controller file?

Thanks in advanced.



[SOLVED] My_Controller ? - El Forum - 09-14-2012

[eluser]solid9[/eluser]
This is what I did so far,

Code:
class My_mother extends CI_Controller
{
  function __construct()
  {
   parent::__construct();

  }
  
  function index()
  {
   echo 'Mother ship';
  }
}

And I name the file mother.php

But it show some error and the error is,
Code:
Message: Cannot modify header information - headers already sent by (output started at /home/struhi/public_html/thefml.org.uk/secret/application/controllers/mother.php:13)






[SOLVED] My_Controller ? - El Forum - 09-14-2012

[eluser]solid9[/eluser]
I'm trying to implement this,
http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY




[SOLVED] My_Controller ? - El Forum - 09-14-2012

[eluser]qcsites[/eluser]
You should follow the directions exactly in the link exactly. The controller needs to be named MY_Controller in order for it to be extended.

You should then return the data you will want to use in controllers you extend from the MY_Controller.

So your MY_Controller file name would be MY_Controller.php as shown.

Then the extended controllers operate as normal controllers, but for things you need site wide in all controllers, you can put that in the MY_Controller. Stuff like session management, site settings stuff etc.


[SOLVED] My_Controller ? - El Forum - 09-14-2012

[eluser]solid9[/eluser]
Thanks dude I did not noticed that.

anyway here is the updated class,
Code:
class MY_Primary extends CI_Controller
{
  function __construct()
  {
   parent::__construct();

  }
  
  function index()
  {
   echo 'Primary';
  }
}

I also saved as MY_Primary.php

And I called it in the URL,
Code:
http://thefml.org.uk/secret/primary/

also like this,
Code:
http://thefml.org.uk/secret/primary/index/

and also like this,
Code:
http://thefml.org.uk/secret/index.php/primary/

But nothing happened.

It says,
Code:
404 Page Not Found

why?




[SOLVED] My_Controller ? - El Forum - 09-14-2012

[eluser]solid9[/eluser]
I tried calling it in the URL like this below,
Code:
http://thefml.org.uk/secret/MY_Primary/index/

Is that the right way to call it in the URL?

If yes, then I have error below,
Code:
Message: Cannot modify header information - headers already sent by (output started at /home/struhi/public_html/thefml.org.uk/secret/application/controllers/MY_Primary.php:19)





[SOLVED] My_Controller ? - El Forum - 09-14-2012

[eluser]solid9[/eluser]
Problem solved.

Solution,
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Primary extends CI_Controller
{
  function __construct()
  {
   parent::__construct();

  }
  
  function index()
  {
   echo 'Primary';
  }
  
  function play()
  {
   echo 'Hello Foot Ball';
  }
  
}

I have forgot this part,
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');






[SOLVED] My_Controller ? - El Forum - 09-15-2012

[eluser]CroNiX[/eluser]
You still didn't do it right because you didn't follow the guide exactly, which is important.

Its called MY_Controller, not MY_Primary, and it gets put in /application/core/MY_Controller.php

Also, you don't call it directly. It is automatically loaded when you call a controller. Its just a way to add methods that will be available to all controllers, that extend MY_Controller.

Then, for your own controllers, they extend MY_Controller, instead of CI_Controller.

If you follow the guide exactly, it will work. If you alter it, it won't.


[SOLVED] My_Controller ? - El Forum - 09-15-2012

[eluser]CroNiX[/eluser]
You will also have problems like you are having with the "cannot modify headers" if you echo from a base controller. That should be done from views.


[SOLVED] My_Controller ? - El Forum - 09-15-2012

[eluser]CroNiX[/eluser]
/application/core/MY_Controller.php
Code:
class MY_Controller extends CI_Controller {
  function __construct()
  {
    parent::__construct();
  }

  //this will be available in all of your controllers now
  function universal_method()
  {
    return 'hello';
  }
}

now one of your controllers (extends MY_Controller)
/application/controllers/something.php
Code:
class Something extends MY_Controller {
  function __construct()
  {
    parent::__construct();
  }

  function show_message()
  {
    //access the method we defined in MY_Controller
    $data['message'] = $this->universal_message(); //now is 'hello'
    $this->load->view('some_view', $data);
  }
}

View
/application/views/some_view.php
Code:
<?php echo "Message is: $message"; ?>

access via url:
http://yoursite.com/something/show_message