Playframework 路线文件:将生产路线与开发路线分开 [英] Playframework route file: Separate Production routes from Dev routes

查看:22
本文介绍了Playframework 路线文件:将生产路线与开发路线分开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

如果您在 Dev 模式下尝试访问 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 路线文件:将生产路线与开发路线分开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