orchid/press

Orchid 平台的 Press 包

1.4.0 2020-06-19 12:46 UTC

This package is auto-updated.

Last update: 2024-09-09 23:37:30 UTC


README

此包正在寻找维护者! 如果你想成为维护者,请发送邮件至 bliz48rus@gmail.com

内容管理系统实体

实体是 Press 内容管理系统的主要部分。您不需要为每个模型生成 CRUD,只需选择单独类型中的任何对象,即可轻松管理。本质仅适用于基于 'Post' 的模型,因为它是典型数据的基础。

请描述您希望接收的字段及其形式,然后它将自动构建其 CRUD。

Entities

您可以使用以下命令创建实体

// Create entity for one record
php artisan orchid:entity-single   

// Create entity for many records
php artisan orchid:entity-many

要向用户显示实体,您必须使用图形界面赋予它或组(角色)必要的权利。

类型看起来像这样

namespace DummyNamespace;

use Orchid\Press\Entities\Many;

class DummyClass extends Many
{

    /**
     * @var string
     */
    public $name = '';

    /**
     * @var string
     */
    public $slug = '';

    /**
     * @var string
     */
    public $icon = '';

    /**
     * Slug url /news/{name}.
     * @var string
     */
    public $slugFields = '';

    /**
     * Rules Validation.
     * @return array
     */
    public function rules()
    {
        return [];
    }

    /**
     * @return array
     */
    public function fields()
    {
        return [];
    }

    /**
     * Grid View for post type.
     */
    public function grid()
    {
        return [];
    }

    /**
     * @return array
     */
    public function options()
    {
        return [];
    }
}

您可以使用所有可用方法扩展数据类型,以添加适合您应用程序的新功能。

网格修改

您可以通过传递一个包含名称和函数的数组来更改要显示在网格中的数据,而不是传递键值,其中传递的参数是原始数据切片。

/**
 * Grid View for post type.
 */
public function grid()
{
    return [
        TD::set('name','Name'),
        TD::set('publish_at','Date of publication'),
        TD::set('created_at','Date of creation'),
        TD::name('full_name','Full name')->render(function($post){
            return  "{$post->getContent('fist_name')} {$post->getContent('last_name')}";
        })
    ];
}

文章

Press 假设默认情况下,包含站点数据的任何元素都是 Post 模型。这种结构适合大多数公共网站,因为它们的结构非常相似。例如:

  • 新闻,
  • 促销,
  • 页面,
  • 职位空缺

您可以想到数百种变体。为了不使用几乎相同的模型并使用迁移,我们使用基本的 Post 模型。它使用 JSON 类型作为列,比 EAV 格式更方便、更简单。它还允许翻译。我们已经专门重现了一些针对 Laravel 的 WordPress 方法,这将使您更加高效。

获取数据

现在您可以获取数据库数据

use Orchid\Press\Models\Post;

$posts = Post::all();
// All posted posts
$posts = Post::published()->get();
$posts = Post::status('publish')->get();

// Specific entry
$post = Post::find(42);

// Post name based on the current localization
echo $post->getContent('name');

能够将本地化存储在 JSON 中并不意味着您必须填写所有值。例如,有关酒吧的信息记录,它可以使用俄语和英语,但座位数量不会因任何语言而改变。相应地,没有意义复制此类参数,但将它们放在 options 中。

// Specific entry
$post = Post::find(42);

// All options
echo $post->getOptions();

// Get all localization parameters from options
echo $post->getOption('locale');

// If the option does not exist or is not specified
// you can specify the second parameter that will be returned.
echo $post->getOption('countPlace',10);

单表继承

如果您选择为您的自定义文章类型创建一个新类,则可以返回该类中该文章类型的所有实例。

写行为定义基于指定的 type

//All objects in the $videos collection will be Post instances
$videos = Post::type('video')->status('publish')->get();

分类学

您可以获取特定文章的分类学,例如:

$post = Post::find(42);
$taxonomy = $post->taxonomies()->first();
echo $taxonomy->taxonomy;

或者,您可以使用您的分类学搜索记录

$post = Post::taxonomy('category', 'php')->first();

更复杂的形式是可能的,例如,获取包括其子类别在内的 main 类别中的所有条目

$posts  = Post::whereHas('taxonomies.term', function($query){
	$query->whereIn('slug',
	    Category::slug('main')->with('childrenTerm')
	    ->first()->childrenTerm->pluck('term.slug')
	);
})->get()

请记住,此类记录在抽样率上可能效率较低,这里仅作为示例。

类别和分类学

您可以使用以下方法检索特定类别、分类学或特定类别的记录

// All categories
$category = Taxonomy::category()->slug('uncategorized')->posts()->first();


// Only all categories and entries associated with it
$category = Taxonomy::where('taxonomy', 'category')->with('posts')->get();
$category->each(function($category) {
    echo $category->term->getContent('name');
});

