Routing in codeigniter incorrect - codeigniter

This is the application structure
-app
-controllers
-v1
-home.php
-login.php
-models
-v1
-home_model.php
-login_model.php
-views
-v1
-home
-index.php
-login
-index.php
My default controller is v1/home.php
I want to remove v1 from the url while routing meaning the url should read www.abc.com/login instead of www.abc.com/v1/login
So when i release a v2 version of the app i can write another rule in the routes and v1 and v2 code both will be live at the same time.
here is what I have tried
$route['default_controller'] = "v1/home";
$route['v1/(:any)'] = "/$1";

try these routes instead
$route['default_controller'] = "v1/home";
$route['(:any)'] = "v1/$1";
in future when v2 is ready, simply change it to
$route['(:any)'] = "v2/$1";

Hello Codeigniter support two types of routing rules
1)Wildcards
2)Regular Expressions
I prefer Wildcards
in routes just place this one
$route['login/(:any)'] = "v1/login";
A URL with "login" as the first segment, and anything in the second will be remapped to the "v1" class and the "login" method.
means your change www.abc.com/login instead of www.abc.com/v1/login
check it once routing in codeigniter here https://ellislab.com/codeigniter/user-guide/general/routing.html
.......

Related

Codeigniter folders for controllers

I would like to build a platform that contain some versions control.
Now I would like to create for each version folder inside the controller folder so I can merge all version related controllers inside.
For example controller folder:
1.0.0/default.php
2.0.0/default.php
3.0.0/default.php
(So in this example 1.0.0 is the version number and default is the controller name)
So I would like to create some routing rules so that version structure will work from URL:
http://www.mywebsite.com/1.0.0/
Any idea how to do it? also there may be more then one controller for each version for example:
1.0.0/default
1.0.0/login
1.0.0/member
here my routes info:
$route['default_controller'] = 'default';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
Thanks
You can do as follows inside your routes file:
$route['1.0.0'] = '1.0.0/default';
or if you want it more dynamic:
$route[(:version)] = "$version/default";
Use subFolders
When using this feature the first segment of your URI must specify the
folder. For example, let’s say you have a controller located here:
application/controllers/products/Shoes.php
To call the above controller your URI will look something like this:
example.com/index.php/products/shoes/show/123

Codeigniter Routes.php within folder

I am using the latest version of CodeIgniter on Server2008 with IIS7.5
I have all of my CI files in a folder mywebsite.com/survey
nps = Controller
survey = Function
client_id = variable base64 encoded client number
I have a script that runs when you visit:
http://mywebsite.com/survey/nps/survey/client_id/MjgzOTcyMW
But I want for it to run when you visit:
http://mywebsite.com/survey/MjgzOTcyMW
How do I set up my routes.php?
I currently have:
$route['/:any'] = 'nps/survey/client_id/';
try
$route['(:any)'] = 'nps/survey/client_id/$1';
or
$route['survey/(:any)'] = 'nps/survey/client_id/$1';
You have to make sure you don't mix up your routes here:
Using just $route['/:any'] would be wrong (even if you had the (:any) correct.
To properly define a route), remember that the left hand side is the pattern route, and the right hand (after the =) is the translated controller/method/parameter format.
So define the route (after all your other routes) as they are to be ordered from MOST specific to least specific (similar to ALLOW/DENY rules etc;):
$route['survey/(:any)'] = 'nps/survey/client_id/$1';

one codeigniter controller named site needs to handle multiple domains

Got a controller in codeigniter who handles different sub sites.
site/index/1 fetches content for subsite A
site/index/2 fetches content for subsite B
Now we decided to register domain names for these sub sites.
so what we need:
http://www.subsite1.com -> default controller should be site/index/1
without the site/index/1 in the URI.
http://www.subsite2.com -> default controller should be site/index/2
without the site/index/2 in the URI.
I fiddled and tried to play with routes.php but getting nowhere..
Can somebody point me in the right direction?
in your routes.php file you need to set this:
$route['default_controller'] = ($_SERVER['SERVER_NAME'] == 'http://www.subsite1.com' ? "site/index/1" : "site/index/2");
and if your trying to force it somewhere when some weird url is types in:
$route['404_override'] = ($_SERVER['SERVER_NAME'] == 'http://www.subsite1.com' ? "site/index/1" : "site/index/2");
and for the second one just switch it to 2

Additional URI segment before Codeigniter controller

I'm making this little project in Codeigniter and I need to have one additional segment before controller.
The default Codeigniter URL is:
base_url/controller/function/parameter1/../parametern
What I need is:
base_url/something/controller/function/parameter1/../parametern
Where "something" is a name that user creates so I cant create a subfolder in controllers folder as suggested in other topics because its dynamic and each user chooses what he likes.
Basically what I need is that it like ignores that segment (I would catch that segment in a hook, extend CI_controller or something like that and validate it), for example if I write base_url/stackoverflow-rocks/home/function, base_url/asd-whatever/home/function Codeigniter would look at it like base_url/home/function
I have also looked at Passing variables before controller URI segment in CodeIgniter question, its almost the same as mine but the suggested answer didn't work.
I used $route['(:any)/(:any)'] = '$2'; which works if the url is base_url/whatever/home, but if the url is base_url/whatever/home/function or base_url/whatever/home/function/param1/../paramn it doesn't, the workaround for this is to write:
$route['(:any)/(:any)'] = '$2';
$route['(:any)/(:any)/(:any)'] = '$2/$3';
...
$route['(:any)/../(:any)'] = '$2/../$n'
which is easy, but seems a bit lame, and it will only work if I have from 2 to n segments in url. I also tried to mix it with regular expressions, like:
$route['(:any)/(.*)'] = '$2'
But it works just like the previous one, (.*) is just one segment but not the whole url...
Is there some way to just write
$route['(:any)/rest of the url'] = 'rest of the url';?
I also tried using .htaccess:
RewriteRule ([^/]*)/(.*) inedx.php?/$2
or
RewriteRule (.*)/(.*) index.php?/$2
But it didn't work because $2 referenced to the first part, for example if I write:
base_url/one/two it rewrites it to base_url/index.php?/one.
But if I use:
RewriteRule (.*)/(.*) index.php?/$1/test/$2
it references as it should, for example base_url/one/two => base_url/index.php?/one/test/two, so $1 = one and $2 = two, as it should be.
Quite a long question but I hope someone can help, thanks!
see if this works:
$route['base_url/controller/(:any)'] = 'base_url/$1'

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.

Resources