기존 API 이름 변경 ①: URL, 컨트롤러 및 뷰의 모듈 이름
9624 단어 fukuoka.exElixirPhoenix
방문해 주셔서 감사합니다

mix phx.gen.json에서 자동 생성한 API의 리네임을 하고 싶을 때, 어디를 고쳐야 하고, 고치지 않아도 좋은 곳이 어딘가, 이마이치 알기 어렵기 때문에, 정리해 보았습니다
전반부는 'URL'과 '컨트롤러 및 뷰 모듈 이름'의 이름 바꾸기입니다.
"Phoenix"은 Elixir 웹 프레임 워크입니다.
내용이 재미 있거나 도움이되면 "좋아요"잘 부탁드립니다

본 칼럼의 검증 환경, 사전 구축의 커멘드
본 칼럼은, 이하 환경에서 검증하고 있습니다(Windows에서 실시하고 있습니다만, Linux나 mac에서도 동작한다고 가정합니다)
또, 이하의 커멘드로 Phoenix PJ+JSON API를 구축하고 있습니다
mix phx.new api_sample --no-webpack
cd api_sample
mix ecto.create
mix phx.gen.json Api Post post title:string body:string
mix ecto.migrate
【手動でルーティング追加とprotect_from_forgery無効化をする】
iex -S mix phx.server
URL을 변경할 때
URL을 http://localhost:4000/post
에서 http://localhost:4000/hoge
로 변경하면 router.ex의 "resource"나 "get"과 같은 첫 번째 인수의 경로를 다시 쓰고 컨트롤러의 create () 함수 내에서 호출됩니다. Routes.xxxx_path()를 이름 바꾸어야합니다.
lib/api_sample_web/router.exdefmodule ApiSampleWeb.Router do
…
scope "/", ApiSampleWeb do
…
resources "/hoge", PostController, except: [:new, :edit]
# modify here-^
…
lib/api_sample_web/controllers/post_controller.exdefmodule ApiSampleWeb.PostController do
…
def create(conn, %{"post" => post_params}) do
with {:ok, %Post{} = post} <- Api.create_post(post_params) do
conn
|> put_status(:created)
|> put_resp_header("location", Routes.hoge_path(conn, :show, post))
# modify here--------------------------^
|> render("show.json", post: post)
end
end
…
컨트롤러 또는 뷰의 모듈 이름을 변경할 때
컨트롤러의 모듈명을 변경할 때는 3개의 재기록 대상이 나옵니다.
먼저 router.ex가 호출하는 컨트롤러 이름입니다.
lib/api_sample_web/router.exdefmodule ApiSampleWeb.Router do
…
scope "/", ApiSampleWeb do
…
resources "/post", HogeController, except: [:new, :edit]
# modify here--------^
…
그런 다음 컨트롤러의 모듈 이름입니다.
덧붙여 여기에서는 파일명도 변경하고 있습니다만, 필수는 아닙니다(하지만, 바꾸어 두는 것이 무난합니다)
lib/api_sample_web/controllers/post_controller.ex ※변경 전defmodule ApiSampleWeb.PostController do
…
lib/api_sample_web/controllers/hoge_controller.ex ※변경 후defmodule ApiSampleWeb.HogeController do
# modify here---------^
…
그런 다음 뷰 이름 바꾸기도 필요합니다 (여기도 파일 이름 변경은 필수는 아닙니다)
lib/api_sample_web/views/post_view.ex ※변경 전defmodule ApiSampleWeb.PostView do
use ApiSampleWeb, :view
alias ApiSampleWeb.PostView
…
lib/api_sample_web/views/hoge_view.ex ※변경 후defmodule ApiSampleWeb.HogeView do
# modify here--------^
use ApiSampleWeb, :view
alias ApiSampleWeb.HogeView
# modify here--------^
…
이것으로 괜찮습니다.
컨트롤러와 뷰의 모듈 이름이 일치하지 않으면 ...
이런 오류 화면이 나옵니다.

