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
Related
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.
This is my first attempt to work/learn CodeIgniter. However, I'm struggling in understanding the "C".
1) Does CodeIgniter always associate a controller to a segment of a URI?
2) What are the best practices to work with controllers? I mean, how can I avoid dumping all my methods in a single controller? Can I split a controller in several files without creating unnecessary URI.
1.Yes controller always associate to segment of a URI. If your controller is under some directory like
controllers
search ---------------------directory inside controller
search ------------------controller
stock_search -------------------method
then it will add whole path in the uri segment e.g :basepath.'search/search/stock_search/';
But you can route it your custom path using routes.php
$route['search'] = 'search/search/stock_search/';
2.You can create different controllers (name should be different) with the different methods or you can say you can split controller methods in different files and customise their url accordingly in routes.php and can create parent controller to use methods in any controller by extending through it.
If you want to get something in codeigniter then codeigniter send the request to a controller. URI must have a controller if no controller in uri then the reguest is goes to default controller which is tell in application/config/routes.php in this code $route['default_controller'] = 'welcome';
And will not be able to split a controller in several files without creating more than one URI.
Controller is associated to url segments.
Url used in Codeigniter is as follows: http://example.com/index.php/projname/controller/method/params.
If you dont specify controller in uri, default controller is called specified in routes.php $route['default_controller'] = 'welcome';
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.
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
I am wondering if there is any other configuration options for a default controller.
For example - if I have a controller called "site" and I set the default controller in the following file: application/config/routes.php to:
$route['default_controller'] = "site";
I should be able to go to http://localhost and that brings up the
index(); function in the site controller.
However, if I try to do go to http://localhost/index.php/index2 to load the index2(); function I get a 404 error. If I change the URL to http://localhost/index.php/site/index2 it works fine - but I thought already set the default controller. Is there any way around this?
The only way to do it is manually writing the routing rule:
$route['index2'] = "site/index2";
You cannot do that.
The default_controller is only for URLs without any URI parameter. How could CodeIgniter distinguish between a method name and a controller name?
What you can do:
define a redirect inside your 404 document or directly map your 404 document to index.php
No CI doesn't work like that the first parameter has to be the name of a controller, so in this case you would have to create a controlled called "index2".