0

I have a folder under my main controller-folder called admin, in that controller i have a file name admin.php which has a function xyz.

I want to access that function using this url

http://localhost/webroot/admin/xyz

However when I try to access it, it is giving me this error.

404 Page Not Found

The page you requested was not found.

this is code of my routes.php file

$default_controller = "welcome";
$controller_exceptions = array('welcome','forum');
$route['default_controller'] = $default_controller;
$route["^((?!\b".implode('\b|\b', $controller_exceptions)."\b).*)$"] = $default_controller.'/$1';
$route['404_override'] = '';
$route['admin'] = "admin/admin";

This is my .htaccess file

 RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]  

Also mod-rewrite is enabled :)

Please let me know why is it not working and how can i make it work. Any kind of help is really appreciated. Thanks

Bilal Ahmad
  • 191
  • 1
  • 2
  • 17

1 Answers1

0

Ensure that your routes are stored in application/config/routes.php (not router.php as specified in your question). Try adding a route that uses a wildcard, like this:

$route['admin']        = "admin/admin";    //Routes to 'index()' function
$route['admin/(:any)'] = "admin/admin/$1"; //Handles all other cases

:any will match a segment containing any character(s), after the admin segment, and will pass it/remmap it to the the admin controller.

The user guide contains more information on controllers in sub-folders and routing.

jleft
  • 3,457
  • 1
  • 23
  • 35
  • Thank you Lefters for your quick response, but it has stopped even accessing http://localhost/webroot/admin give 404 error :S PS: Yes its routes.php that was typo, sorry. – Bilal Ahmad May 19 '13 at 12:42
  • No problem. Can you access all of your other controllers? Have you tried setting your `RewriteBase` to: `RewriteBase /` or `RewriteBase /webroot/`? – jleft May 19 '13 at 13:15
  • Yes I can access my controller that is not in admin folder under main-controller folder. I tried RewriteBase / and it leads me to localhost page, (i.e main page of wamp server) RewriteBase /webroot and RewriteBase /webroot/ gives same error of 404. :( Thanks for helping. – Bilal Ahmad May 19 '13 at 13:33
  • If you move the _admin_ controller to `application/controllers/` can you access it there? Is the controller itself defined properly - class name starts with an upper case letter, extends `CI_Controller` (or a class already extending that), etcetera? – jleft May 19 '13 at 13:47
  • What is that controller_exceptions supposed to be doing? Comment that out and use Lefters code for your admin and I'll take a bet it works. Or put the admin route at the top below the default controller. – Rick Calder May 19 '13 at 14:02
  • OK It was working when I placed outside the controller folder. Rick you are right, I have commented that thing already, and its giving some issues will post in a bit, thankyou. – Bilal Ahmad May 19 '13 at 14:09
  • I have removed those lines, it is working now, but the problem is that http://localhost/webroot/admin & http://localhost/webroot/ both redirect me to the main controller's index() function, which obviously it should not. however http://localhost/webroot/admin/xyz leads me right to admin controller. Here is my routes.php file [code] $default_controller = "welcome"; $route['default_controller'] = $default_controller; $route['admin/(:any)'] = "admin/admin/$1"; $route['404_override'] = ''; [/code] – Bilal Ahmad May 19 '13 at 14:18
  • I added this to my routes.php and it works now, is it ok or should use some other way ? coz this three times admin seems to me like m enforcing it and not going by rule. $route['admin'] = "admin/admin/admin"; $route['admin/(:any)'] = "admin/admin/$1"; Thanks for all the help – Bilal Ahmad May 19 '13 at 14:37
  • Does your `index` function in _admin_ accept any parameters? If not, then you can route it like this: `$route['admin'] = "admin/admin/index";` Place this rule above: `route['admin/(:any)'] = "admin/admin/$1";` – jleft May 19 '13 at 15:10
  • ok sounds great!! I'll use this $route['admin'] = "admin/admin/index"; as it doesn't take any parameters. Thank you so much. :) – Bilal Ahmad May 19 '13 at 16:02
  • 1
    No worries at all, I'm glad that I've helped! (`$route['admin'] = "admin/admin";` _might_ also work, as a slightly shorter alternative to `$route['admin'] = "admin/admin/index";`) – jleft May 19 '13 at 16:11
  • OMG!! :) no idea how but yes it does work now `$route['admin'] = "admin/admin"`!! Perfect :) Thanks – Bilal Ahmad May 19 '13 at 16:15