Get Route from Controller and method - playframework-1.x

In Play if I want to get the string form of the route how do I get that from the controller and method declaration?
eg
Application.home()

Check out https://www.playframework.com/documentation/1.4.x/routes#reverse
String url = Router.reverse("Application.home").url;

Related

Ember js encode reserved url characters

I have query param as array: ?cars=[Audi,Fiat]
But ember encode it to ?cars=%5BAudi%2CFiat%5D
How I can get more pretty url like ?cars=[Audi,Fiat] ?
Why ember encodes reserved url characters?
You can override serializeQueryParam and deserializeQueryParam private methods in your route.
https://github.com/emberjs/ember.js/blob/v2.15.1/packages/ember-routing/lib/system/router.js#L703
https://github.com/emberjs/ember.js/blob/v2.15.1/packages/ember-routing/lib/system/router.js#L739
In these methods instead of JSON.stringify and JSON.parse, you can return the value itself and get whatever you want to parse from the string.

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

Routing issue in codeigniter 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

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

How to insert link to controller action in Play framework 2.0 template

If I have an action Application.show(tag: String), and also have a corresponding routing entry, how can I insert a link to this action to a template without crafting the url manually?
I would like to do something like magiclink(Application.show("tag")).
syntax:
<a href='#routes.Application.show("some")'>My link with some string</a>
By analogy you can also generate urls in your controllers. ie. for redirecting after some action:
public static Result justRedirect(){
// use as String
String urlOfShow = routes.Application.index().toString().
// or pass as a redirect() arg
return redirect(routes.Application.show("some"));
}
The format for putting a URL from your routes file in your html is as follow:
#routes.NameOfYourClass.nameOfyourMethod()
So, if in your routes file you have:
GET /products controllers.Products.index()
And your Products class looks like this:
public class Products extends Controller {
public Result index() {
return ok(views.html.index.render());
}
}
Your <a> should look like this:
Products
In addition: If your method can accept parameters, then you can of course pass them in between the parenthesize of your method like this: index("Hi").
I hope this answer is more clear to understand.
The accepted answer is right, but it doesn't cover the case where the controller is in a sub-package, i.e.: controllers.applications.MyFavouriteApplication.show()
Since I had a hard time finding the answer, I'll post it here.
To put a non-scoped link into a template, the proper pattern is #controllers.{sub-packages if any}.routes.{your class}.{your method}()
So in this case it would be #controllers.applications.routes.MyFavouriteApplication.show()
IF you were using the recommended Play pattern of using #Inject to create singleton controller objects, and IF you thought the correct answer was #controllers.applications.MyFavouriteApplication.show(), you would get an error like this:
Object MyFavouriteApplication is not a member of controllers.applications. Note: class MyFavouriteApplication exists, but it has no companion object.
Given that you had already supplied the #Inject() #Singleton annotation, this would seem like a very strange error indeed. It might make you question if you were building the project correctly. Determining the true cause could cost you considerably in blood and treasure.
Ah, as simple as #{routes.Application.show("tag")}.