// all posts from category
$category = Category::slug('uncategorized')->posts()->first();
$category->posts->each(function($post) {
    echo $post->getContent('name');
});

附件

附件是与记录相关的文件。这些文件可以是不同格式和分辨率的。

$item = Post::find(42);
$image->attachment()->first();
$image->url();

全文搜索

要使用全文搜索,您需要向您的行为类添加一个新方法

/**
 * Get the indexable data array for the model.
 *
 * @param $array
 *
 * @return mixed
 */
public function toSearchableArray($array)
{
    // Customize array...

    return $array;
}

它将接受所有模型数据,并返回用于索引所需的内容。

以标准的 "DemoPost.php" 为例,它有许多参数,但实际上我们只需要两个

  • 文章标题
  • 文章内容

为此,我们必须返回它们

/**
 * Get the indexable data array for the model.
 *
 * @param $array
 *
 * @return mixed
 */
public function toSearchableArray($array)
{
    $array['content']['en']['id'] = $array['id'];

    return $array['content']['en'];
}

我们以英文返回所有数据,并附有邮政编码。

导入时,只需应用该命令即可。

php artisan scout:import Orchid\\Press\\Models\\Post

现在我们可以在我们的项目中使用搜索了。

use Orchid\Press\Models\Post;
$articles = Post::search('как пропатчить kde2 под freebsd')->get();

标签

标签(标签)- 一个可以联合一组文本、图片等内容的单词或短语

可以使用特性将标签连接到所有创建的模型。

use Orchid\Press\TraitsTaggableTrait;

class Product extends Eloquent
{
    use TaggableTrait;
}

Post模型中,标签默认包含在内,因此示例将在其上。在本节中,我们将向您展示如何管理您的标签主题。

use Orchid\Press\Models\Post;

// Get the entity object
$post = Post::find(1);

// Through a string
$post->tag('foo, bar, baz');

// Through an array
$post->tag([ 'foo', 'bar', 'baz']);

通过数组或实体分隔的字符串从对象中移除一个或多个标签。

// Get the entity object
$post = post::find(1);

// Through a string
$post->untag('bar, baz');

// Through an array
$post->untag(['bar', 'baz']);

// Remove all the tags
$post->untag();

此方法与tag()方法非常相似,但它结合了untag()来自动识别要添加或删除的标签。当运行主题更新时,这是一种有益的技术,您不需要处理检查以确定哪些标签应该添加或删除。

// Get the entity object
$post = Post::find(1);

// Through a string
$post->setTags('foo, bar, baz');

// Through an array
$post->setTags(['foo', 'bar', 'baz']);

// Using the `slug` column
$post->setTags(['foo', 'bar', 'baz'], 'slug');

我们有一些方法可以帮助您获取附加到对象的全部标签,并执行相反的操作以获取具有给定标签的所有对象。

// Get the entity object
$post = Post::whereTag('foo, bar')->get();


$post = Post::find(1);
$tags = $post->tags;

$tags = Post::allTags();

评论

对于某些类型的网站,评论是必需的属性。多亏了它们,用户可以表达他们对任何记录的意见,支持或反驳所表达的意见,与其他用户进行对话。

评论附加到帖子条目

use Orchid\Press\Models\Comment;
use Orchid\Press\Models\Post;

$post = Post::find(42);

$comment = Comment::create([
    'post_id'   => $post->id,
    'user_id'   => Auth::id(),
    'parent_id' => 0,
    'content'   => 'Any text',
    'approved'  => 1,
]);

关系

// Retrieve all comments for a specific Post
$comments = Comment::findByPostId(42);


$comment = Comment::find(1);

// Get in touch with Post
$post = $comment->post();

// Get parent comment
$comment = $comment->original();

// Get child comments
$comment = $comment->replies();

// Get the author of the comment
$comment = $comment->author();

检查

$comment = Comment::find(1);


// Check if a comment has been posted
$comment->isApproved();

// Check if a comment is a response to another comment
$comment->isReply();

// Check if the comment has answers
$comment->hasReplies();

菜单

该软件包包括一个易于使用的机制,用于通过拖放和本地化支持创建自定义菜单(导航)。

配置

大多数菜单都显示在网站顶部,但不同应用程序的位置可能不同,菜单的数量有限,并在配置文件config/press.php中定义。

'menu' => [
    'header'  => 'Top Menu',
    'sidebar' => 'Sidebar Menu',
    'footer'  => 'Footer Menu',
],

模型

菜单类是一个常规的Eloquent模型,所有其功能都可用,例如,要显示只有带有子链接的父菜单项并考虑本地化,则需要

namespace Orchid\Press\Models\Menu;

$menu = Menu::where('lang', app()->getLocale()())
    ->where('parent',0)
    ->where('type', 'footer')
    ->with('children')
    ->get();

可用方法

//First child
$menu = Menu::find(1)->children()->first();


//Parent element
$menu = Menu::find(1)->parent()->get();

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件