Reputation: 3008
How to access controller only with folder name, folder exist in controller. Folder name is admin inside controller folder. Controller is dashboard(php file) inside admin folder.
$route['admin/(:any)'] = 'admin/dashboard';
htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|jquery|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?$1 [L,QSA]
I want to access dashboard(php file) using: http://Website/admin. But not working.I thnik, I have issue with above route line.
Working route
$route['(:any)'] = "admin/dashboard";
Using $route['(:any)']
page can access with: http://Website/any-string-or-letter. But how to access only with: http://Website/admin.
Upvotes: 2
Views: 1486
Reputation: 462
Follow these steps to do this:
application/controllers/admin
application/controllers/admin/dashboard
Dashboard.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Dashboard extends CI_Controller {
/**
* Index Page for this controller.
*
*/
public function index()
{
$this->load->view('my-view');
//$this->load->view('admin/my-view'); //If you are saving the views in a sub directory under views/admin/my-view.php
}
}
Create a my-view.php
under application/controllers/views/my-view.php
<h5>MY VIEW FILE CONTENT</h5>
Access this controller like this: If you are on localhost then it will be like: http://localhost/my-project/admin/dashboard/index
.htaccess
file and naming conventions for CodeIgniter 3. .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /my-project
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt|css|docs|js|system)
RewriteRule ^(.*?)$ /my-project/index.php?/$1 [L]
</IfModule>
routes.php
$route['admin'] = 'admin/dashboard/index';
*Please try to remove/comment all other routes except this one to avoid the conflicts
Upvotes: 1
Reputation: 4582
The problem you are probably having right now is that CodeIgniter is expecting something in the place of (:any), so when you try to go to /admin it fails because there is no Admin.php controller, and you lacked the second URI segment. Try this:
$route['admin/(.+)'] = function ($x = NULL)
{
return 'admin/dashboard';
};
Probably another thing to consider is what the CI user guide says about controllers in sub-directories, and that you may have a default controller:
Each of your sub-directories may contain a default controller which will be called if the URL contains only the sub-directory. Simply put a controller in there that matches the name of your ‘default_controller’ as specified in your application/config/routes.php file.
Link to that section: https://www.codeigniter.com/userguide3/general/controllers.html#organizing-your-controllers-into-sub-directories
What that means is that if you look in config/routes.php, if your default_controller is "welcome", then you can also have a controller named application/controllers/admin/Welcome.php , and requests routed to /admin would go there if there was no other matching controller.
I don't think any of this is ideal. If it were me, I'd figure something else out in the way of app organization.
Upvotes: 0