How do I add a query parameter to a Play redirect? - scala

If I construct a Play redirect like Redirect(routes.RegistrationController.register()), how can I add a query parameter to the URL I'm redirecting to?
For example, I'd like a URL like this: /register?token=1234.

You can just do Redirect(routes.RegistrationController.register().url + "?token=1234").
Or, supposing your route declares token as an optional parameter with the route declaration GET /register #controllers.RegistrationController(token: Option[Int]), then you could do Redirect(routes.RegistrationController.register(Some(1234))).
There are various variations to how you could set this up. You may want to see the Play router documentation.

Related

go-restful extract URL pattern path from request

I am using a the emicklei/go-restful framework to deal with rest API.
I wish to access the route path from the request. Meaning that when I configure a dummy route like this:
ws := new(restful.WebService)
ws.Path("/resources/names")
ws.Route(ws.GET("/{name}").To(getName))
restful.DefaultContainer.Add(ws)
I wish to access the information that the route was "/resources/names/{name}"
I can access the actual URL which is call by calling:
req.Request.URL.Path
But this will return the specific URL, not the generic one.
Any suggestion?
After more research I finally found that the method req.SelectedRoutePath() will return expected value.

How to add query parameter to routes in Lumen?

I am trying to know how to add query parameters to routes in Lumen
this is an example of a route I created
$app->get('/product/{apikey}','ProductController#getProduct');
This works when I use
http://api.lumenbased.com/product/10920918
but I would like to use it like this
http://api.lumenbased.com/product/?apikey=10920918
I tried this
$app->get('/product/?apikey={apikey}','ProductController#getProduct');
But this gives me MethodNotAllowedHttpException
I would like to know how to write routes with query parameters in Lumen ?
Just do:
$app->get('/product','ProductController#getProduct');
and use:
$request->get('apikey')
in the ProductController#getProduct function.
(That said, validating an API key is better done via middleware...)

Play framework's route always calls the default one

I have the following routes defined:
GET /practiceexams controllers.content.ExamController.allExams
GET /practiceexams/:id controllers.content.ExamController.allExamsForTechnology(id: Int)
My URL to the second route looks like:
http://localhost:9000/practiceexams?id=1000
But for some reason that I can't fathom, the call always lands in the default url which is /practiceexams. Is there anything that I'm missing?
You specified the id in the second route to be a path parameter, but your URL uses it as a query parameter. That's not good.
Try this:
http://localhost:9000/practiceexams/1000
This route defines id as a path parameter:
GET /practiceexams/:id controllers.content.ExamController.allExamsForTechnology(id: Int)
This route defines id as a query parameter:
GET /practiceexams controllers.content.ExamController.allExamsForTechnology(id: Int)
See the documentation for more details.

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")

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.