Welcome Guest, Not a member yet? Register   Sign In
overriding default controller via $routing[] in index.php can't call methods other than index
#1

[eluser]taber[/eluser]
I have an /admin directory set up with a basic auth .htaccess and this index.php (snippet) in it:

Code:
/*
* --------------------------------------------------------------------
* DEFAULT CONTROLLER
* --------------------------------------------------------------------
*
* Normally you will set your default controller in the routes.php file.
* You can, however, force a custom routing by hard-coding a
* specific controller class/function here.  For most applications, you
* WILL NOT set your routing here, but it's an option for those
* special instances where you might want to override the standard
* routing in a specific front controller that shares a common CI installation.
*
* IMPORTANT:  If you set the routing here, NO OTHER controller will be
* callable. In essence, this preference limits your application to ONE
* specific controller.  Leave the function name blank if you need
* to call functions dynamically via the URI.
*
* Un-comment the $routing array below to use this feature
*
*/
    // The directory name, relative to the "controllers" folder.  Leave blank
    // if your controller is not in a sub-folder within the "controllers" folder
    $routing['directory'] = '';

    // The controller class file name.  Example:  Mycontroller.php
    $routing['controller'] = 'admin';

    // The controller function you wish to be called.
    $routing['function'] = '';

/*
* -------------------------------------------------------------------
*  CUSTOM CONFIG VALUES
* -------------------------------------------------------------------
*
* The $assign_to_config array below will be passed dynamically to the
* config class when initialized. This allows you to set custom config
* items or override any default config values found in the config.php file.
* This can be handy as it permits you to share one application between
* multiple front controller files, with each file containing different
* config values.
*
* Un-comment the $assign_to_config array below to use this feature
*
*/
    $assign_to_config['admin_authed'] = TRUE;

Loading up /admin/ in my browser works fine, but if I try /admin/somethingelse/ I get an Apache 404 error (not a custom 404).

So I tried 2 things at this point:

1) Using routes.php to redirect /admin/somethingelse/ to /any_other_controller as a test. Routing is apparently totally ignored at this point.

2) Using mod_rewrite in .htaccess to redirect /admin/* requests to /index.php?/admin/$1 - which WORKS, except it looks like my custom config assignment doesn't get set. Sad It only seems to get set when calling the method stored in $routing['function'].

Has anyone else seen this? I think everything would be fine if either #1 or #2 worked as expected.

PS:
I know the comment says "The controller class file name. Example: Mycontroller.php" but adding ".php" doesn't work. Just a side note. Doesn't seem to make a difference w/ my issue. Smile

Edit:
Okay, I tried commenting out those lines and not overriding the default controller. Except now I'm unable to call any methods other than index on my site's default controller. (The other pages 404.) Sad AND I can't use routes.php to re-route anyhere as it's being ignored. Help! Smile
#2

[eluser]InsiteFX[/eluser]
You cannot override the default controller you have to have a default controller!

The best thing to do when using admin etc is to create a MY_Controller and extend all your other controllers from that!

Also you can rename the default controller to whatever you want.

InsiteFX
#3

[eluser]taber[/eluser]
[quote author="InsiteFX" date="1306404976"]You cannot override the default controller you have to have a default controller!

The best thing to do when using admin etc is to create a MY_Controller and extend all your other controllers from that!

Also you can rename the default controller to whatever you want.

InsiteFX[/quote]

Well I don't really want to override the site's default controller per se... just force my /admin/index.php to use my "Admin" controller (and any other methods I create under it). For example, I hit http://mysite.com/admin/delete/3 and expect it to use my Admin controller's "delete" method:

Code:
function delete($id) {
    // delete id 4
}

This is the key part that I'm trying to get to work (from the comments of index.php)

Quote:* IMPORTANT: If you set the routing here, NO OTHER controller will be
* callable. In essence, this preference limits your application to ONE
* specific controller. Leave the function name blank if you need
* to call functions dynamically via the URI
.

(Emphasis mine.) I should also mention I'm using CI 2.0.2.
#4

[eluser]InsiteFX[/eluser]
Then what you need is to use routes.

This is how I have mine setup, should be a good example for you.
Code:
$route['login']           = "admin/admin/login";
$route['logout']          = "admin/admin/logout";
$route['register']        = "admin/admin/register";
$route['admin/dashboard'] = "admin/admin/index";
So if you go to http://www.yoursite.com/login then it will use admin/admin/login

Where the first admin is controllers/admin directory
Then admin.php and the methods in admin.php login, logout, register and index

if you need to pass parameters then it is something like this:
Code:
$route['login/(:num)']        = "admin/admin/login/$1";
$route['login/(:num)/(:num)'] = "admin/admin/login/$1/$2";

// or
$route['login/(:any)']        = "admin/admin/login/$1";
$route['login/(:any)/(:any)'] = "admin/admin/login/$1/$2";

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB