I am new to CodeIgniter and I have some issues with its routing config.
Everything works fine if I access a page as it should be
http://localhost/index.php/class/controller
But if I add some random arguments
http://localhost/index.php/class/controller/randomStuff
it displays the page without the CSS or the JS but I would like to display a 404 error page.
Here is my route.php file
$route['stats'] = 'main/account';
$route['/'] = 'index.php';
$route['/(:any)'] = 'main/disperr'; //Tried this to solve the problem but doesn't work (disperr simply returns show_404())
$route['default_controller'] = 'main';
$route['all'] = 'main';
$route['404_override'] = 'errors/page_missing';
$route['translate_uri_dashes'] = FALSE;
Thanks in advance for your help.
Edit : Thank you for your answers !
After routing the pages correctly I stil had the missing css issue.
As I'm using twig, I sent the base_url() as a parameter to my view and added it to the path to my CSS and JS.
It works now.
you may plese the following url
http://localhost/class/index.php/main
after index.php you need to give your name of the controller(not folder name controller)
You have to follow some rules from documentation page.
Routes you found after CodeIgniter installation have to be at the beginning in order you found those.
$route['default_controller'] = 'main';//Main class need to have index() method for this works
$route['404_override'] = 'errors/page_missing';
$route['translate_uri_dashes'] = FALSE;
You have to take care of precedence because rules will be read in order those are defined in file.
$route['all'] = 'main';
$route['stats'] = 'main/account';
$route['(:any)'] = 'main/disperr'; //Tried this to solve the problem but doesn't work (disperr simply returns show_404())
If you have (:any) wildcard placeholder it has to be at the end of file because you have to check all specific routes not to pass (:any) rule, because that rule is valid for any route (as name suggest). Slash to index route is not needed because slash is default controller and it is already defined. Routing to index.php is not valid and you are suppose to route just to 'controller_name/method_name/param1' or similar pattern. No need for slashes at the beginning, as you see I removed those. Maybe removing slashes from the beginning of made routes will solve your CSS issue, but you need to follow other documentation rules I exposed here. Read full page here.
Related
Please can any one suggest how to shorten the url
http://localhost:8080/MyWebApp/index.php/Cpanel_control/
to
http://localhost:8080/MyWebApp/Cpanel
in Codeigniter using routes.
I tried it in this way
$route['Cpanel'] = "MyWebApp/index/Cpanel_control";
But did not work
To remove index.php from your url in CI, you need .htaccess file.
Check this out https://gist.github.com/philipptempel/4226750
I'm assuming Cpanel_control is a valid controller.
For the routing, you can have this in your routes settings
$route['Cpanel'] = "Cpanel_control";
To avoid any other issues, make sure base_url in config file is set thus
$config['base_url'] = "http://localhost:8080/MyWebApp";
New Approach
Routing:
In some instances, however, you may want to remap this relationship so
that a different class/method can be called instead of the one
corresponding to the URL.
A short excerpt from the CI doc shows that you can't use routing here. Instead you should go for a mod_rewrite rule (.htaccess)
RewriteEngine On
RewriteRule ^index/Cpanel_control/(.*) Cpanel/$1 [R]
Old approach
According to the corresponding documentation, the paths are both not absolute. Furthermore, you need to set the "from"-URI as the array key and the "to" URI as the string. If you want to route index/Cpanel_control to Cpanel, you need to swap the URIs of you example.
So this would be correct:
$route['index/Cpanel_control'] = "Cpanel";
Having problems with routing in CodeIgniter.
Here's my routes.php
$route['default_controller'] = "site/site/index/$1";
$route['404_override'] = '';
$route['admin'] = 'admin/index';
I'm using modular system and my folder format is:
In side the modules > site > controllers >site.php i have the following that gets the path of each that is entered into the cms, but the problem is when i navigate to localhost/my_site/ it shows the page but with errors, so if i put the following route in: $route['site/(:any)'] = 'site/site/index/$1'; it then works and shows all the pages but obviously I don't want this, i want it to know that the home page is set. Or can this be done via htaccess DirectoryIndex ??
If this doesn't make sense or you cant understand what i mean give me a shout and i'll try explain more details, but that's about it the problem.
I think its because you default controller expects a parameter but you are not passing it one
$route['default_controller'] = "site/site/index/$1";
I think it should be
$route['default_controller'] = "site/site/index";
I am having trouble removing the controller name from my url path on my localhost.
i have this url - localhost:8888/localhost/site_name/
i have been able to remove index.php from the url using my htaccess similar to http://codeigniter.com/wiki/mod_rewrite so that:
localhost:8888/localhost/site_name/index.php/controller_name
is now:
localhost:8888/localhost/site_name/controller_name/
but i can't remove the controller name from the path so that:
localhost:8888/localhost/site_name/controller_name/function_name/
becomes:
localhost:8888/localhost/site_name/function_name/
I am using only one controller, and i have added:
$route['^(function_name1|function_name2|function_name3)(/:any)?$'] = 'controller_name/$0';
$route['^(?!ezstore|ezsell|login).*'] = "home/$0"; /*similar variation i tried*/
and other variations to my routes file but it does not have any effect. i also tried using the _remap function but that does not help in this case.
Any help will be appreciated! Thanks
You can use a wildcard route,
$route['(:any)'] = "controller_name/$1";
Then when if you go to http://localhost/function_one/param1
it will call the controller controller_name the function function_once and pass param1 as the first parameter.
nb: I must point out, using only one controller for an entire site does raise warning bells for me, you may want to get your code design checked out, but that's just me.
I'm using the following code in my routes.php to hide the controller name from the URL structure:
$route['(:any)'] = "auth/$1";
It works great, but my problem is this: When I want to access another controller, it seems to treat it as a function of the hidden controller.
So for example. I have
http://mysite.com/controller1/somefunction
which turns into:
http://mysite.com/somefunction
What if I want to access:
http://mysite.com/jsonfunction/anotherfunction/
How can I access the other controller while keeping the other one hidden? I really don't want visitors seeing http://mysite.com/maincontroller/ that's just redundant!
You are going to have to define your routes more specific I am afraid.
You can still use:
$route['(:any)'] = "auth/$1";
But it will probably go to the button of your list of routes.
if you want other routes to be added that overrule that one you'll have to place them on top.
For example like this:
$route['login'] = "auth/login";
$route['varY'] = "controllerX/varY";
$route['varY/(:any)'] = "controllerX/varY/$1";
$route['foobar'] = "controller/method";
$route['(:any)'] = "auth/$1";
See this document for more info and future reference:
http://codeigniter.com/user_guide/general/routing.html
Been trying to learn codeigniter, 1 problem I had though is that if I had a function say
top_10()
It would mean that my urls will be something like
..../top_10/
Which is fine but I prefer - more than _ for urls. I tried changing my function names to top-10(), but it results in a syntax error it seems (even if it doesn't it results in a ugly function name), Is there a way to let codeigniter auto converts all the _ in my controller functions to - when it comes to the urls?
.../top_10/ -> .../top-10/ for all other similar controller functions.
You can use the URI routing rule
something like :
$route['controller/top-10'] = "controller/top_10";
Add this line in the routes.php file inside application/config/routes.php
Here controller/top-10 will be the part of your url which will be routed to the desired function provided by on the right hand side.
also if you aren't using mod_rewrite to rewrite your url then your URL will appear some thing like
http://localhost/index.php/controller/top-10
Inside your application/config/routes.php folder if there isn't a line for translating uri dashes then add this line: $route['translate_uri_dashes'] = TRUE;
If $route['translate_uri_dashes'] already exists, simply set it's value to true.