Hey I am having an issue routing to my other controllers in codeigniter.
Basically this is my base url http://localhost:8012/CodeIgniterProject/
This works fine but every time I type something after it I get an object not found error. I am trying to route to my Dashboard.php controller but having no luck. I want to type http://localhost:8012/CodeIgniterProject/dashboard and hit the Dashboard controller.
$route['default_controller'] = 'home';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['home/(:any)'] = "dashboard";
$route['(:any)'] = "";
$route['404_override'] = 'dashboard';
$route['Dashboard'] = "dashboard";
$route['about'] = "site/about";
I know this might be simple to other people but I don't usually work in php and this has me a little stumped. I was under the impression codeigniter would pick up dashboard from my controllers folder and got to my controller class to load it? And then if I wanted to pass params to the class etc I'd just need to add e.g /123??? Thanks for your time.
define method too, like:
$route['Dashboard'] = "dashboard/index";
Related
$route['default_controller'] = 'Student';
$route['admin'] = 'index.php/Admin_depart/index';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
I am fairly new to CodeIgniter and finishing up my first project. However, before I put it up on my hosting site I would like to clean up the URL's using the routes.php file that CodeIgniter provides in the config folder. but it is not working it work correctly when i write same url i the default_controller
The index.php is causing the issue.
Route destinations must be defined only as a controller/method pair. So in your case the correct route would be:
$route['admin'] = 'Admin_depart/index';
anyone browsing to yoursite.com/admin would be served whatever is in your Admin_depart controller, index method (just as if he/she browsed directly to yoursite.com/admin_depart/index;
I will preface my question by saying I have searched high and low for a solution to my problem, including, but not limited to, Stackoverflow, YouTube and Google.
My issue: Only my default route is working in Codeigniter. My default route is set to a controller called Home which loads a home.php view. That works fine. I have another controller called Pages which as of now has only one method, Contact which points to view of the same name.
My routes.php file looks like this.
$route['default_controller'] = 'home/home';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
// All custom routs go below this line.
$route['contact'] = 'pages/contact';
If I should change the default rout to 'pages/contact' my contact page shows.
My setup is as follows:
Windows 10
XAMMP 5.6.12
PHP 5.6.12
Any help would be greatly appreciated.
Default controller name should be like this $route['default_controller'] = 'home'; not like your $route['default_controller'] = 'home/home';
And then try it the url www.site.url/pages/contact is working or not if working then try
$route['contact'] = 'pages/contact';
Also you can debug your route with this link.
Structure of my controller directory:
--Controller
--frontend
--user_controller
When i call default controller it says 404 page not found
Route i have set :
$route['default_controller'] = "frontend/user_controller";
$route['admin_panel/site'] = "site";
$route['admin_panel'] = "backend/admin_controller/dashboard";
$route['admin_panel/login'] = "backend/admin_controller/index";
$route['admin_panel/(:any)'] = "backend/admin_controller/$1";
$route['admin_panel/(:any)/(.*)'] = "backend/admin_controller/$1/$2";
And when i run http://localhost/example/frontend/user_controller then it works.
So but didn't work this url : http://localhost/example
Please try with: move default controller at bottom
$route['admin_panel/site'] = "site";
$route['admin_panel'] = "backend/admin_controller/dashboard";
$route['admin_panel/login'] = "backend/admin_controller/index";
$route['admin_panel/(:any)'] = "backend/admin_controller/$1";
$route['admin_panel/(:any)/(.*)'] = "backend/admin_controller/$1/$2";
$route['default_controller'] = "frontend/user_controller";
Codeigniter will not allow default controller to be in a sub directory.
You have to rewrite it as
$route['default_controller'] = "user_controller";
And place that directly in the controller folder
You can refer this thread to avoid this. But I didn't tested it.
It is better to avoid the core functionalities as it may affect the updates.
I have not tested this but potentially you could use this to get around the sub-folder?
$route['frontend'] = 'frontend/user_controller';
$route['default_controller'] = 'frontend';
need to make sure there is a 'public function index()' in there though as you aren't specifying the method to load...
I want to remove controller name from every controller is it possible to do like this in route file.For single controller it is working fine for me what if I want to use for all controller in my project?
$route['default_controller'] = 'user';
$route['(:any)'] = "user/$1";
$route['(:any)/(:any)'] = "user/$1/$1";
$route['(:any)/(:any)/(:any)'] = "user/$1/$1/$1";
$route['(:any)'] = "grant/$1";
$route['(:any)/(:any)'] = "grant/$1/$1";
$route['(:any)/(:any)/(:any)'] = "grant/$1/$1/$1";
when I tried this its not working for me.
try this
You may try any one of these
// url could be yourdomain/imran
$route['(:any)'] = 'profile/index/$1';
// url could be yourdomain/10
$route['(:num)'] = 'profile/index/$1';
// url could be yourdomain/imran10
$route['([a-zA-Z0-9]+)'] = "profile/index/$1";
Your class may look like this
class Profile extends CI_Controller {
public function index($id)
{
// $id is your param
}
}
Update : (Be careful)
Remember that, if you have a class Someclass and you use url like yourdomain/Someclass then this will be routed to profile/index/$1 if you have $route['(:any)'] or $route['([a-zA-Z0-9]+)'].
I got the answer
$route['(:any)'] = "user/$1"; //this is dynamically for user controller
for another controller in static way like this
$route['functionName'] = "controller/functionName";
$route['functionName'] = "controller/functionName";
this work fine for me for the time .I wish if this is possible dynamically
The code example you have provided will not work because the rules are exactly the same, for example:
$route['(:any)'] = "user/$1";
$route['(:any)'] = "grant/$1";
There is no difference between the two so how is the application going to know where to route your request. It will just pick the first route that matches for all requests (user controller)
What you are trying to achieve is not possible unless (as #syed) has tried explaining above, there is a difference in at least the secondary parts of the url.
For example any numeric ids are passed to a user controller.
// url could be yourdomain/10
$route['(:num)'] = 'user/index/$1';
Any text based are passed to a different controller
// url could be yourdomain/imran
$route['([A-z]+)'] = "name/index/$1";
So on so forth.
I have two controllers
1- site
2- management
the first controllers(Site) is work successfuly
the second controllers(Managemnt) is not work.
I don't know what is the errror
I change the routes.php but still doesn't work(managment)
$route['default_controller'] = "site";
$route['(:any)'] = "site/$1";
$route['Administration'] = "Administration/index";
$route['Administration/([a-z])'] = 'Administration/$1';
this links work:
example.com/hotel/12312
example.com/contact
example.com/city/newyork
example.com/Administration
but this links doesn't works:
example.com/Administration/hotels
example.com/Administration/add_new
example.com/Administration/cities
where is the problem pls because I tired to solve this problem
thaks
It has to do with the order in witch you are giving route directives.
Code igniter routes requests from top to bottom, so if you want your $route['Administration'] to precede $route['(:any)'] you have to set it first.
$route['default_controller'] = "site";
$route['Administration/([a-z])'] = 'Administration/$1';
$route['Administration'] = "Administration/index";
$route['(:any)'] = "site/$1";
I would allways sugest putting (:any) routes at the end so they don't overwrite more specific routes.
I had same problem & I get this working:
$route['default_controller'] = "welcome";
$route['([a-z-A-Z1-9_]+)'] = "site";
$route['management']="management";
$route['404_override'] = '';
it may help you!
I'm not familiar with Codeigniter routing, but to me looks like everything is matching $route['(:any)'] = "site/$1"; before it ever reaches your Administration routes. Try moving it below everything else...you might also have to switch your Administration routes for the ([a-z]) to match
$route['default_controller'] = "site";
$route['Administration/([a-z])'] = 'Administration/$1';
$route['Administration'] = "Administration/index";
$route['(:any)'] = "site/$1";