Alan Souza
Alan Souza

Reputation: 7795

Play Framework: How to change play default packages?

I was just wondering if it is possible to change the default packages from Play. For example: I want to change the "controllers" package to "com.test.controllers". I don't know if this makes any sense, but I just want to know how if it is possible. I did not find anything related to this in play website.

Upvotes: 33

Views: 11320

Answers (5)

yaoviametepe
yaoviametepe

Reputation: 63

Late answer, but for people who can encounter the same situation in 2022, This is possible with Play 2.8.x Scala doc, but is valid for Java too.

You can define your custom package com.test.controllers (even in the root App folder). You need after to define routes for it classes to match Reverse routing as stated in the documentation here.

For example, if you defined a class foo.java that has a method index that is accessible at endpoint /index, your routes file have to reflect it like this:

GET /index  com.test.controllers.foo.index

And you can access it from the HTML file using helper (Reverse routing) like this:

@com.test.controllers.routes.foo.index()

Upvotes: 0

Codemwnci
Codemwnci

Reputation: 54924

Updated to make the distinction between play1 and 2 clear.

For Play 1.x, this is not possible.

No, all controllers must be in a package, or sub package of controllers. If you wanted to keep a com.test package structure, you can do controllers.com.test

For more info, see this thread.

For Play2.x, this is possible. Just move everything to the package you desire. Make sure that the outermost app/ directory stays at outside. An example would be play-project/app/com.company/controllers.

So the simple answer is...it depends, on whst version of Play you are using.

Upvotes: 7

Sam
Sam

Reputation: 407

According to the current Play 2.0 documentation, this is now possible:

Note that in Play 2.0, the controllers, models and views package name conventions are now just that and can be changed if needed (such as prefixing everything with com.yourcompany).

This works well for an empty Play application, there are however some details to take note of for anything else:

  1. Importing custom namespaces into view templates will work for any types except for those that are declared in the first line of a template, which are the arguments for the scala render function that is generated out of a view. Our workaround is to add the full package name to type declarations in the first line of view templates.
  2. For every namespace defined in the routes file (e.g. a custom package and the default package for the Assets route), Play 2.0 generates a corresponding routes source file within the same namespace, so you need to take care to address the correct file when e.g. doing redirection.

Upvotes: 21

Rich
Rich

Reputation: 15767

There is a discussion of this here.

Also there is this in the FAQ.

Upvotes: 1

dogbane
dogbane

Reputation: 274738

From the manual:

A Controller class must be defined in the controllers package and must be a subclass of play.mvc.Controller.

You can add a Java package before the Controller class name if it isn’t defined directly under the controllers package. The controllers package itself is implicit, so you don’t need to specify it.

This means that you can't change your controllers package to com.test.controllers (because the root package must be controllers), but you can change to controllers.com.test.

Upvotes: 13

Related Questions