Phoenix概述
Phoenix是一款使用Elixir编写的Web开发框架。它实现了服务端的MVC架构。它的许多组件和概念类似于我们常见的框架如Django和Ruby on Rails。
Phoenix提供了开发和生成环境的最佳实践,即高生产效率和高应用性能。它同时也有一些有趣的特性,如使用channels来实现实时特性和预编译的模板。
如果你已经对Elixir很熟悉,那么你可以直接开始Phoenix的学习,如果不是,你也可以查阅Elixir guides来学习它。我们有一系列的帮助资源Learning Elixir and Erlang Guide。
这个引导教程的目的是给你一个简明的Phoenix概览。包括了Phoenix的组成部分和支持Phoenix的不同层级。
Phoenix
Phoenix可以看做一系列模块组成的多层系统。其他层级包括Plug,Ecto等。Erlang的HTTP服务器Cowboy作为Phoenix和Plug的底层基础,这里Phoenix将Cowboy封装好供大家轻松使用。
Phoenix由一系列不同部分构成,每部分都有它的目的。这里是一个概览:
-
The Endpoint(入口)
- handles all aspects of requests up until the point where the router takes over
- provides a core set of plugs to apply to all requests
- dispatches requests into a designated router
-
The Router(路由)
- parses incoming requests and dispatches them to the correct controller/action, passing parameters as needed
- provides helpers to generate route paths or urls to resources
- defines named pipelines through which we may pass our requests
- Pipelines
*allow easy application of groups of plugs to a set of routes
-
Controllers(控制器)
- provide functions, called actions, to handle requests
- Actions
- prepare data and pass it into views
- invoke rendering via views
- perform redirects
-
Views(视图)
- render templates
- act as a presentation layer
- define helper functions, available in templates, to decorate data for presentation
-
Templates(模板)
- are what they sound like :)
- are precompiled and fast
-
Channels(频道)
- manage sockets for easy realtime communication
- are analogous to controllers except that they allow bi-directional communication with persistent connections
-
PubSub(发布订阅)
- underlies the channel layer and allows clients to subscribe to topics
- abstracts the underlying pubsub adapter for third-party pubsub integration
Plug(插件)
Plug is a specification for constructing composable modules to build web applications. Plugs are reusable modules or functions built to that specification. They provide discrete behaviors - like request header parsing or logging. Because the Plug API is small and consistent, plugs can be defined and executed in a set order, like a pipeline. They can also be re-used within a project or across projects.
Plugs can be written to handle almost anything, from authentication to parameter pre-processing, and even rendering.
Phoenix takes great advantage of Plug in general - the router and controllers especially so.
One of the most important things about Plug is that it provides adapters to HTTP servers which will ultimately deliver application content to our users. Currently Plug only provides an adapter forCowboy, a web server written in Erlang by Loïc Hoguin of 99s.
Have a look at the Plug Guide for more details.
Ecto(类似ORM库,类似LINQ)
Ecto is a language integrated query composition tool and database wrapper for Elixir. With Ecto, we can read and write to different databases, model our domain data, write complex queries in a type-safe way, protect against attack vectors - including SQL injection, and much more.
Ecto is built around four main abstractions:
Repo - A repository represents a connection to an individual database. Every database operation is do,lei'sne via the repository.
Model - Models are our data definitions. They define table names and fields as well as each field's type. Models also define associations - the relationships between models.
Query - Queries tie both models and repositories together, allowing us to elegantly retrieve data from the repository and cast it into the models themselves.
Changeset - Changesets declare transformations we need to perform on our model data before our application can use it. These include type casting, validations, and more.
A new Phoenix application will use Ecto with PostgreSQL storage by default.
总结
Phoenix作为Meteor社区广泛讨论的竞争对手或替代品势必将得到关注。但这个并不仅仅是Phoenix VS Meteor的事,而是Erlang VS NodeJS的事。学好JavaScript和NodeJS,我们可以开发从前端到后端,从移动端到桌面的任何应用。所以,当前我认为若想成为全栈开发者,还是先打好JavaScript和NodeJS的基础,然后学习Meteor和Electron,这样是通吃三端的最高效的方式。以后局部的优化可能会用到Erlang和Phoenix。