How to rewrite URL in sails.js - sails.js

Anyone can help me,i want to rewrite url from http://www.example.in/search?city='xyz'
to
http://www.example.in/city/xyz
in sails.js

Check the url slug section of the documentation.
You could create a route like this one:
// config/routes.js
module.exports.routes = {
// The simple route you use to access to the search controller for now
'get /search': 'SearchController.index'
// The "rewritten" route that calls the same controller
// processing ":city" as if it was part of the query string
'get /city/:city': 'SearchController.index'
}
Then, http://www.example.in/search?city=xyz and http://www.example.in/city/xyz will have the same result, like if you had used url rewriting with Nginx or Apache.

Related

Add a prefix to a bunch of urls

I have a bunch of routes with the same prefix:
# with prefixes
GET /api/v1.0/action1 controllers.Api.action1
GET /api/v1.0/action2 controllers.Api.action2
GET /api/v1.0/action3 controllers.Api.action3
GET /api/v1.0/action4 controllers.Api.action4
# normal urls
GET /action1 controllers.Home.action1
GET /action2 controllers.Home.action2
I want to get rid of repetition of /api/v1.0/. The urls must remain the same, I just want to not write them manually for each url in route file. In Rails it's possible. If there any way to do that?
Either you implement your own router for these actions following James Ropers' post, as mentioned by Rich. Doing so, allows you add the following to your route file:
-> /api/v1.0 YourPathBindableController
Alternatively you can use a plugin, such as navigator, which offers you advanced routing. Your navigator route file would then contain something like:
// Namespace ...
namespace("api"){
namespace("v1"){
GET on "index" to Application.index _
}
}
// ... or with reverse routing support
val api = new Namespace("api"){
val v2 = new Namespace("v2"){
val about = GET on "about" to Application.about _
}
}

Play Framework - Redirect with params

I am trying to figure out how to do a redirect within a controller action in Play (2.0) using Scala.
The redirect using
Redirect(routes.Application.index)
works just fine.
What I cannot figure out from the docs, API, or Google is how to add parameters to the call.
I am coming from Grails where this could be done easily as follows:
redirect action: "index", params: ["key": "value"]
.
The only way I have found is to call Redirect using a string url and a query string, which seems awkward.
Basically I would like to make use of Redirect(Call) somehow, but I do not how to create the Call object using reverse routing.
Am I missing something/not getting the concept in Play/Scala?
Thanks in Advance!
Ellou'
A route is just a function, so you can pass arguments as usual:
// Redirect to /hello/Bob
def helloBob = Action {
Redirect(routes.Application.hello("Bob"))
}
This snippet comes from http://www.playframework.org/documentation/2.0/ScalaRouting (at the bottom)
You can also avoid creating another function just for this in your controller. In your route config, you can simply add something like this:
GET /google #controllers.Default.redirect(to = "http://google.com")

Codeigniter - How to get and change uri segement then redirect?

In my web application I have a url that looks like the following:
http://mydomain.com/search/index/list/for-sale/london/0/0/0/0
I would like to use the uri class and redirect to change $this->uri->segment(3); to map and then redirect.
So that once redirected and the segment has been changed the url would look like:
/search/index/map/for-sale/london/0/0/0/0
How would I go about doing this?
It may need some extra checks, but this would be a simple approach:
$segment_to_replace = "/".$this->uri->segment(3)."/";
$new_url = str_replace ($segment_to_replace, "/map/", current_url());
redirect ($new_url);

Zend framework routing params

I have several routes defined in my application.
When route A is matched and I assemble an URL using route B without resetting, it does not include the current request parameters.
Is there an easy way to include all the request parameters when assembling an URL via a different route than the current route?
I did have a look at Zend_Controller_Router_Rewrite->useRequestParametersAsGlobal, but this will (obviously) also include the request parameters when reset = true
You could try the following.
$oldParams = $this->_getAllParamas();
unset($oldParams['module']);
unset($oldParams['controller']);
unset($oldParams['action']);
Pass
array_merge(array('new'=>'param'),$oldParams)
to your URL view helper.

How would I configure the global.config to allow for root path to actionMethod in MVC2?

specifically, the default directory naming structure is [Controller]/[ActionMethod] resulting in a rendered url like www.mysite.com/home/actionMethodName.
What if I want to simply imply the Controller (in this example, 'home') so that I can get a url that looks like this: www.mysite.com/actionMethodName.
I haven't seen many requests for this kind of configuration. I can see how it breaks convention, but I would imagine that there are lots of people who need root pathing.
Because you are planning to remove the {controller} element of the url, you may need to get a bit more specific with your other urls, e.g.:
routes.MapRoute("MyOtherControllerRoute", "Account/{action}", new { controller = "Account", action = "Index" });
routes.MapRoute("MyDefaultRoute", "{action}", new { controller = "Home", action = "Index" });
When the route table is interrogated, if the url such as www.mysite.com/Account is used, it will match the first route, because we have been specific about the pattern used to match the url. If we then do something like www.mysite.com/DoSomething it will use the default route we've selected last, trying to invoke the DoSomething action on the HomeController type.
Something which I've noticed is that a lot of MVC developers seems to assume that the url is strictly {something}/{something}/{something}, whereas it can essentially be anything you like, e.g, I can have a route that does: www.mysite.com/my-weird-and-wonderful-url which I could map specifically:
routes.MapRoute("Somewhere", "my-weird-and-wonderful-url", new { controller = "Whatever", action = "Whenever" });
Hope that helps.
Easy as apple pie - you just specify a route of your own! =)
An example:
routes.MapRoute(
"RootPathing",
"{action}",
new { controller = "Default", action = "Index" });
This will register a route that catches all paths, and try to map them to the DefaultController with an action name corresponding to the path. However, note that if you place this route above the included default route, you will not be able to reach any other controller than the DefaultController - hence, place this route below the default route in the chain. It will then be matched by all paths that don't match a controller name.
When debugging routes, Phil Haack's Routing Debugger is really worth taking a look at.