Laravel 融合 Markdown 做部落格書寫後臺案例

程式碼的壞味道發表於2019-07-06

1.composer安裝指定的包

composer require chenhua/laravel5-markdown-editor

2.在 config/app.php 中 providers 中新增

Chenhua\MarkdownEditor\MarkdownEditorServiceProvider::class,

3.修改 config/app.php 中 aliases 中新增

'MarkdownEditor' => Chenhua\MarkdownEditor\Facades\MarkdownEditor::class,

4.執行 artisan 命令,生成 config/markdowneditor.php 配置檔案

php artisan vendor:publish --tag=markdown

5.修改 config/markdowneditor.php 配置檔案

<?php
return [
    "default"     => 'local', //預設返回儲存位置url.也可以設定七牛上傳路徑,阿里雲端儲存檔案等等
    "dirver"      => ['local'], //儲存平臺 ['local', 'qiniu', 'aliyun']
    "connections" => [
        "local"  => [
            'prefix' => 'uploads/markdown', //本地儲存位置,預設uploads
        ],
        "qiniu"  => [
            'access_key' => '',
            'secret_key' => '',
            'bucket'     => '',
            'prefix'     => '', //檔案字首 file/of/path
            'domain'     => '' //七牛自定義域名
        ],
        "aliyun" => [
            'ak_id'     => '',
            'ak_secret' => '',
            'end_point'  => '',
            'bucket'    => '',
            'prefix'    => '',
        ],
    ],
];

建立view頁面 index.blade.php.然後新增指定程式碼:

 <form method="post" action="{{route('admin.eidt')}}">
        <div id="test-editormd">
            <textarea name="test-editormd" style="display:none;"></textarea>
        </div>
        @include('markdown::encode',['editors'=>['test-editormd']])
        <button type="submit" class="btn btn-primary">提交</button>
    </form>
    <script>\
    var testEditor = editormd("test-editormd",{\
        width :"90%",\
        height :640,\
        path :"../vendor/markdown/lib/",\
        saveHTMLToTextarea :true\
  });\
  testEditor.getHTML();// 獲取 Textarea 儲存的 HTML 原始碼\
</script>

建立控制器開啟指定頁面

  //主頁顯示
    public function index(Request $request)
    {
        return view('index');
    }
    //提交
      public function add(Request $request)
    {
        $content = $request->input('test-editormd-html-code');
        $article=Article::create($content);
  }

Laravel 融合 Markdown 做部落格書寫後臺案例

希望有能力的同學還是把markdown頁面寫到vue裡面,不建議直接寫到laravel專案裡面,我這裡只是一個案例測試.

cfun

相關文章