如何在WordPress中使用Laravel

下面由WordPress教程欄目給大家介紹如何在 wordpress 中使用 laravel,希望對需要的朋友有所幫助!

Corcel 讓你在 WordPress 中使用 laravel

你想過可以在 WordPress 中使用 Laravel 或者任意一種 PHP 框架嗎??Corcel?可以幫你實現!

開發網站應用就應該是快捷并有趣的。當然了,每個應用都會有它自己的需求和生命周期。

WordPress 是基于 PHP 編寫的功能強大的 CMS,你可以使用它非常快的創建你的產品。然而,它并沒有遵循最近 PHP 的變化和約定,但是你可以將它與其他類似 Laravel 這樣的框架一起使用來平衡這一點。

Corcel

我認為 WordPress 的后臺管理面板很棒。它有一堆插件,可以讓你快速地生成字段,文章類型,圖片,作物等等。這真的很棒!

這就是為什么有了?Corcel?,它可以輕松讓你從 WordPress 數據庫中獲取數據。你只需要使用 Composer 在你的 PHP 應用程序框架( Laravel 或其他框架)中安裝 WordPress 和 Corcel 就可以了。

當然我們也可以在 WordPress 中使用 MVC !

你可以為你的 WordPress 搭建控制器、模型和視圖。Corcel 為你創建了一個模型集合來檢索文章、頁面和菜單等,甚至還可以連接不同的數據庫,一個用于 Laravel ,另一個用于 WordPress 。

<?php // File: /config/database.php 'connections' => [     'mysql' => [         'driver'    => 'mysql',         'host'      => 'localhost',         'database'  => 'app',         'username'  => 'admin'         'password'  => 'secret',         'charset'   => 'utf8',         'collation' => 'utf8_unicode_ci',         'prefix'    => '',         'strict'    => false,         'engine'    => null,     ],     'wordpress' => [         'driver'    => 'mysql',         'host'      => 'localhost',         'database'  => 'corcel',         'username'  => 'admin',         'password'  => 'secret',         'charset'   => 'utf8',         'collation' => 'utf8_unicode_ci',         'prefix'    => 'wp_',         'strict'    => false,         'engine'    => null,     ],      ],

下面開始從 WordPress 數據庫中獲取你所需要的東西:

<?php // File: /app/Http/Controllers/AnyController.php // ... public function index() {     $posts = Post::published()->take(10)->get();     $page = Page::where('post_name', 'about')->first();     return view('posts.index', compact('posts', 'page')); } // ...

文章類型與自定義字段

不知道你是否使用過高級自定義字段功能( ACF )?在這里你也可以得到所有的自定義字段:

<?php $post = Post::find(1); $avatar = $post->meta->avatar; $phone = $post->meta->phone;

你可以創建與自定義文章類型相關的自定義模型:

<?php  use CorcelPost as Corcel; class Service extends Corcel {     protected $postType = 'service'; }

關于更多的功能,你可以直接在 GitHub(https://github.com/corcel/corcel) 倉庫中查看。
你可以在任何 PHP 框架中使用 Corcel,甚至是像 Slim,Silex 這樣的微型框架。它可以讓你獲得所有 WordPress 管理面板數據,并可以讓你使用自定義路由、控制器、模型和視圖來組織你的項目。

來給 Corcel 一個機會吧,也歡迎大家給一些建議或者直接貢獻代碼,謝謝!

? 版權聲明
THE END
喜歡就支持一下吧
點贊14 分享