Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 393
ron Avatar asked Sep 16 '12 14:09

ron


2 Answers

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"));
}
like image 72
biesior Avatar answered Sep 19 '22 15:09

biesior


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:

<a href="@routes.Products.index()">Products</a>

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.

like image 38
David Gatti Avatar answered Sep 21 '22 15:09

David Gatti