Welcome Guest, Not a member yet? Register   Sign In
CI controller function is ignored
#1

[eluser]den-javamaniac[/eluser]
Hi. I'm trying to pass through a CI tutorial (writing a simple forum) and I can't make a controller function called. To be more specific I have this in a controller:

Code:
function  __construct() {
        parent::Controller();
                $this->load->helper('url');
    }

    function index() {
            $data['title'] = "Wlcome to Gossip";
            $data['heading'] = "Welcome to Gossip";
            $data['query'] = $this->db->get('topic')->result();
            $this->load->view('welcome', $data);
    }

        function topic() {
            echo 'testing...';
        }
and when I'm trying to access the /index.php/forum/topic/ uri (Forum is the name of my controller class) it just prints out contents of index() function. Defining next in a controller:

Code:
function _remap($method){
            if ($method == 'topic'){
                $this->topic();
            } else if ($method == 'index'){
                $this->index();
            }
        }
results in only index method name passed. If tested for other controller names result remains the same. Can any one point out what may cause the problem?

NOTE: this issue is also discussed here.
#2

[eluser]erik.brannstrom[/eluser]
I copied your code and tried it myself with the only difference that I only echoed a line of text in the index method. For me, everything worked fine so it seems the problem must be something else. Have you set any routes for example?
#3

[eluser]den-javamaniac[/eluser]
Well, I get a very strange error:

If routes are not set (some default value is not assigned) like this:
Code:
$route['default_controller'] = "";
$route['scaffolding_trigger'] = "";
I can't get to any controller (I've created another one for testing) - it prints out an
Code:
"Unable to determine what should be displayed. A default route has not been specified in the routing file."
error. So this looks like I can only get to some predefined controller index method. Is it possible that some php installation module/param/etc was not defined or alike?
#4

[eluser]erik.brannstrom[/eluser]
That error message is displayed when one is trying to access index.php without a default controller set. You said you still got this message when going to index.php/forum/topic/? Have you created a htaccess file?
#5

[eluser]den-javamaniac[/eluser]
You said you still got this message when going to index.php/forum/topic/? - that's correct.

Yes, I did. It lies in a project root directory (the same as index.php) and has next content:
Code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php$1 [L]
#6

[eluser]erik.brannstrom[/eluser]
You shouldn't use index.php since you are rewriting the URL. Also, for me it worked if I changed the last line to the following, perhaps that will work for you too.
Code:
RewriteRule ^(.*)$ index.php/$1 [L]
#7

[eluser]den-javamaniac[/eluser]
Nope, It didn't help me. Could you provide more details on "You shouldn’t use index.php since you are rewriting the URL."?
#8

[eluser]erik.brannstrom[/eluser]
The .htaccess file redirects all requests to your index.php file. This is done so your URLs will look "cleaner", i.e. instead of www.example.com/index.php/forum you go to www.example.com/forum. Have you tried removing the htaccess file completely and then access index.php/forum/topic?
#9

[eluser]den-javamaniac[/eluser]
If I remove .htaccess completely I can easily go to index.php/forum/ and get a proper content. If I remove default controller in remote I still get the error. In fact that file did most certainly nothing (at list I couldn't see any changes/use it properly) so I'm removing it for now. Any other ideas?
#10

[eluser]erik.brannstrom[/eluser]
To me it seems your problem is the htaccess file. Do some searches on the subject, there are a number of standard files for removing index.php from your URLs. As I said before, your CI-code looks alright (though at the moment there is no reason to have the _remap method). Hope it works out!




Theme © iAndrew 2016 - Forum software by © MyBB