We have a social networking web site for students. Our url looks like this domain.com/student/145236 where 145236 is the roll number. We now have an option of doing something like this domain.com/student/145236/student-name but since google / search engines would prefer domain.com/student-name/student/145236 we would like to do like this, but we are unable to do it, we tried doing this using routes $route['(:any)/student/(:num)'] = 'student/$1'; but it shows 404 page, any help would be grateful :)
This should be work for you:
$route['(:any)/student/(:num)'] = 'student/yourfunction/$1/$2';
Here $1 is for ist param (:any) and
$2 is for second param (:num) .
For getting values from the URL inside the student controller you can use CI Segments.
Related
I am working a codeigniter project. for example I have a controller Blog and a method category with param.
When i go to a link
Developer Blog
it works fine. which redirect to a page developer blog.
Is it possible to rewrite the url as domain.com/blog/developer-blog dynamically ? Thank you.
Open your application/config/routes.php and try following (if you want exact as mentioned in question)
$route['blog/developer-blog'] = 'blog/category/2';
$route['blog/user-blog'] = 'blog/category/3';
for dynamic routing you may use this
$route['blog/developer-blog/(:num)'] = 'blog/category/$1';
$route['blog/user-blog/(:num)'] = 'blog/category/$1';
so your URL now should be looks like https://domain.com/blog/developer-blog/1, https://domain.com/blog/developer-blog/2
or https://domain.com/blog/user-blog/1, https://domain.com/blog/user-blog/2 etc
for more see http://www.codeigniter.com/user_guide/general/routing.html
I am setting the pagination variable with the following:
$dp->pagination = array('pageVar'=>'page');
And adding to the main config rule with this:
'/myrule/page/<page:\d+>' => 'controller/action'
URLs /myrule/page/1 and /myrule/?page=1 work. However, pagination generates /myrule/?page=1. How do I get URLs like /myrule/page/1 from pagination?
Resolve: displace rule higher on the list - that rule worked before the other.
Generally in CodeIgniter we get this structure of URL :
domain/controller/function
So suppose I went to some user's profile then URL would be :
domain/user/profile/username
How can I change that to
domain/username
OR even more better if
domain/u/username
where the middle part is what i decide.
Any Suggestions?
You can user routes.php for this purpose
Suppose you want a url like
domain/u/username
Then in routes.php you can specify the routing rule like below:
$route['u/(:any)'] = "user/profile/$1";
This specifies that if a url comes like '/u/anyvalue' then execute user/profile/$1
where $1 will be the anyvalue
For my current project in codeignitor I needed to make user profile like this
http://domain.com/userid
Then I tried to add this in router.php
$route['(:any)'] = 'profile/user/$1';
Which is working fine. Now I want to make another URL for language like this
http://domain.com/es
http://domain.com/fr
As for both url uri segments are first, when I type
http://domain.com/es
I see the page of
http://domain.com/userid
I am using .htaccess file for removing index.php in codeignitor. Is there any help how can I achive this task in making shot url for multiple controller. Either with .htaccess or router.php?
Because the routes system works from the top down, if you have multiple rules that can match a url, it picks the first one. So you could do:
$route['(es|fr|en)'] = 'language/$1';
$route['(:any)'] = 'profile/user/$1';
If the first rule matches, it runs, otherwise it tests the profile rule.
You will definitely continue running into issues though with that profile rule, and it would be easier if you did something like:
$route['users/(:any)'] = 'profile/user/$1';
That way it would be more clear what the url is doing, and it will help you for when you are writing rules in the future.
This is about setting WebRoot/protected/config/main.php *urlManager* on Yii Framework.
I want a few predefined url like /About /Contact or some other custom special page like /LatestUpdates or /HTCDesire
I can do 'Contact'=>'site/contact' to map /Contact to /site/contact
How can I 'hard code' query string to the route?
I try:
'About'=>'site/page?view=about'
'LatestUpdates'=>'update/view?id=65'
'HTCDesire'=>'product/view?id=33'
but not working, 404 not found because it expect the right side should be route in format controller/action.
I try:
'LatestUpdates'=>'update/view/id/65'
I can browse to /LatestUpdates but the anchor link still shows /update/view/id/65
Is there any other way?
This works for me (except I substituted your values in, so maybe I broke it...):
'LatestUpdates'=>array('update/view','defaultParams'=>array('id'=>'65')),
More about this here and here.
Cheers!