3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

strapiを使ってみた

Last updated at Posted at 2020-01-25

概要

HeadlessCMSを自前で用意するため、オープンソースのstrapiを利用してみました。
構築からコンテンツ登録までの手順をご紹介します。

環境構築

strapiインストール&プロジェクト作成

本家ドキュメントに従い、構築します。

yarn create strapi-app my-strapi-cms --quickstart

しばらく待っているだけで何もせず、ユーザー登録画面起動:smiley:
01.png

Adminのユーザー情報を入力して[Ready to strapi]でスタートします。

--quickstartを指定した場合、DBはSQLiteが選択され、
一緒にインストールされます。その他のDBは以下が選択可能です。

  • SQLite
  • MongoDB
  • MySQL and MariaDB
  • PostgreSQL

DBを指定する場合の手順はこちら
予めDBのインストールが必要です。

ユーザー登録

ユーザを登録します。
03.png

  • CONTENT TYPESの下にあるユーザーをクリック
  • Userを追加をクリック
  • Username,Email,Password を入力
  • ConfirmedONを選択
  • RoleAuthenticatedを選択
  • 保存をクリック

コンテンツタイプの作成

「Restaurant」というコンテンツを登録します。
04.png

  • CONTENT TYPESの下にあるコンテンツタイプ作成をクリック
  • Create new content-typeをクリック
  • Display Namerestaurantと入力し続けるをクリック
05.png 06.png 07.png
  • Textを選択
  • 基本設定タブでNamenameを入力
  • 高度な設定タブでRequired fieldをチェック
  • 高度な設定タブでUnique fieldをチェック
  • +Add another fieldをクリック

上記を切り返し、複数フィールドを設定します。
最後にFinishをクリックします。
08.png

保存をクリックでコンテンツタイプの作成完了です。
08.png

リレーションの作成

コンテンツタイプにリレーションを組むことができます。
以下の内容の「Category」というコンテンツを登録します。

  • Display Namecategory
  • TextName:name
  • Required fieldをチェック
  • Unique fieldをチェック

リレーションを張るためのfieldを追加します。
09.png

  • Relationを選択
  • Restaurantを選択
  • 多対多を選択

コンポーネントの作成

フィールドに指定できるコンポーネントを作成できます。
10.png

  • Create new content-typeをクリック
  • Namehours_of_operation
  • categoryhours
  • 続けるをクリック
11.png
  • TextName:day_interval
  • Required fieldをチェック

上記の手順で複数項目を設定し、保存します。

Hours_of_operationRestaurantに割り当てるため、
Restaurant+ Add another fieldで以下を追加します。
12.png
13.png
14.png

  • Use an existing componentを選択
  • Namerestaurant_hoursを入力
  • Select a componenthours_of_operationを選択
  • Repeatable componentを選択
  • Finishをクリック
15.png
  • 保存をクリック

Configure the viewからコンテンツ登録時の画面レイアウトの編集ができます。
16.png

コンテンツ登録の準備が整いました!

コンテンツ登録

CONTENT TYPESの下にあるRestaurantからコンテンツを登録できます。
17.png

コンテンツ公開

デフォルトでは限定公開のため、公開設定が必要です。

  • ロールと権限を選択
  • Publicを選択
  • 「Restaurant」、「category」のfind ,findoneをチェックします。
18.png

APIでアクセス

http://localhost:1337/restaurantsにアクセスするとJSONが取得できます。
19.png

以上でHeadlessCMSの準備完了です!

まとめ

簡単に構築できるうえ、リレーション、コンポーネントも組み合わせることで
単純なデータ構造だけではなく、様々なデータ構造に対応できるため、汎用性が高いと感じました。

フロントエンドと組み合わせてCMSライフにどんどん活用したいと思います。:blush:

参考

strapi
WordPress以外の選択肢!HeadlessCMS「Strapi」を試す
超簡単にAPIが作れる?Headless CMS “strapi” を触ってみる
【strapi】HeadlessCMSのstrapiを触ってみた

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?