官术网_书友最值得收藏!

  • Phoenix Web Development
  • Brandon Richey
  • 336字
  • 2021-08-27 18:28:24

The most important directory: lib

Now we'll move on to the real meat of our Phoenix application: lib. Lib is where our application lives. By default, our Phoenix application is broken into two separate ideas. Using the application name of Sampler as an example, we will see two directories: lib/sampler and lib/sampler_web. The idea behind this is that every web application in Phoenix has at least two separate applications that work together to get the job done. The first is divorced from the idea of anything web-related, so there are no controllers or templates, JSON, or anything similar. One of the most common patterns that you’ll see here is all of your database-specific Ecto logic through contexts and schemas.

A context is a collection or boundary of domain-specific logic. For example, if you have users and organizations in your project, you may have a unifying context to those separate schemas called something like Accounts.

A Schema, on the other hand, is the one-to-one mapping of a database table to an application concept. Schemas need to be separated from the functionality of your code and used strictly to describe the shape of the data; this keeps your code pure from side-effects and helps make your code significantly easier to test in the long run.

Finally, we have lib/sampler_web. This is where all of the Phoenix-specific work gets done, so everything related to controller logic, templates, or views goes here in the appropriate sub-directory. Channel logic also falls underneath this particular design pattern. This is also where the endpoint.ex file lives, which defines how Plug constructs the Phoenix response to browser/API consumer/etc. It also determines how requests are parsed and how information is passed along to the Phoenix router.

Speaking of the router, our Phoenix router.ex file also lives here (again, because this is Phoenix-specific logic rather than the overarching common application functionality). This is where we will set up all of the rules to describe how to communicate between the browser and Phoenix's controllers.

主站蜘蛛池模板: 资中县| 会理县| 岳西县| 萨嘎县| 甘德县| 怀柔区| 古交市| 东丰县| 上犹县| 乡宁县| 财经| 怀集县| 远安县| 西丰县| 永清县| 临潭县| 来宾市| 张家口市| 南溪县| 湟源县| 刚察县| 方正县| 华宁县| 阳高县| 沁阳市| 文安县| 娄底市| 即墨市| 巴东县| 毕节市| 满洲里市| 黑山县| 林州市| 贵州省| 安宁市| 拉孜县| 称多县| 垫江县| 罗甸县| 朔州市| 大厂|