LoginSignup
0
0

More than 3 years have passed since last update.

routes ファイルの modifier 構文

Last updated at Posted at 2019-09-26

Play では routes ファイルで HTTP ルーティングを記述します。各 route には modifier というマークが付けられます。

例えば次の + nocsrf の部分。

+ nocsrf
POST  /api/new              controllers.Api.newThing

Play のドキュメント では

It is also possible to apply modifiers by preceding the route with a line starting with a +. This can change the behavior of certain Play components.

と書かれていますが、具体的な構文が書かれていません。

routes ファイルをパースする部分のソースを見ると

  def modifiers: Parser[List[Modifier]] =
    "+" ~> ignoreWhiteSpace ~> repsep("""[^#\s]+""".r, separator) <~ ignoreWhiteSpace ^^ (_.map(Modifier.apply))

separatornamedError(whiteSpace, "Whitespace expected") で、 whiteSpace"""[ \t]+""".r です。

宣言的でわかりやすいですね。
大雑把に言えば 「+ のあとに whiteSpace 区切りで文字列を並べる」 という構文のようです。
+ foo bar baz とすれば foo, bar, baz が modifier として設定されます。

play.api.routing.Router.RequestImplicits._ を import すると、
RequestHeader に対して hasRouteModifier(String) が呼び出せるので、リクエストが modifier の付いた route を通ってきたかわかります。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0