CodeIgniter Forums
Trouble implementing a new controller in an existing project. - 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: Trouble implementing a new controller in an existing project. (/showthread.php?tid=21050)



Trouble implementing a new controller in an existing project. - El Forum - 07-29-2009

[eluser]henryboy[/eluser]
OK, so I'm new to Codeigniter, but I've been programming for many years.

Here is my issue. I'm working on some existing Codeigniter code, I am trying to implement a new controller but cannot get it working. Right now I have 2 existing controllers that do work. Controller1 is set to default and works fine. The other works by accessing it at site.com/controller2/. So if I copy controller2.php (contents is just a simple hello world index function) to controller3.php, and edit the class name should I not get a working site.com/controller3/? Currently it defaults to a custom file not found page.

Where do I need to look first to find out where things are getting redirected?


Trouble implementing a new controller in an existing project. - El Forum - 07-29-2009

[eluser]jedd[/eluser]
Hi henryboy and welcome to the CI forums.

If you're using the standard, recommended, PHP-4 safe method of constructor names - you'll need to change that line as well as the Class line. Without seeing some code it's hard to offer further insights. It might be a permissions problem (unlikely) or .. well, any other number of unlikely causes.


Trouble implementing a new controller in an existing project. - El Forum - 07-29-2009

[eluser]henryboy[/eluser]
Thanks for the welcome. This is the contents of my non-working controller file: testing.php

Code:
<?php

class Testing extends Controller {

    function index() {
                parent::Controller();
        echo 'Hello World';
    }

}

?>

The working controller is identical with a different filename and class name. Keep in mind the working controller was already existing, I just replaced it with the simple function for testing purposes. Permissions are identical. Not sure what you mean by changing the other line if using PHP-4 safe mode constructors.

Thanks.


Trouble implementing a new controller in an existing project. - El Forum - 07-29-2009

[eluser]jedd[/eluser]
[quote author="henryboy" date="1248916782"]
Code:
class Testing extends Controller {

    function index() {
                parent::Controller();
        echo 'Hello World';
    }
}
[/quote]

I'm not sure why you think the parent::Controller() line should exist in the index() rather than the class's constructor. I suspect you've made some changes from the other controllers without understanding about how constructors work ... ? This is a PHP thing rather than a CI thing, so I'd suggest you read up on the PHP manual about classes/constructors.

You want something like this, btw:
Code:
<?php
class Testing extends Controller {

        function Testing()
        {
             parent::Controller();
        }

        function index()
        {
             echo "Hello sailor";
        }
}


Quote:Not sure what you mean by changing the other line if using PHP-4 safe mode constructors.

With PHP4 (something that CI goes to some effort to maintain compatibility with) you have a constructor that shares the same name (see above) but with PHP5 you can use __construct() - the biggest advantage of this is that you do not have to change your class name in two places whenever you use an existing one as the basis of a new one.


Trouble implementing a new controller in an existing project. - El Forum - 07-29-2009

[eluser]davidbehler[/eluser]
What error are you getting? 404?

Maybe there are some routes in play...check config/routes.php


Trouble implementing a new controller in an existing project. - El Forum - 07-29-2009

[eluser]henryboy[/eluser]
[quote author="jedd" date="1248918476"]
I'm not sure why you think the parent::Controller() line should exist in the index() rather than the class's constructor. I suspect you've made some changes from the other controllers without understanding about how constructors work ... ? This is a PHP thing rather than a CI thing, so I'd suggest you read up on the PHP manual about classes/constructors.
[/quote]

Yeah, I just cut and paste a simple hello world snippet I found online. Since controller1 works and controller2 (testing) does not, there is another issue. I have not made any modifications, I have only inserted the test hello world controllers, which I have modified to your example.

I believe it's something happening before Codeigniter is deciding which controller to use. In other words using the same controller file with the file name and class name modified, site.com/controller1/ works fine and site.com/testing/ does not. It seems to be using the default controller defined in the config instead of 'testing'.


Trouble implementing a new controller in an existing project. - El Forum - 07-29-2009

[eluser]henryboy[/eluser]
[quote author="waldmeister" date="1248918621"]What error are you getting? 404?

Maybe there are some routes in play...check config/routes.php[/quote]

It's a custom page not found message that is generated within the default controller. The only thing defined in routes.php is the default_controller.


Trouble implementing a new controller in an existing project. - El Forum - 07-29-2009

[eluser]jedd[/eluser]
Quote:Yeah, I just cut and paste a simple hello world snippet I found online.

Well, try cutting-n-pasting from the [url="http://ellislab.com/codeigniter/user-guide/general/controllers.html"]CI User Manual section on Controllers[/url] and see if that fails in the same way. If it does, then you're right. If it doesn't, then the problem is the dodgy code you've picked up.

Quote:I believe it's something happening before Codeigniter is deciding which controller to use.

Anything is possible, I suppose. It might be related to the number of days since the Solstice.

Is there an .htaccess file in play here?

If you do a clean CI install in a directory, do things Work As Expected?


Trouble implementing a new controller in an existing project. - El Forum - 07-29-2009

[eluser]henryboy[/eluser]
[quote author="jedd" date="1248920696"]
Is there an .htaccess file in play here?
[/quote]

Doh! That's about the only thing I haven't checked since I initially transferred things. There IS some funky stuff in there that looks like it could be causing the issue. Sorting through this now.

Thanks!