0

Triển khai ứng dụng Elixir Phoenix (P1)

Giới thiệu

  • Elixir là một ngôn ngữ lập trình hàm phát triển bởi Jose Valim, cựu thành viên phát triển Rails & Devise. Elixir dựa trên Erlang và chạy trên nền máy ảo BEAM
  • Phoenix là một Elixir web framework, sử dụng Elixir và được inspired từ Rails

Cài đặt

Elixir

Để cài đặt elixir ta làm theo hướng dẫn https://elixir-lang.org/install.html

macOS

brew install elixir

ubuntu/debian

wget https://packages.erlang-solutions.com/erlang-solutions_2.0_all.deb && sudo dpkg -i erlang-solutions_2.0_all.deb
sudo apt-get update
sudo apt-get install esl-erlang
sudo apt-get install elixir

Phoenix

Để cài đặt Phoenix ta làm theo hướng dẫn: https://hexdocs.pm/phoenix/installation.html

mix archive.install hex phx_new

Khởi tạo

Để khởi tạo 1 dự án Phoenix ta cần chạy câu lệnh mix phx.new [tên_dự_án] (ở đây tôi đặt tên dự án là blog_phx, sau đó cd vào thư mục và chạy mix ecto.create để tạo config ecto kết nối đến database:

mix phx.new blog_phx

Output sẽ dạng như sau:

Database

  • Mặc định dự án Phoenix mặc định kết nối với database Postgres. Để đổi sang mysql lúc khởi tạo dự án bạn thêm --database mysql ở bước trên.
  • Ứng với mỗi môi trường, phoenix đặt cấu hình database trong từng file cấu hình riêng thay vì nằm trong 1 file database.yml như Rails
  • Ở folder dự án bạn thay đổi file config/dev.exs, đảm bảo database username & password là đang đúng.

  • Chạy mix ecto.create để khởi tạo database

Tạo nhanh 1 trang CRUD

Ở đây ta dùng câu lênh mix phx.gen.html Accounts User users name:string age:integer, câu lệnh này sẽ tạo ra đủ cấu trúc controller, view và model.

Câu lệnh này gần giống với rails generate scaffold trong Rails

	new file:   lib/blog_phx/accounts.ex
	new file:   lib/blog_phx/accounts/user.ex
	new file:   lib/blog_phx_web/controllers/user_controller.ex
	new file:   lib/blog_phx_web/templates/user/edit.html.eex
	new file:   lib/blog_phx_web/templates/user/form.html.eex
	new file:   lib/blog_phx_web/templates/user/index.html.eex
	new file:   lib/blog_phx_web/templates/user/new.html.eex
	new file:   lib/blog_phx_web/templates/user/show.html.eex
	new file:   lib/blog_phx_web/views/user_view.ex
	new file:   priv/repo/migrations/20210320082002_create_users.exs
	new file:   test/blog_phx/accounts_test.exs
	new file:   test/blog_phx_web/controllers/user_controller_test.exs

Thay đổi file quản lý router lib/blog_phx_web/router.ex, thêm resources "/users", UserController:

  scope "/", BlogPhxWeb do
    pipe_through :browser

    get "/", PageController, :index
    resources "/users", UserController
  end

Sau đó chạy mix ecto.migrate để chạy migrate thêm bảng users vào database

16:46:15.421 [info]  == Running 20210320082002 BlogPhx.Repo.Migrations.CreateUsers.change/0 forward

16:46:15.423 [info]  create table users

16:46:15.428 [info]  == Migrated 20210320082002 in 0.0s

Bật server bằng mix phx.server vào trang http://localhost:4000/users là ta có thể thấy được trang CRUD mới

Mời các bạn đón đọc các bài tiếp theo ở phần sau.


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí