4
2

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 3 years have passed since last update.

【Laravel】CKEditorを使ってWYSIWYGエディタで投稿する!

Last updated at Posted at 2021-07-02

はじめに

よし、ブログ投稿画面を作るぞ!
まずは form をつくって~...
image.png
よし出来た!
...けど文字装飾とか確認できないし、何より味気ないなぁ...

→CKEditor導入後
image.png
これこれ!これをやりたかったんだ!

ちなみにWYSIWYGとは"What You See Is What You Get"の略で、
編集中のデータがそのまま画面に表示されるよといった意味合いです。

開発環境

Laravel 8.45.1
PHP 7.4.7

導入

なんの変哲も捻りもないフォームに対して、
①input textarea にクラスを付与
②ckeditorのスクリプト読み込み
この2件で再現可能です!

edit.blade.php
<form method="POST" action="{{ route('posts.store') }}">
    @csrf
    <div >
        <label for="title_post">Title</label>
        <div>
            <input type="text-input" name="title">
        </div>
    </div>
    
    <div style="width:50%">
        <label for="text-input">Text</label>
        <div>
            // class="ckeditor"を付与
            <textarea class="ckeditor" name="text"></textarea>
        </div>
    </div>

    <div>
        <button type="submit">Post</button>
    </div>
</form>

// 外部スクリプトの読み込み
<script src="//cdn.ckeditor.com/4.14.1/standard/ckeditor.js"></script>

以上です、簡単!
パッケージをダウンロードしたい場合は公式からダウンロード可能です。
https://ckeditor.com/ckeditor-4/download/

使い方

image.png
このように直感的に作成が可能です。
image.png
POSTデータはHTML構造で格納されます。
公式ページでデモ操作が可能ですので、お試しあれ!
https://ckeditor.com/ckeditor-4/demo/

終わりに

今回はCKEditorを利用してWYSIWYGエディタを導入する方法をご紹介いたしました。
エディタ自体の装飾もできるようなので、どんなコンセプトのサイトでも気軽に導入できそうですね!

4
2
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
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?