컨트롤러의 모듈 이름을 기준으로 자동으로 뷰의 모듈 이름을 확인한 것 같습니다.
끝
기존 API URL, 컨트롤러/뷰의 모듈 이름을 이름 바꾸는 방법에 대해 설명했습니다.
운용중에 URL을 변경하지 않으면 안 되었을 때나, 컨트롤러/뷰의 모듈명을 철자 미스 해 버렸다…
다음 번에는 요청 JSON 및 응답 JSON 키를 이름 바꾸는 방법에 대해 설명합니다. 예정입니다
p.s.「좋아요」 잘 부탁드립니다
페이지 왼쪽 상단의
또는
을 클릭해 주셔서 감사합니다 
여기의 숫자가 늘어나면, 작가로서는 「우케하고 있다」라는 감각을 얻을 수 있어 연재를 더욱 진화시켜 나가는 동기 부여가 되기 때문에, 보다 Elixir 재료를 보고 싶다고 하는 당신, 저희와 함께 북돋워 제발!
Reference
이 문제에 관하여(기존 API 이름 변경 ①: URL, 컨트롤러 및 뷰의 모듈 이름), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/piacerex/items/688b3e2542bc67ef52c7
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
defmodule ApiSampleWeb.Router do
…
scope "/", ApiSampleWeb do
…
resources "/hoge", PostController, except: [:new, :edit]
# modify here-^
…
defmodule ApiSampleWeb.PostController do
…
def create(conn, %{"post" => post_params}) do
with {:ok, %Post{} = post} <- Api.create_post(post_params) do
conn
|> put_status(:created)
|> put_resp_header("location", Routes.hoge_path(conn, :show, post))
# modify here--------------------------^
|> render("show.json", post: post)
end
end
…
컨트롤러의 모듈명을 변경할 때는 3개의 재기록 대상이 나옵니다.
먼저 router.ex가 호출하는 컨트롤러 이름입니다.
lib/api_sample_web/router.ex
defmodule ApiSampleWeb.Router do
…
scope "/", ApiSampleWeb do
…
resources "/post", HogeController, except: [:new, :edit]
# modify here--------^
…
그런 다음 컨트롤러의 모듈 이름입니다.
덧붙여 여기에서는 파일명도 변경하고 있습니다만, 필수는 아닙니다(하지만, 바꾸어 두는 것이 무난합니다)
lib/api_sample_web/controllers/post_controller.ex ※변경 전
defmodule ApiSampleWeb.PostController do
…
lib/api_sample_web/controllers/hoge_controller.ex ※변경 후
defmodule ApiSampleWeb.HogeController do
# modify here---------^
…
그런 다음 뷰 이름 바꾸기도 필요합니다 (여기도 파일 이름 변경은 필수는 아닙니다)
lib/api_sample_web/views/post_view.ex ※변경 전
defmodule ApiSampleWeb.PostView do
use ApiSampleWeb, :view
alias ApiSampleWeb.PostView
…
lib/api_sample_web/views/hoge_view.ex ※변경 후
defmodule ApiSampleWeb.HogeView do
# modify here--------^
use ApiSampleWeb, :view
alias ApiSampleWeb.HogeView
# modify here--------^
…
이것으로 괜찮습니다.
컨트롤러와 뷰의 모듈 이름이 일치하지 않으면 ...
이런 오류 화면이 나옵니다.

컨트롤러의 모듈 이름을 기준으로 자동으로 뷰의 모듈 이름을 확인한 것 같습니다.
끝
기존 API URL, 컨트롤러/뷰의 모듈 이름을 이름 바꾸는 방법에 대해 설명했습니다.
운용중에 URL을 변경하지 않으면 안 되었을 때나, 컨트롤러/뷰의 모듈명을 철자 미스 해 버렸다…
다음 번에는 요청 JSON 및 응답 JSON 키를 이름 바꾸는 방법에 대해 설명합니다. 예정입니다
p.s.「좋아요」 잘 부탁드립니다
페이지 왼쪽 상단의
또는
을 클릭해 주셔서 감사합니다 
여기의 숫자가 늘어나면, 작가로서는 「우케하고 있다」라는 감각을 얻을 수 있어 연재를 더욱 진화시켜 나가는 동기 부여가 되기 때문에, 보다 Elixir 재료를 보고 싶다고 하는 당신, 저희와 함께 북돋워 제발!
Reference
이 문제에 관하여(기존 API 이름 변경 ①: URL, 컨트롤러 및 뷰의 모듈 이름), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/piacerex/items/688b3e2542bc67ef52c7
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
페이지 왼쪽 상단의



여기의 숫자가 늘어나면, 작가로서는 「우케하고 있다」라는 감각을 얻을 수 있어 연재를 더욱 진화시켜 나가는 동기 부여가 되기 때문에, 보다 Elixir 재료를 보고 싶다고 하는 당신, 저희와 함께 북돋워 제발!

Reference
이 문제에 관하여(기존 API 이름 변경 ①: URL, 컨트롤러 및 뷰의 모듈 이름), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/piacerex/items/688b3e2542bc67ef52c7텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)