LoginSignup
0
0

More than 5 years have passed since last update.

[PlayFramework] 03. HTTP Routing

Posted at

Scalaの一番有名なフレームワークはPlay Frameworkである。

Play Frameworkに対して簡単に勉強しようと。

特に話がない内容はPlayFramework公式ホームページの内容を基に作成。

HTTP Routing

内装のHTTPルータ

RouterはActionに来るHTTP Requestを訳すコンポーネントである。

MVC Frameworkで、HTTP RequestはEventに扱われる。

Eventは2つの情報のパートを含めている。

  1. クエリ文字列を含めたRequest Path(e.g. /clients/1542, /photos/list)

  2. HTTP Method(e.g. GET, POST)

Routesはconf/routesファイルに書いてある。

routesファイルのSyntax

conf/routesはルータに使われる設定ファイルである。

各ルーツはHTTP MethodURI patternで構成されている。

2つのパートで構成されているルーツでActionが呼ばれる。

GET     /clients/:id        controllers.Clients.show(id: Long)
->      /api                api.MyRouter

一般的にはHTTP MethodURIControllerの関数の指定でルーツを書く。

しかし、別のroutesファイルを作成した場合には->URIControllerの関数を書く。

Note

  • URIのパターンは1つ以上のDynamicパートで構成可能

  • Regular Expressionの適応も可能。

HTTP Method

PlayはHTTPがサポートするすべてのメソッドの使用が可能。

GETPATCHPOSTPUTDELETEHEADのすべてが使用可能。

Reverse Routing

def helloBob = Action {
    implicit request => Redirect(routes.<Controller>.<method>(<params>))
}

Default Controller

PlayにはDefault Controllerが内装されている。

// 303 See Other
GET     /about      controllers.Default.redirect(to = "http://~")

// 404 Not Found
GET     /orders     controllers.Default.notFound

// 500 Internal Server Error
GET     /clients    controllers.Default.error

// 501 Not Implemented
GET     /posts      controllers.Default.todo
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