Phoenix 项目设置与结构
创建新的 Phoenix 项目,了解其目录结构,并探索路由器和控制器等核心组件。
Phoenix 项目设置与结构 是 CoddyKit 上的免费 Elixir & Phoenix: Scalable Backend Development 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Elixir & Phoenix: Scalable Backend Development 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Elixir & Phoenix: Scalable Backend Development 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Welcome to Phoenix!
Hello! In this lesson, we'll begin our journey with Phoenix, Elixir's powerful web framework. Phoenix helps you build robust, high-performance web applications and APIs.
It leverages the speed and fault-tolerance of the Erlang Virtual Machine (BEAM) through Elixir, making it ideal for real-time features and concurrent systems.
Why Choose Phoenix?
Phoenix combines the elegance of Elixir with the reliability of Erlang/OTP. This means your applications can handle many concurrent users with low latency and are highly resilient to failures.
It's a fantastic choice for building anything from simple websites to complex real-time applications like chat rooms or live dashboards.
Setup Your Environment
Before we create a Phoenix project, ensure you have Elixir and Erlang installed on your system. You can check your versions using the commands below.
If you don't have them, consider using asdf for easy installation and version management.
elixir -v
mix -vGenerating a New Project
The easiest way to start a new Phoenix project is by using the mix phx.new command. This command sets up a new application with all the necessary files and dependencies.
Let's create our first project, named hello_phoenix:
mix phx.new hello_phoenixUnderstanding Project Structure
After running mix phx.new hello_phoenix, a new directory named hello_phoenix is created. Inside, you'll find a well-organized set of folders and files.
This structure helps keep your code modular and maintainable as your application grows.
Key Project Folders
Let's look at some of the top-level directories:
assets/: Contains your frontend files like CSS, JavaScript, and images.config/: Holds configuration files for different environments (development, test, production).priv/: Stores private application resources, such as database migrations and static files not served directly.
`lib/` - Your Application Code
The lib/ directory is the heart of your Elixir application. For Phoenix projects, it usually contains two main sub-directories:
lib/hello_phoenix/: Your core application logic and business rules.lib/hello_phoenix_web/: Web-specific components like controllers, views, and routers.
This separation helps keep your web interface distinct from your core logic.
hello_phoenix/
├── lib/
│ ├── hello_phoenix/
│ └── hello_phoenix_web/Introducing the Router
Inside lib/hello_phoenix_web/, you'll find router.ex. The router is like the traffic cop of your web application.
It defines how incoming web requests (like visiting a URL) are mapped to specific actions within your application, typically handled by controllers.
# lib/hello_phoenix_web/router.ex
defmodule HelloPhoenixWeb.Router do
use HelloPhoenixWeb, :router
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_flash
plug :put_root_layout, {HelloPhoenixWeb.LayoutView, :root}
plug :protect_from_forgery
plug :put_secure_browser_headers
end
scope "/", HelloPhoenixWeb do
pipe_through :browser
get "/", PageController, :index
end
endRunning Your Phoenix App
After generating your project and installing dependencies (mix deps.get), you can start the development server. This compiles your code and makes your application accessible in your browser, usually at localhost:4000.
Navigate into your project directory first!
cd hello_phoenix
mix phx.serverQuick Check: Project Structure
The mix phx.new command creates a standard Phoenix project. Which directory is typically used for frontend assets like CSS and JavaScript?
Lesson Summary
Great job! You've successfully created your first Phoenix project using mix phx.new. We explored the basic directory structure, including assets/, config/, lib/, and priv/.
You now have an understanding of where your core application logic lives (lib/hello_phoenix) and where web-specific components reside (lib/hello_phoenix_web), including the crucial router.ex. Next, we'll dive deeper into how routing works and how controllers handle requests!
常见问题解答
「Phoenix 项目设置与结构」课时是免费的吗?
是的 — 「Phoenix 项目设置与结构」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Elixir & Phoenix: Scalable Backend Development 课程的其余内容,请升级到 CoddyKit PRO。 Elixir & Phoenix: Scalable Backend Development 课程共包含 4 节课。
「Phoenix 项目设置与结构」这节课中我会学到什么?
创建新的 Phoenix 项目,了解其目录结构,并探索路由器和控制器等核心组件。 你通过在浏览器中直接运行的动手代码来练习 Elixir & Phoenix: Scalable Backend Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Elixir & Phoenix: Scalable Backend Development 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Elixir & Phoenix: Scalable Backend Development 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「Phoenix 项目设置与结构」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Elixir & Phoenix: Scalable Backend Development 课中编写并运行代码吗?
能。每节 Elixir & Phoenix: Scalable Backend Development 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- Phoenix 项目设置与结构
- 路由、控制器与 Plugs
- 视图、模板与 LiveView 基础
- 使用 Phoenix Channels 实现实时功能