在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):corcel/corcel开源软件地址(OpenSource Url):https://github.com/corcel/corcel开源编程语言(OpenSource Language):PHP 100.0%开源软件介绍(OpenSource Introduction):A collection of Model classes that allows you to get data directly from a WordPress database. Corcel is a collection of PHP classes built on top of Eloquent ORM (from Laravel framework), that provides a fluent interface to connect and get data directly from a WordPress database. You can use WordPress as the backend (administration panel) or CMS, for inserting posts, custom types, etc, and any other PHP app in the other side querying those data (as a Model layer). It's easier to use Corcel with Laravel, but you're free to use it with any PHP project that uses Composer. Buy me a Coffee | Follow Corcel on Twitter Table of ContentsInstalling CorcelVersion Compatibility
Installing CorcelYou need to use Composer to install Corcel into your project:
Configuring (Laravel)Laravel 5.5 and newerCorcel wil register itself using Laravel's Auto Discovery. Laravel 5.4 and olderYou'll have to include 'providers' => [
/*
* Package Service Providers...
*/
Corcel\Laravel\CorcelServiceProvider::class,
] Publishing the configuration fileNow configure our config file to make sure your database is set correctly and to allow you to register custom post types and shortcodes in a very easy way: Run the following Artisan command in your terminal:
Now you have a Database SetupLaravel SetupJust set the database Let' suppose you have those following database connections in your // File: /config/database.php
'connections' => [
'mysql' => [ // for Laravel database
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'mydatabase',
'username' => 'admin'
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'wordpress' => [ // for WordPress database (used by Corcel)
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'mydatabase',
'username' => 'admin',
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => 'wp_',
'strict' => false,
'engine' => null,
],
], In this case you should want to use the 'connection' => 'wordpress', Other PHP Framework (not Laravel) SetupHere you have to configure the database to fit the Corcel requirements. First, you should include the Composer require __DIR__ . '/vendor/autoload.php'; Now you must set your WordPress database params: $params = [
'database' => 'database_name',
'username' => 'username',
'password' => 'pa$$word',
'prefix' => 'wp_' // default prefix is 'wp_', you can change to your own prefix
];
Corcel\Database::connect($params); You can specify all Eloquent params, but some are default (but you can override them). 'driver' => 'mysql',
'host' => 'localhost',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => 'wp_', // Specify the prefix for WordPress tables, default prefix is 'wp_' UsagePosts
// All published posts
$posts = Post::published()->get();
$posts = Post::status('publish')->get();
// A specific post
$post = Post::find(31);
echo $post->post_title; Creating your own model classesOptionally you can create your own
<?php // File: app/Post.php
namespace App;
use Corcel\Model\Post as Corcel;
class Post extends Corcel
{
protected $connection = 'foo-bar';
public function customMethod() {
//
}
} So, now you can fetch WP database data using your own class: $posts = App\Post::all(); // using the 'foo-bar' connection
Meta Data (Custom Fields)
You can retrieve meta data from posts too. // Get a custom meta value (like 'link' or whatever) from a post (any type)
$post = Post::find(31);
echo $post->meta->link; // OR
echo $post->fields->link;
echo $post->link; // OR To create or update meta data form a User just use the $post = Post::find(1);
$post->saveMeta('username', 'jgrossi'); You can save many meta data at the same time too: $post = Post::find(1);
$post->saveMeta([
'username' => 'jgrossi',
'url' => 'http://jgrossi.com',
]); You also have the $post = Post::find(1);
$postMeta = $post->createMeta('foo', 'bar'); // instance of PostMeta class
$trueOrFalse = $post->saveMeta('foo', 'baz'); // boolean Querying Posts by Custom Fields (Meta)There are multiples possibilities to query posts by their custom fields (meta) by using scopes on a To check if a meta key exists, use the
If you want to precisely match a meta-field, you can use the // Find a published post which matches both meta_key and meta_value.
$post = Post::published()->hasMeta('username', 'jgrossi')->first(); If you need to match multiple meta-fields, you can also use the $post = Post::hasMeta(['username' => 'jgrossi'])->first();
$post = Post::hasMeta(['username' => 'jgrossi', 'url' => 'jgrossi.com'])->first();
// Or just passing the keys
$post = Post::hasMeta(['username', 'url'])->first(); If you need to match a case-insensitive string, or match with wildcards, you can use the // Will match: 'J Grossi', 'J GROSSI', and 'j grossi'.
$post = Post::published()->hasMetaLike('author', 'J GROSSI')->first();
// Using % as a wildcard will match: 'J Grossi', 'J GROSSI', 'j grossi', 'Junior Grossi' etc.
$post = Post::published()->hasMetaLike('author', 'J%GROSSI')->first(); Fields AliasesThe $post = Post::find(1);
$post->title === $post->post_title; // true If you're extending the class A extends \Corcel\Post
{
protected static $aliases = [
'foo' => 'post_foo',
];
}
$a = A::find(1);
echo $a->foo;
echo $a->title; // from Post class Custom ScopesTo order posts you can use $newest = Post::newest()->first();
$oldest = Post::oldest()->first(); PaginationTo order posts just use Eloquent $posts = Post::published()->paginate(5);
foreach ($posts as $post) {
// ...
} To display the pagination links just call the {{ $posts->links() }} Advanced Custom Fields (ACF)If you want to retrieve a custom field created by the Advanced Custom Fields (ACF) plugin, you have to install the $post = Post::find(123);
echo $post->acf->some_radio_field;
$repeaterFields = $post->acf->my_repeater_name; To avoid unnecessary SQL queries just set the field type you're requesting. Usually two SQL queries are necessary to get the field type, so if you want to specify it you're skipping those extra queries: $post = Post::find(123);
echo $post->acf->text('text_field_name');
echo $post->acf->boolean('boolean_field_name'); Custom Post TypeYou can work with custom post types too. You can use the // using type() method
$videos = Post::type('video')->status('publish')->get();
// using your own class
class Video extends Corcel\Post
{
protected $postType = 'video';
}
$videos = Video::status('publish')->get(); Using Custom post types and meta data: // Get 3 posts with custom post type (store) and show its address
$stores = Post::type('store')->status('publish')->take(3)->get();
foreach ($stores as $store) {
$storeAddress = $store->address; // option 1
$storeAddress = $store->meta->address; // option 2
$storeAddress = $store->fields->address; // option 3
} Configuring the returning InstanceEvery time you call something like If you choose to create a new class for your custom post type, you can have this class be returned for all instances of that post type. Registering Post Types (the easy way)Instead of call 'post_types' => [
'video' => App\Video::class,
'foo' => App\Foo::class,
] So every time you query a custom post type the mapped instance will be returned.
Registering Post Types (the hard way)//all objects in the $videos Collection will be instances of Post
$videos = Post::type('video')->status('publish')->get();
// register the video custom post type and its particular class
Post::registerPostType('video', '\App\Video')
//now all objects in the $videos Collection will be instances of Video
$videos = Post::type('video')->status('publish')->get(); You can also do this for inbuilt classes, such as Page or Post. Simply register the Page or Post class with the associated post type string, and that object will be returned instead of the default one. ShortcodesFrom config (Laravel)You can map all shortcodes you want inside the 'shortcodes' => [
'foo' => App\Shortcodes\FooShortcode::class,
'bar' => App\Shortcodes\BarShortcode::class,
], This is a sample shortcode class: class FakeShortcode implements \Corcel\Shortcode
{
/**
* @param ShortcodeInterface $shortcode
* @return string
*/
public function render(ShortcodeInterface $shortcode)
{
return sprintf(
'html-for-shortcode-%s-%s',
$shortcode->getName(),
$shortcode->getParameter('one')
);
}
} In runtimeYou can add shortcodes by calling the // [gallery id="1"]
Post::addShortcode('gallery', function ($shortcode) {
return $shortcode->getName() . '.' . $shortcode->getParameter('id');
});
$post = Post::find(1);
echo $post->content;
If you are using Laravel, we suggest adding your shortcodes handlers in Shortcode ParsingShortcodes are parsed with the thunderer/shortcode library. Several different parsers are provided. 'shortcode_parser' => Thunder\Shortcode\Parser\RegularParser::class,
// 'shortcode_parser' => Thunder\Shortcode\Parser\WordpressParser::class, If you are not using Laravel, you can to do this in runtime, calling the |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论