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';
Related
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. )
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";
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.
I am trying to work out the code igniter routing
I have a url that will look like
http://example.com/register
I want that to hit
http://example.com/account/register/normal
I also have
http://example.com/invite/something_random_here
which I want to hit
http://example.com/account/register/invite/something_random_here
I also have
http://example.com/register/something_random_here
which I want to hit
http://example.com/account/register/subscribed/something_random_here
How do I setup these routes?
Pretty much Straight out of the User Guide
$route['register'] = "account/register/normal";
$route['invite/(:any)'] = "account/register/invite/$1";
Basically anything after invite/ will get tacked onto the end of account/register/invite. I believe it only works for one segment, if you want multiple segment support you'll have to use a regular expression:
$route['invite/(.+)'] = "account/register/invite/$1";
Another usefull one (because I believe (:any) only works for characters) would be:
$route['invite/([a-zA-Z0-9_-]+)'] = "account/register/invite/$1";
This will let all alpha-numeric values (single segment) with _ or - go through, great for GUIDs :)
I believe for your first URL, you would use the following route:
$route['register'] = "account/register/normal";
Your second URL, would make use of the following route:
$route['invite/(:any)'] = "account/register/invite/$1";
Both of these routes would need to be placed in your "config/routes.php" file.
Hope this helps?
Is there a way (via routing) in CodeIgniter to change:
example.com/category/4 to example.com/category/foo-bar
where Foo Bar is the name of category 4 in the database?
Access from the SEO-friendly URL should be allowed, but access via the integer should cause a 404 error.
This should also work dynamically, where any integer is automatically converted to a URL-safe version of its corresponding category name.
I've seen a few solutions that use 'slugs'... is there a decent alternative?
Thanks.
I've only been working with CodeIgniter for the past couple of months in my spare time, so I'm still learning, but I'll take a shot at this (be gentle):
There is a url_title() function in the URL Helper (which will need loaded, of course) that will change Foo Bar to foo-bar.
$name = 'Foo Bar';
$seo_name = url_title($name, TRUE);
// Produces: foo-bar
http://codeigniter.com/user_guide/helpers/url_helper.html
The URL helper strips illegal characters and throws in the hyphens by default (underscores, by parameter) and the TRUE parameter will lowercase everything.
To solve your problem, I suppose you could either do a foreach statement in your routes.php file or pass the url_title() value to the URL, rather than the ID, and modify your code to match the url_title() value with its category name in the DB.
Afaik the link between 4 and "foo-bar" has to be stored in the DB, so you'll have to run some queries. This is usually not done via routing in CI. Also routing just points a URL to a controller and function and has little to do with url rewriting.
Why don't you want to use slugs?
You could try storing the search engine friendly route in the database using this method or this one.
I wouldn't recommend throwing a 404. Use the canonical link tag in the instead if your worried about Google indexing both http://googlewebmastercentral.blogspot.com/2009/02/specify-your-canonical.html.
But if you really want to I guess you could write a function that is called during the pre_controller hook http://codeigniter.com/user_guide/general/hooks.html that checks to see if the URL has an integer as the second segment then call the show_404() method. Perhaps a better solution when writing this function would be to redirect to the SEO friendly version.
Is there a way (via routing) in CodeIgniter to change:
example.com/category/4 to example.com/category/foo-bar
where Foo Bar is the name of category 4 in the database?
Yes.
Using CI 3,
http://www.codeigniter.com/user_guide/general/routing.html
Use Callbacks, PHP >= 5.3
$route['products/([a-zA-Z]+)/edit/(\d+)'] = function ($product_type, $id)
{
return 'catalog/product_edit/' . strtolower($product_type) . '/' . $id;
};
You can route to call a function to extract the name of the category.
Hope I answered your question and can help more people to like codeigniter as I believe it's speedy and light.
Slugs usage is important to make web application more secure which i think is important.
A better recommendation will be to use route to give you a better solution.
$route['(:any)/method/(:num)'] = 'Class/method';
or
$route['(:any)/method/(:num)'] = 'Class/method/$1';
$route['(:any)/gallery/(:num)'] = 'Class/gallery/$1';
base_url()/business-services/gallery/6
base_url()/travel/gallery/12
how to modify routes in codeigniter
Have fun :)