Setting Routing rules for different classes in codeigniter - php

My application URL "localhost/crdlabs/PHP" goes to "localhost/crdlabs/home/display/PHP" which is done using route.php. The rule is as follows.
$route['(:any)'] = "home/display/$1";
Now that I have another controller class called displayarticles(). The current URL for this class is "localhost/crdlabs/displayarticles/article/learning-coding". I understand that I cannot use the routing above for the new controller. How to set a routing rule for the current one to make the URL look like "localhost/crdlabs/learning-coding".
Note : learning-coding part is dynamically set which means there are several different articles that should go to the same controller.
Any help/advise please.

The current routing rule.
$route['(:any)'] = "home/display/$1";
Will routes anything appearing after localhost/crdlabs/argument to localhost/crdlabs/home/display/argument.So the route localhost/crdlabs/learning-coding will be redirected to localhost/crdlabs/home/display/learning-coding.So you can not use like this.
But I suggest you to show your articles
localhost/crdlabs/displayarticles/article/learning-coding
To
localhost/crdlabs/articles/learning-coding
using the following routing rule.
$route['articles/(:any)'] = "displayarticles/article/$1";
Will be best.

Related

Display dynamic URLs in CodeIgniter for seo

I'm building an online store based on CodeIgniter. I'd like URLs to look like this? What is the solution for this type of SEO friendly url.
http://example.com/[product-category]/[product-sub category]
I need this url:
example.com/women/sarees-sari
But my url is generated
example.com/Product/item/MQ==/women/sarees-sari
/Product/ is my controller,
/item/ is function name,
/MQ==/ is my product id
You can use routing to handle your request url. It's simple. For example for your case:
$route['women/sarees-sari'] = 'Product/item/MQ==';
Codeigniter has _remap function that can be called on controllers. So you can call this on core controller or main controller, and call your function that wish.
CodeIgniter has very good routing System so you can modify your url as per your requirement and linking using /application/config/routes.php file.
If you open this file first time, you will see only default controller, i.e $route['default_controller'] = 'welcome';
but you can add as many routes as you want. Like in your case for seo, you should add
$route['women-sarees-sari'] = 'Product/item/MQ=='; and this will route the user from www.example.com/women-sarees-sari to correct controller and method.

CodeIgniter - Stuck with routes

I am trying out CodeIgniter 2.1.4. I already have a controller for showing static pages which I built using the tutorial in CodeIgnitor documentation. I later set-up my routes like this:
// <http://localhost/> refers to <http://localhost/pages/view/>
$route['default_controller'] = "pages/view";
// <http://localhost/somepage/> refers to <http://localhost/pages/view/somepage/>
$route['(:any)'] = "pages/view/$1";
// .htaccess is already setup to rewrite the url without index.php
Now, I don't have much experience with PHP, and the concepts of URL Rewriting and MVC Architecture are fairly new to me.
Let's say there's are pages called •Home, •About, •Admin and •Contact.
For the pages •Home, •About and •Contact, the Pages controller works right as it should.
But for •Admin page, I want to have a separate controller which determines whether the user has logged in or not, and whether he has admin rights etc. And if he hasn't logged in yet, I should load the Login view instead of the Admin view.
The Pages controller has a fairly simple logic. It checks whether the string in argument, appended with .php and prepended with the views directory, exists as a file or not. If it doesn't show_404(), if it does, load view header-template, then load the page, then load view footer-templat. I'm pretty sure most of you who have worked with CodeIgniter must've seen a similar logic for static pages.
I could do redirect('login') inside my Admin view, but that doesn't seem to work. If I create a separate controller for Admin, how would I access it, while according to routes, every URL gets directed to pages/view controller (line#4 in the above code).
As I've already said, I'm fairly new to this. It might be some retarded mistake that I'm making. Or my whole MVC structure might be inappropriately built. How do I get past this and start worrying about the authentication stuff? Can anyone advise?
$route['default_controller'] = "pages/view";
$route['admin/(:any)'] = "admin/$1"; //(admin controller) with "any" method
$route['(:any)'] = "pages/view/$1";
localhost/poject/admin/edit as example
The problem you are experiencing is simple, you overwrite all controllers by that (:any) it isn't wrong but you need to manualy assign each controller that you want route as normal controller as I posted above.
please note that routes are order dependant and if one (first) is used second one is ignored. "Routes will run in the order they are defined. Higher routes will always take precedence over lower ones."
For authentification please see this post.
I would rather use _remap() and extend CI_Controller to take over my routes instead of having this route $route['(:any)'] = "pages/view/$1"; in routes.php.
remapping

Codeigniter URL rewrite to hide controler and method name

My current url is universitytwig.com/user/userprofile/cm.cm , where user is the controller , userprofile is the method and cm.cm is the parameter .How i change this url to universitytwig.com/cm.cm by route.php or by routing from database, Or by .htaccess
Try to add
$route['cm.cm'] = 'user/userprofile/cm.cm';
to your application/config/routes.php
Or
$route['(:any)'] = 'user/userprofile/$1';
if your parameter can be different.
you can use the routes.php but if you make a rule in something like cm.cm to user/userprofile/cm.cm
$route['(:any)']= 'user/userprofile/$1';
But this is not a perfect solution it will point all the controllers of every thing after base url to user/userprofile/ you need to specify some string the CI routes will identify that this needs to be point on this controller if cm.cm is dynamic parameters not hard coded for this your url structure should be
universitytwig.com/students/cm.cm
and in routes you can now make a rule
$route['students/(:any)']= 'user/userprofile/$1';
This will work for your scenario

CodeIgniter __remap() routing more than one controller

I am trying to create a URL shortener using CodeIgniter 2.
I have 2 controllers: main and api.
For redirecting a short link through the router, I am using this setting in config/routes:
$route['(.*)'] = "main/$1";
along with a method in the main controller which should work. However, the controllers won't start. Please help me to solve this problem.
You controller "any" isnt called because it falls into that regex, so it's routed to main.
In order to exclude "any" from this rule you need to create a special rule for that, keeping in mind that for CI rules are cascading , so they're executed in the order they're presented
Note: Routes will run in the order
they are defined. Higher routes will
always take precedence over lower
ones.
So, you would have:
// reserved routes must come before custom routes
$route['default_controller'] = "home";
$route['404_override'] = '';
$route['any'] = //your rule here. maybe "any". ?
$route['(.*)'] = "main/$1"; // CI also provides you with `(:any)` rule, that mateches any character.
More on this here: Uri routing

Codeigniter Routing not equal to pattern

I am working on a codeigniter app and am having some trouble wrapping my head around a routing issue. Basically I would like all routes to map to an specific controller action by default, but I would also like to able to specify an array of routes (or ideally initial url segments) which shouldn't follow this pattern.
By way of example:
If I enter domain.com/username it maps to domain.com/controller/method/show/username
If I enter domain.com/account it maps to domain.com/account
Any help very gratefully received!
James
Open config/routes.php and add the following:
$route['(:any)'] = "controller/method/show/$1";
Please see the link below for more routing concepts.
http://codeigniter.com/user_guide/general/routing.html
Routes will run in the order they are defined. So in your routes file, put the routes for other controllers you still want to work above your catch-all for usernames:
$route['default_controller'] = 'home'; //so root url still works
$route['accounts'] = "accounts";
$route['accounts/(:any)'] = "accounts/$1";
...
$route['(:any)'] = "controller/method/show/$1";

Categories