Phoenix generator phoenix.gen.json vs mix phx.gen.json

时间:2017-08-23 19:19:09

标签: elixir phoenix-framework

我想知道与phoenix.gen.jsonphx.gen.json的区别是什么。

我目前正在使用Phoenix 1.3。 Ubuntu 16.04LTS。和以下版本的Erlang / Elixir

Erlang/OTP 20 [erts-9.0] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:10] [hipe] [kernel-poll:false]

Elixir 1.5.1

使用phoenix.gen.json

$>  mix phoenix.gen.json Session sessions session_id:string
mix phoenix.gen.json is deprecated. Use phx.gen.json instead.
* creating web/controllers/session_controller.ex
* creating web/views/session_view.ex
* creating test/controllers/session_controller_test.exs
* creating web/views/changeset_view.ex
mix phoenix.gen.model is deprecated. Use phx.gen.schema instead.
* creating web/models/session.ex
* creating test/models/session_test.exs
* creating priv/repo/migrations/20170823190558_create_session.exs

Add the resource to your api scope in web/router.ex:

    resources "/sessions", SessionController, except: [:new, :edit]

Remember to update your repository by running migrations:

    $ mix ecto.migrate

使用phx.gen.json

$> mix phx.gen.json API ContentAsset content_assets content_id:integer
* creating lib/my_app_web/controllers/content_asset_controller.ex
* creating lib/my_app_web/views/content_asset_view.ex
* creating test/my_app_web/controllers/content_asset_controller_test.exs
* creating lib/my_app_web/views/changeset_view.ex
* creating lib/my_appi_web/controllers/fallback_controller.ex
* creating lib/my_app/api/content_asset.ex
* creating priv/repo/migrations/20170823191342_create_content_assets.exs
* creating lib/my_app/api/api.ex
* injecting lib/my_app/api/api.ex
* creating test/my_app/api/api_test.exs
* injecting test/my_app/api/api_test.exs

Add the resource to your :api scope in lib/my_app_web/router.ex:

    resources "/content_assets", ContentAssetController, except: [:new, :edit]


Remember to update your repository by running migrations:

    $ mix ecto.migrate

我们可以看到这两个生成器不会生成相同的文件集。

更重要的是,当我们运行开发服务器时。我看到phx.gen.json文件中存在以下错误。

$> mix phx.server
Compiling 12 files (.ex)

== Compilation error in file lib/my_app_web/views/content_asset_view.ex ==
** (CompileError) lib/my_app_web/views/content_asset_view.ex:2: module MyAppWeb is not loaded and could not be found
    (elixir) expanding macro: Kernel.use/2
    lib/my_app_web/views/content_asset_view.ex:2: MyAppWeb.ContentAssetView (module)
    (elixir) lib/kernel/parallel_compiler.ex:121: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/1

1 个答案:

答案 0 :(得分:3)

与phoenix.gen.json和phx.gen.json的显着区别是上下文模块。它是来自领域驱动设计的“有界上下文”的概念。

  

上下文是一个Elixir模块,用作API的边界   给定资源。上下文通常包含许多相关资源。   因此,如果上下文已经存在,它将被扩充   给定资源的函数。注意,资源也可以拆分   在不同的上下文(例如Accounts.User和Payments.User)。

https://hexdocs.pm/phoenix/Mix.Tasks.Phx.Gen.Json.html

在您的情况下,您在 lib / my_app / api / api.ex 中生成了 API 上下文模块。

您可能希望将 API 上下文模块重命名为内容

== Compilation error in file lib/my_app_web/views/content_asset_view.ex ==
** (CompileError) lib/my_app_web/views/content_asset_view.ex:2: module MyAppWeb is not loaded and could not be found
    (elixir) expanding macro: Kernel.use/2
    lib/my_app_web/views/content_asset_view.ex:2: MyAppWeb.ContentAssetView (module)
    (elixir) lib/kernel/parallel_compiler.ex:121: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/1

根据您的错误消息,很可能未定义 MyAppWeb 模块。

有关上下文的更多学习资源:

https://www.youtube.com/watch?v=tMO28ar0lW8&feature=youtu.be&t=12m21s

https://martinfowler.com/bliki/BoundedContext.html

http://phoenixframework.org/blog/phoenix-1-3-0-released