Skip to content

Commit b831844

Browse files
committed
First commit
0 parents  commit b831844

File tree

8 files changed

+251
-0
lines changed

8 files changed

+251
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
composer.phar
2+
composer.lock
3+
vendor

composer.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "dishark/metaeloquent",
3+
"description" : "Meta Eloquent is an eloquent booster that allows it to easily manage HTTP Meta tags",
4+
"keywords" : ["laravel", "eloquent", "metatags"],
5+
"homepage" : "http://dishark.github.io/metaeloquent",
6+
"license" : "MIT",
7+
"authors": [
8+
{
9+
"name": "Dishark",
10+
"email": "hudson.byte@gmail.com"
11+
}
12+
],
13+
"autoload": {
14+
"psr-4": {
15+
"Dishark\\Metaeloquent\\": "src/"
16+
}
17+
}
18+
}

readme.MD

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# Meta Eloquent
2+
3+
Meta Eloquent is an eloquent booster that allows it to easily manage HTTP Meta tags.
4+
5+
# Installation
6+
7+
## 1. Dependecy
8+
9+
Using composer, execute the following command to automatically update your composer.json:
10+
11+
```
12+
composer require dishark/metaeloquent
13+
```
14+
or manually update your composer.json file
15+
16+
```
17+
{
18+
"require": {
19+
"dishark/metaeloquent": "dev-master"
20+
}
21+
}
22+
```
23+
24+
## 2. Provider
25+
26+
You need to update your application configuration in order to register the package, so it can be loaded by Laravel. Just update your config/app.php file adding the following code at the end of your 'providers' section:
27+
28+
```php
29+
// file START omitted
30+
'providers' => [
31+
// other providers omitted
32+
'Dishark\Metaeloquent\MetaEloquentServiceProvider',
33+
],
34+
// file END omitted
35+
```
36+
37+
#Usage
38+
39+
Make sure to use `Dishark\Metaeloquent\Traits\MetaTrait` in your model. Then declare the `$metaAttributes` property specifying the metatag as the key and the column as the value.
40+
41+
Example 1:
42+
43+
````php
44+
<?php App\Post;
45+
46+
use Dishark\Metaeloquent\Traits\MetaTrait;
47+
48+
class Post {
49+
use MetaTrait;
50+
51+
protected $metaAttributes = [
52+
'author' => 'author',
53+
'description' => 'title',
54+
'keywords' => 'keywords',
55+
];
56+
}
57+
```
58+
59+
Sometimes the column isn't enough. Let's create some Meta accessor:
60+
61+
Example 2:
62+
63+
```php
64+
<?php App\Post;
65+
66+
use Dishark\Metaeloquent\Traits\MetaTrait;
67+
68+
class Post {
69+
use MetaTrait;
70+
71+
protected $metaAttributes = [
72+
'author' => 'author',
73+
'description' => 'title',
74+
'keywords' => 'keywords',
75+
];
76+
77+
public function getMetaAuthor()
78+
{
79+
return $this->author->name;
80+
}
81+
}
82+
```
83+
84+
## View
85+
86+
In your view:
87+
88+
```php
89+
@extends ('layout')
90+
91+
@section ('metadata')
92+
{!! $post->meta() !!}
93+
@endsection
94+
```
95+
96+
The layout:
97+
98+
```php
99+
<head>
100+
@yield('metadata')
101+
</head>
102+
```
103+
104+
So now the author attribute will be called through this method instead.
105+
106+
## OpenGraph
107+
108+
```php
109+
<?php App\Post;
110+
111+
use Dishark\Metaeloquent\Traits\MetaTrait;
112+
113+
class Post {
114+
use MetaTrait;
115+
116+
protected $metaAttributes = [
117+
'author' => 'author',
118+
'description' => 'title',
119+
'keywords' => 'keywords',
120+
121+
'og:title' => 'name',
122+
'og:image' => 'image',
123+
'og:type' => 'article',
124+
'og:url' => 'url'
125+
];
126+
127+
public function getMetaImage()
128+
{
129+
return asset('images_path/' . $this->image);
130+
}
131+
132+
public function getMetaUrl()
133+
{
134+
return route('articles', $this->slug);
135+
}
136+
}
137+
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php namespace Dishark\Metaeloquent;
2+
3+
use Illuminate\Support\ServiceProvider;
4+
use Dishark\Metaeloquent\Providers\W3cProvider;
5+
use Dishark\Metaeloquent\Providers\OpenGraphProvider;
6+
7+
class MetaEloquentServiceProvider extends ServiceProvider {
8+
public function register()
9+
{
10+
$this->app->bind('dishark.metaeloquent.w3c', W3cProvider::class);
11+
$this->app->bind('dishark.metaeloquent.og', OpenGraphProvider::class);
12+
}
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php namespace Dishark\Metaeloquent\Providers;
2+
3+
interface MetaProviderInterface {
4+
public function provide($attributes);
5+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php namespace Dishark\Metaeloquent\Providers;
2+
3+
class OpenGraphProvider implements MetaProviderInterface {
4+
public function provide($attributes)
5+
{
6+
$output = '';
7+
8+
$ogKeys = array_filter(array_flip($attributes), function($key){
9+
return strpos($key, 'og:') === 0;
10+
});
11+
12+
$ogKeys = array_flip($ogKeys);
13+
14+
foreach ($ogKeys as $key => $value) {
15+
$output .= "<meta property=\"{$key}\" content=\"{$value}\" />";
16+
}
17+
18+
return $output;
19+
}
20+
}

src/Providers/W3cProvider.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php namespace Dishark\Metaeloquent\Providers;
2+
3+
class W3cProvider implements MetaProviderInterface {
4+
public function provide($attributes)
5+
{
6+
$output = '';
7+
8+
$output .= (isset($attributes['author'])) ? "<meta name=\"author\" content=\"{$attributes['author']}\">" : '';
9+
$output .= (isset($attributes['description'])) ? "<meta name=\"description\" content=\"{$attributes['description']}\">" : '';
10+
$output .= (isset($attributes['keywords'])) ? "<meta name=\"keywords\" content=\"{$attributes['keywords']}\">" : '';
11+
12+
return $output;
13+
}
14+
}

src/Traits/MetaTrait.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php namespace Dishark\Metaeloquent\Traits;
2+
3+
trait MetaTrait {
4+
public function parseMetaAttributes()
5+
{
6+
if(! is_array($this->metaAttributes)) return;
7+
8+
foreach ($this->metaAttributes as $key => $attribute) {
9+
if (method_exists($this, 'getMeta' . $attribute)) {
10+
$this->metaAttributes[$key] = $this->{'getMeta' . $attribute}();
11+
12+
continue;
13+
}
14+
15+
$this->metaAttributes[$key] = ($this->$attribute) ? : $attribute;
16+
}
17+
18+
return $this->metaAttributes;
19+
}
20+
21+
public function meta()
22+
{
23+
$this->metaProviders = $this->metaProviders?:['w3c', 'og'];
24+
$metaAttributes = $this->parseMetaAttributes();
25+
$metaTags = '';
26+
27+
foreach ($this->metaProviders as $provider) {
28+
if (app()->bound($provider)) {
29+
$metaTags .= app($provider)->provide($metaAttributes);
30+
31+
continue;
32+
}
33+
34+
if (app()->bound('dishark.metaeloquent.' . $provider)) {
35+
$metaTags .= app('dishark.metaeloquent.' . $provider)->provide($metaAttributes);
36+
}
37+
}
38+
39+
return $metaTags;
40+
}
41+
}

0 commit comments

Comments
 (0)