Routing issue in codeigniter regex - regex

Route are defined as
$route['hotels/([a-z]+)/(:any)'] = "hotels/city/$1/$2";
$route['hotels/placename/fivestar'] = "hotels/placename/star/fivestar";
I want to execute the city action in hotels controller when some one type url like
http://website.com/hotels/myspecificcity/fivestar
the above first route is not working. it always load 2nd route . can some one please guide me
i don't want to remove 2nd route though as i will using for other purposes
Thanks

Related

Express.JS - Dynamic Route Alias

I am designing a community site that will allow users to login and befriend others, and their account will be accessible via the following routes:
https://domain.com/en/community/id/:userid AND
https://domain.com/en/community/users/:username
However I want to always want users to use the first URL as it looks more aesthetically pleasing. I could just redirect straight from the ID route, but with the planned friends and groups, I will end up with URLS such as:
https://domain.com/en/community/id/:userid/friends
https://domain.com/en/community/users/:username/friends
https://domain.com/en/community/id/:userid/groups
https://domain.com/en/community/users/:username/groups
How can I always redirect from /id/:userid to /users/:username but keep the rest of the URL?
The first task is matching all URLs with /id/:userid to use the same route callback.
The second task is to replace the /id/:userid with /users/:username.
Can these two tasks be achieved with Regular Expressions?
Thank you very much.
Okay, it turns out it was very simple to allow the user ID alias.
I can use an asterisk to capture all the other URLs, and then use a simple RegExp replace call to replace the ID with the Username.
app.get('/:lang/community/id/:id/*', function(req, res){
var id = req.params.id;
User.getByID(id, function(err, result, user){
var url = req.url.replace(new RegExp('/id/'+id, 'g'), '/users/'+user.name);
res.redirect(url);
}
});
This is the best solution I could think of. I hope it helps other people.

express router not working with routes that include regex

I'm new to node and unable to create a simple route which will include regex as on of the parameter
// student.js - route file for route /student
app.get('/student/:/^[a-z0-9-]+$/', function(req,res){
res.send('student found');
});
when i hit localhost:3000/student/student-slug it says Cannot GET /student/student-slug
two more question
1) how to get param which is of regex, usually we can do this var _student = res.param.student_name but i'm unable to think for the regex
2) how to set optional param, let's say for pagination, route is like
/list/students/ will show list of last x student but /list/students/48 will offset that value to 48th row
this question may be duplicate but i'm unable to find answer
You need to encode the uri string before pass to request and decode it in your route handler.
Usage is very clear:
encodeURIComponent(str);
And for decoding use:
decodeURIComponent(str);
check the official documentation here
also do checkout this blog post on escape vs encode vs encodeURIComponent

Codeigniter URI route /$USERNAME to /user/profile/USERNAME

I have symbol "$" allowed in URL. Every user of my application has a username. I want to show their profiles with website.com/$USERNAME. I want to route /$USERNAME TO /users/profile/USERNAME. I am trying this
when I hard code it like this
$route['$username'] = "user/profile/username";
its working but when i try it like this...
$route['$(:any)'] = "user/profile/$1";
its not working...
Any help/suggestions?

Mapping urls in routes.py

I am using web2py and builing a REST api and have one of my URLs set up like this:
routes_in (
('/myapp/something/{?P<id>.*)/myfunction', /myapp/default/myfunction/\g<id>')
)
routes_out = (
('/myapp/default/myfunction/\g<id>', '/myapp/something/{?P<id>.*)/myfunction')
)
If my app is setup this way my function is not even entered into and I get an invalid request if I remove the id argument from the url that my url is being mapped to i.e. remove g<id> from above, I enter my function but the argument is not being captured.
I cannot change the structure of the URL as per my requirements and I am not sure how to go about this.
I would appreciate any pointers.
Thanks,
nav
The above does work in web2py I found that some other area of my code was breaking.

Zend Framework how to configure a router GET parameters

URL
somedomain.com/?_escaped_fragment_
try it:
routes.escaped-fragment.type = "Zend_Controller_Router_Route_Regex"
routes.escaped-fragment.route = "\?_escaped_fragment_"
routes.escaped-fragment.defaults.controller = "index"
routes.escaped-fragment.defaults.action = "someaction"
but he does not see the GET parameter ?_escaped_fragment_
and run
IndexController::indexAction
The ZF router operates on the path only, query string params are stripped off before the route matching occurs, so you're not going to be able to get this working easily. Your options are:
Change your URL structure
Rewrite this URL in .htaccess
Extend/replace the default router to check the query string before doing the standard routing