LoginSignup
9
7

More than 5 years have passed since last update.

PhoenixのControllerのパラメータについて

Last updated at Posted at 2015-12-24

Phoenixを勉強しはじめているのですが、ちょっと疑問に思ってたことに対する答えが見つかったので

// hello_controller.ex
def world(conn, %{"name" => name}) do
    render conn, "world.html", name: name
end

パラメータを%{"name" => name}のようの文字列のキーを使うのが一般的なようなのですが、
他の処理ではAtomとか使っているのだけど、ここだけなんでなんだろう?

Programming Phoenixの中のコラムAtom keys vs. string keys?では以下のように書いてあります。

In the world action in our controllers, the external parameters have string keys, "name" => name, while internally we use name: name. That’s a convention followed throughout Phoenix. External data is unsafe, so we explicitly match on the string keys, and then our application boundaries like controllers and channels will convert them into atoms keys which we will rely on everywhere else inside Phoenix.

英語苦手なので正しく読めてるかわかりませんが、こう理解しました。
Phoenixの内部処理ではAtomを使うが、外部とのやり取りではStringのパラメータの使っているのはデータの安全性を考慮してのこと。

文字列のかわりにAtomを使うとなぜ安全じゃないのか?

ちょうど、stackoverflowに質問している人がいました。
http://stackoverflow.com/questions/34446221/atom-keys-vs-string-keys-in-phoenix

Atomの上限が関係しているんですね。
1048576が上限のようで、そういえばE本で読んだことがあったような気がします。

9
7
3

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
9
7