CodeIgniter Forums
Problems with custom controllers - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: Problems with custom controllers (/thread-22350.html)



Problems with custom controllers - El Forum - 09-06-2009

[eluser]Unknown[/eluser]
Hello.
Been planning to code an ACL-supported controller and is honestly quite perplexed.
I have two controllers for testing purposes when I tried extending the default CI controller.
First one:
/application/libraries/MY_Controller.php
Code:
<?php
class MY_Controller extends Controller {
    function MY_Controller() {
        parent::Controller();
    }
}
Second one:
/application/libraries/MY_Testcontroller.php
Code:
<?php
class MY_Testcontroller extends Controller {
    function MY_Testcontroller() {
        parent::Controller();
    }
}

First one works, second one doesn't. Now, can anyone explain why in gods name I can't name my CUSTOM controllers any name I want (accepting the fact that I have to have the prefix MY_ in the front)?
There are naming conventions for the file, the class itself when it comes to capitalization, but why the same name as the original class I am trying to extend?

If this is a gotcha, I am a bit tempted to jump ship in fear of what other gotchas I am to expect.

//Frank


Problems with custom controllers - El Forum - 09-06-2009

[eluser]InsiteFX[/eluser]
Hi,

You can name anythibg you want in code but the filename has to be MY_Controller.
When you do this the your Constructor would be the name you used like below.

Code:
class MY_Testcontroller extends Controller {
    function MY_Testcontroller() {
        parent::MY_Controller();
    }
}

Enjoy
InsiteFX


Problems with custom controllers - El Forum - 09-06-2009

[eluser]Unknown[/eluser]
Thanks but that was not really what I was looking for. The name "MY_Testcontroller" was merely there for demonstrating that anything other than "MY_Controller" doesn't seem to work. I started out with just "ACL_Controller" which ofcourse failed.

Code:
class Testar extends MY_Testcontroller {
    
    function Testar() {
        parent::MY_Testcontroller();
    }
    
    function index() {
        echo "hello";
    }
}
This is what I tried to do, but doesn't really work.


Problems with custom controllers - El Forum - 09-06-2009

[eluser]John_Betong[/eluser]
I have just tried this on my LOCALHOST and it seems to work.
 
 
Code:
// ./controller/c_test.php

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

require APPPATH .'libraries/MY_test.php';

//============================================
class C_test extends MY_test
{
    
   //===============================================================================================
   function index($owner=NULL, $pagename=NULL, $key=NULL)
   {
     echo 'THIS WORKS FINE ON MY LOCALHOST';
   }

}//endclass

// ./libararies/MY_test.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

// http://ellislab.com/forums/viewthread/62859/
class My_test extends Controller
{
    
    // ESSENTIAL TO HAVE A CONTRUCT
        // http://ellislab.com/forums/viewthread/128180/
      function __construct()
    {
      parent::__construct();
      $this->load->helper('url');
    }    


  //==================================================
  function index()
  {
    // this is NEVER EVER called      
    echo __FILE__; die;
  }//endfunc

//endclass
 
 
I reckon that it would be beneficial if CodeIgniter elaborated a little on their help and maybe with the installation supplied some empty classes, models, plugins, etc.