codeigniter routing issue - codeigniter

I had my users profile at
www.domain.com/user/username
and moved it at
www.domain.com/username
but this required to add most classes functions into the routes.php file in the config and if i want to add new features to my app i will need to add all the functions into the routes.php file which doesnt sound good practice...
What is the best way that other deal with it on CodeIgniter ?

Perhaps, you can do it the other way round - make a whitelist of usernames that can't be taken (those would be names of your controllers, like admin, contact, etc...) and route anything except the whitelist items.

seems i got the answer
what i did is add the below code for every controller i have
$route['controller'] = "controller";
$route['controller/(:any)'] = "controller/$1";
and this code at the bottom
$route['(:any)'] = "user/$1";

Related

Codeigniter subfolder named like controller

I'm trying to make folder in controller folder with exact name like controller in Codeigniter. Is it possible by some trick or something?
Screenshot here: http://i.imgur.com/vrQ1J9V.png
If it will be like
/controllers/manage/manage.php
you should add in /config/routes.php
$route['manage/(.*)'] = "manage/manage/$1";
$route['manage'] = "manage/manage";
There is no problem in using the same name, but it is confusing. The best solution would be to change the name, but you can use just fine.
Remember to use the routes with 'manage/manage/function'.
EDIT
I incorrectly viewed the first time and thought manage.php was outside the manage folder.
It will work, but your URL will just have "manage" twice. You could change the routes config to remove the first "manage", but it will be less confusing and time-consuming to just name manage.php something different.

Redirection rules in codeigniter

I want to create location pages in my codeigniter site. So I have one contrller named locations and index method. So all the requests http://mysite.com/location_name should be landed to http://mysite.com/index.php/locations/index. And all other should work as it is like http://mysite.com/login should be landed to http://mysite.com/index.php/home/login. Contact us http://mysite.com/contact-us should be landed to http://mysite.com/index.php/home/contact.
I tried to achieve this by writing following line route rule (route.php):
$route['(:any)'] = 'locations'; //location name can be anything around the world
So locations are working fine, but http://mysite.com/login and http://mysite.com/contact-us are not working, they redirecting continuously in infinite loop.
Please suggest the solution. Thank.
The routes are applied from top to bottom, so you need to have your more specific rules listed first, then your more generic rules last:
$route['login'] = 'home/login';
$route['contact-us'] = 'home/contact';
$route['(:any)'] = 'location/index';
I see that you mentioned you've tried changing the order of rules in your route file. So If you have done this and it's not working - you have something else going on.
I would check these things:
.htaccess file (if you're using it)
controllers that are causing the infinite loop (any redirections in there, _remap method, etc. )

How to build CodeIgniter URL with hierarchy?

I know this doesn't exactly match the form of www.example.com/class/function/ID/, but what I want to display to the user would make more sense.
This is what I would like to do:
www.example.com/project/id/item/item_id/
So, an example would be:
www.example.com/project/5/item/198237/
And this would be the behavior:
www.example.com/project/ --> This would show a list of projects (current implementation)
www.example.com/project/5/ --> This would show a list of items on project 5 (current implementation)
www.example.com/project/5/item/ --> This wouldn't really mean anything different than the line above. (Is that bad?)
www.example.com/project/5/item/198237/ --> This would show details for item 198237.
So, each item is directly associated with one and only one project.
The only way I can think how to do this is to bloat the "project" controller and parse the various parameters and control ALL views from that "project" controller. I'd prefer not to do this, because the model and view for an "item" are truly separate from the model and view of a "project."
The only other solution (that I am currently implementing and don't prefer) is to have the following:
www.example.com/project/5/
www.example.com/item/198237/
Is there any way to build a hierarchical URL as I showed at the beginning without bloating the "project" controller?
There are 3 options, sorted by how practical they can be:
Use URI Routing. Define a regular expression that will use a specific controller/method combination for each URL.
Something like that could help you, in routes.php:
$route['project/'] = 'project/viewall';
$route['project/(.+)'] = 'project/view/$1';
$route['project/(.+)/item/'] = 'project/view/$1';
$route['project/(.+)/item/(.+)'] = 'item/view/$2';
That is, considering your controllers are item and project respectively. Also note that $n in the value corresponds to the part matched in the n-th parenthesis.
Use the same controller with (or without) redirection. I guess you already considered this.
Use redirection at a lower level, such as ModRewrite on Apache servers. You could come up with a rule similar to the one in routes.php. If you are already using such a method, it wouldn't be a bad idea to use that, but only if you are already using such a thing, and preferably, in the case of Apache, in the server configuration rather than an .htaccess file.
You can control all of these options using routes.php (found in the config folder). You can alternatively catch any of your URI segments using the URI class, as in $this->uri->segment(2). That is if you have the URL helper loaded. That you can load by default in the autoload.php file (also in the config folder).

CI: Controllers in subfolder - removing subfolder from url

I want to group my controllers, views and models into public/ and members/ subfolders.
But for the public stuff, I don't want /public/ to show in the URL, so:
http://mysite.com/ & http://mysite.com/section/
should point to: /public/home & public/section/
How should I change routes.php to accommodate this?
(I'm fine with members/ stuff having members/ in the url)
To make http://mysite.com/ point to /public/home you will need to mark public/home as your default controller. You will also need to add a specific route to make http://mysite.com/section point to /public/section. Try this (the regex is a little dubious in my opinion, but may do what you want):
$route['default_controller'] = "public/home";
$route['section/(.*?)'] = "public/section/$1";
Kindly see if this works for you:
$route['section'] = "public/section";
Your index page would remain as "public/index.php"
You don't need to add routes for every controller.
This works for me:
$route['members'] = 'members'; // route members to members
$route['members/(.*?)'] = 'members/$1'; // route members/... to members/...
$route['(.*?)'] = 'public/$1'; // route anything but above lines to public/...
The first two lines are intentionally redundant to protect 'members' segment from being routed to 'public'. And the third line does the magic.

How can I use GET forms with CodeIgniter?

I understand that CI is mostly URL segment based, but I want to have a query string: blahblah.com/search.html?q=keyword
When I try $this->input->get( "q" ), it returns empty. Is there a route or something I need to configure?
Why not make it http://mysite.com/search/keyword/
You have to enable query strings
CodeIgniter optionally supports this capability, which can be enabled in your application/config.php file. If you open your config file you'll see these items:
$config['enable_query_strings'] =
FALSE;$config['controller_trigger'] =
'c'; $config['function_trigger'] =
'm';
If you change "enable_query_strings" to TRUE this feature will become active. Your controllers and functions will then be accessible using the "trigger" words you've set to invoke your controllers and methods:
index.php?c=controller&m=method
Example: index.php?c=products&m=view&id=345
http://codeigniter.com/user_guide/general/urls.html
The best way to get query strings working in CodeIgniter is to use Google. This question is asked (and answered) on the forums, here and on twitter at least 10 times a day.
There are a few methods, but recently I am a fan of the following method:
http://www.dijexi.com/2009/08/how-to-mix-segment-and-query-string-in-codeigniter/
I prefer this over others as it will have no application-wide effects like some other approaches and it won't require any crazy hacking to get it working.
If you want this $_GET support throughout the entire app, just put the parse_str into MY_Controller or a pre_controller hook.

Resources