Default Controller Issue

#1
[eluser]TheServant[/eluser]
I have spent the past 3 hours reading posts on these forums from people who seem to have a similar problem but no solution works. Please can someone have a look and see if you can help:
This is my controller:
Code:
class Page extends Controller {
    function Page()
    {
        parent::Controller();
    }
    function index($page='register')
    { ...
With/without any routing or mod_rewrite the address: index.php/page/index/$page works as expected.

When I use mod_rewrite:
Code:
<IfModule mod_rewrite.c>
    # Customized error messages.
    ErrorDocument 404 /index.php

    # Set the default handler.
    DirectoryIndex index.php

    # Various rewrite rules.
    RewriteEngine on
    RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
Almost anything I type will get me the default $page in the controller. I have also tested opening another file with this mod_rewrite code and it works for that, just not CI.
/ -> Default
/page -> Default
/page/ -> Default
/page/index -> Default
/page/index/ -> Default
/page/index/home -> Default
/page/index/asdasd -> Default

I have changed my $config['uri_protocol'] to all of the options with no solution.

My $route['default_controller'] = "page";

I have added another route: $route[':any'] = 'page/index';
With variations:
$route[':any'] = 'page';
$route[':any'] = 'page/';
$route[':any'] = 'page/index';
$route[':any'] = 'page/index/';
$route[':any'] = 'page/index/$1';
$route['(:any)'] = 'page/index/$1';
All resulting in the Default.

I think that some override is happening with the default_controller that it either serves that or nothing (404). I can't run other classes even if I try:
index.php/test/index/test (and all the variations) as it continually returns the default from the class defined in the default controller.

Any help would be greatly appreciated. This has brought me to utter exhaustion and all the time CI has saved me has been lost Sad

#2
[eluser]mddd[/eluser]
Could your 'defaultindex' be causing trouble? I mean, if the server comes across something that is not an existing file (maybe something like /page/index/abc) can't it be that the server will then redirect you to index.php, as that is the default index?
Try removing that line and see what happens.

Also, to help us understand, is Page your default controller or are you trying to call the Page controller but getting another controller which is your default?

#3
[eluser]TheServant[/eluser]
Thanks for your reply.
I have deleted defaultindex and no change.
Yes, Page is the default controller and I can't get any other.

#4
[eluser]mddd[/eluser]
If you remove all your routes except the default controller route,
then it should work like this:

/ -> page controller. index method. default $page: register
/somecontroller -> goes to that controller
/page/somevalue -> page controller. somevalue method (if it exists). otherwise: 404 error.
/page/index -> page controller. index method. default $page: register
/page/index/somevalue -> page controller. index method. $page: somevalue

If you want this:
/page/somevalue -> page controller. index method. $page: somevalue
then you must use the _remap method in the page controller. this takes care of any method calls to methods
that don't exist:

Code:
function _remap($vars)
{
   $this->index($vars);
}
This will send ALL calls to the page controller through _remap, and _remap sends them on to index, along with the values from the requested url.
So /page/somevalue goes to _remap('somevalue') and that goes on to index('somevalue').

#5
[eluser]Jelmer[/eluser]
I have a similar setup and I have in routes.php:
Code:
$route[':any'] = 'page';

In my controller I do the processing using a _remap() function:
Code:
function _remap()
{
    $this->index($this->uri->uri_string());
}

This is how I solved it over 2 years ago, I'm not sure anymore why I did it like this and not for instance using $route[':any'] = 'page/index'; - but this works for me, and I can remember similar problems while first writing this setup.

#6
[eluser]TheServant[/eluser]
I have solved it for as far as I can see...
I have not tried any remap functions but simply used:
Code:
$route['(:any)'] = 'page/index/$1';
Which I had tried before, and then changed my uri_protocol to ORIG_PATH_INFO.

I think that's all I have changed and it seems to be working. Thanks for your replies. If I run into anymore trouble with this I will definitely give _remap() a go.

Thanks again.

#7
[eluser]porky_voice[/eluser]
i used the route but i can't browse any other page

#8
[eluser]Jelmer[/eluser]
When you use the rule with (:any) in there it will match any uri (as one might expect Wink) - so be very carefull when you use it to define exceptions to the rule, or you won't be able to use other controllers anymore.
Also you need to define the exceptions prior to the :any rule, if you don't the :any rule will supercede the other rules and they will never be invoked.

#9
[eluser]porky_voice[/eluser]
Thanks for that Jelmer... it's working now.. Big Grin


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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