问题描述
Play中是否有一种方法可以对路线进行注释,以通知某些路段/组路线仅在开发或生产模式下可用
Is there a way in Play to annotate routes to inform that a certain section/group routes is only available in dev or prod mode
推荐答案
好吧,这没有记录,所以我不确定是否有意做到这一点,但是我找到了一种使这项工作可行的方法. 但是请注意,由于这是一个未记录的功能,可能意味着它是意料之外的,因此可能会在以后的播放版本中中断.
Well, this is not documented, so I am not sure if this is intentionally possible or not, but I have found a way to make this work. Please note however, as this is an undocumented feature, may mean it is unintended, and therefore may break in future versions of play.
您可以使用路由文件中的以下行来实现所需的功能.
You are able to achieve what you want using the following line in your routes file.
%{ if (play.mode.isDev()) }%
我通过几个操作创建了一个测试应用程序
I created a test application with a couple of actions
public class Application extends Controller {
public static void index() {
render();
}
public static void noDev() {
renderText("NoDev");
}
public static void noProd() {
renderText("NoProd");
}
}
然后我将以下内容添加到我的路线文件中
I then added the following to my routes file
# Home page
GET / Application.index
# Ignore favicon requests
GET /favicon.ico 404
# Map static resources from the /app/public folder to the /public path
GET /public/ staticDir:public
%{ if (play.mode.isDev()) }%
GET /route1 Application.noDev
GET /route2 Application.noDev
GET /route3 Application.noDev
* /{controller}/{action} {controller}.{action}
%{ if (play.mode.isProd()) }%
GET /route4 Application.noProd
GET /route5 Application.noProd
GET /route6 Application.noProd
* /{controller}/{action} {controller}.{action}
因此,您可以看到,使用简单的if语句,它将仅在该模式下执行下一组路由.如果找到下一个if语句,则if语句将结束.
So, you can see that using a simple if statement, it will execute the next group of routes only in that mode. The if statement will end when the next if statement is found.
如果在开发人员模式下尝试访问route4,将无法访问它,并且会看到RouteNotFound页面,其中显示可用路由仅是为Dev定义的路由.
If in Dev mode you try to access route4, you will not be able to access it, and you will see the RouteNotFound page showing that the available routes are those that you have defined for Dev only.
这篇关于Playframework路线文件:将生产路线与开发路线分开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!