การตั้งค่าและโครงสร้างโครงงาน Phoenix
สร้างโครงงาน Phoenix ใหม่ ทำความเข้าใจโครงสร้างไดเรกทอรี และสำรวจองค์ประกอบหลักอย่างเราเตอร์และตัวควบคุม
การตั้งค่าและโครงสร้างโครงงาน Phoenix เป็นบทเรียน Elixir & Phoenix: Scalable Backend Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Elixir & Phoenix: Scalable Backend Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Elixir & Phoenix: Scalable Backend Development มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การตั้งค่าและโครงสร้างโครงงาน Phoenix”
สร้างโครงงาน Phoenix ใหม่ ทำความเข้าใจโครงสร้างไดเรกทอรี และสำรวจองค์ประกอบหลักอย่างเราเตอร์และตัวควบคุม คุณปฏิบัติ Elixir & Phoenix: Scalable Backend Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Elixir & Phoenix: Scalable Backend Development หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Elixir & Phoenix: Scalable Backend Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การตั้งค่าและโครงสร้างโครงงาน Phoenix” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Elixir & Phoenix: Scalable Backend Development นี้ได้ไหม
ได้ บทเรียน Elixir & Phoenix: Scalable Backend Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การตั้งค่าและโครงสร้างโครงงาน Phoenix
- การกำหนดเส้นทาง ตัวควบคุม และ Plugs
- มุมมอง แม่แบบ และพื้นฐาน LiveView
- ความสามารถแบบเรียลไทม์ด้วย Phoenix Channels