dishark/metaeloquent

Meta Eloquent 是一个 eloquent 插件,可以轻松管理 HTTP Meta 标签

v1.0 2015-05-26 16:43 UTC

This package is not auto-updated.

Last update: 2024-09-28 18:10:34 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

Meta Eloquent 是一个 eloquent 插件,可以轻松管理 HTTP Meta 标签。

安装

1. 依赖关系

使用 composer,执行以下命令以自动更新您的 composer.json 文件:

composer require dishark/metaeloquent

或者手动更新您的 composer.json 文件

{
    "require": {
        "dishark/metaeloquent": "dev-master"
    }
}

2. 提供者

您需要更新应用程序配置以注册包,以便它可以由 Laravel 加载。只需在 'providers' 部分的末尾添加以下代码即可更新您的 config/app.php 文件

// file START omitted
    'providers' => [
        // other providers omitted
        'Dishark\Metaeloquent\MetaEloquentServiceProvider',
    ],
// file END omitted

使用方法

请确保在您的模型中使用 Dishark\Metaeloquent\Traits\MetaTrait。然后声明 $metaAttributes 属性,指定 metatag 作为键,列作为值。

示例 1

<?php App\Post;

use Dishark\Metaeloquent\Traits\MetaTrait;

class Post {
	use MetaTrait;

	protected $metaAttributes = [
		'author' => 'author',
		'description' => 'title',
		'keywords' => 'keywords',
	];
}
```

Sometimes the column isn't enough. Let's create some Meta accessor:

Example 2:

```php
<?php App\Post;

use Dishark\Metaeloquent\Traits\MetaTrait;

class Post {
	use MetaTrait;

	protected $metaAttributes = [
		'author' => 'author',
		'description' => 'title',
		'keywords' => 'keywords',
	];

	public function getMetaAuthor()
	{
		return $this->author->name;
	}
}
```

## View

In your view:

```php
@extends ('layout')

@section ('metadata')
{!! $post->meta() !!}
@endsection
```

The layout:

```php
<head>
@yield('metadata')
</head>
```

So now the author attribute will be called through this method instead.

## OpenGraph

```php
<?php App\Post;

use Dishark\Metaeloquent\Traits\MetaTrait;

class Post {
	use MetaTrait;

	protected $metaAttributes = [
		'author' => 'author',
		'description' => 'title',
		'keywords' => 'keywords',

		'og:title' => 'name',
		'og:image' => 'image',
		'og:type' => 'article',
		'og:url' => 'url'
	];

	public function getMetaImage()
	{
		return asset('images_path/' . $this->image);
	}

	public function getMetaUrl()
	{
		return route('articles', $this->slug);
	}
}
```