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";
Related
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.
I have a simple problem but I just can't figure it out. I wrote a method that reads parameters from the url (one parameter which is the username) and queries all data to that user and displays it in the page.
the only problem is that the method is part of a controller and naturally it has to show in the url (which is simply said: not too nice for sharing. Also I have to write the string as follows:
www.domain.com/controller/profile_guest?user=username
I want to get rid of all that is before username. So for it to show as:
www.domain.com/username
Now there are two scenarios to cover here.
a) someone browse the catalogue, clicks on profile name and then redirects to profile page. once there he likes what he sees, he copies url, and share it.
b) someone receives the shared link and clicks on it, so what should load is the profile page with the url he received not the full one showing controller and method.
is this possible to achieve?
Thanks for the help :)
Update:
$route['default_controller'] = "main/index";
$route['404_override'] = '';
you can use this route:
$route['(:any)'] = "controller/profile_guest/$1";
but any other route, you will need to write it manually above this route.
for example:
$route['controller/view'] = "controller/view";
$route['(:any)'] = "controller/profile_guest/$1";
Explain:
The routes are handled in the order in which they appear on your routes.php file, so if you put $route['(:any)'] at the top, it will handle anything.
suppose that you have a contoller and a function inside it, and you need to execute it for example: www.domain.com/contoller/function, you cann't execute your function because it will match this route $route['(:any)'], so you need to define a route for it before $route['(:any)']
for example:
$route['contoller/function'] = "controller/function"
$route['(:any)'] = "controller/profile_guest/$1";
and do the same thing with all your contollers and its function, but you have to put $route['(:any)'] at the last route.
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.