-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit b831844
Showing
8 changed files
with
251 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
composer.phar | ||
composer.lock | ||
vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "dishark/metaeloquent", | ||
"description" : "Meta Eloquent is an eloquent booster that allows it to easily manage HTTP Meta tags", | ||
"keywords" : ["laravel", "eloquent", "metatags"], | ||
"homepage" : "http://dishark.github.io/metaeloquent", | ||
"license" : "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Dishark", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"Dishark\\Metaeloquent\\": "src/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
# Meta Eloquent | ||
|
||
Meta Eloquent is an eloquent booster that allows it to easily manage HTTP Meta tags. | ||
|
||
# Installation | ||
|
||
## 1. Dependecy | ||
|
||
Using composer, execute the following command to automatically update your composer.json: | ||
|
||
``` | ||
composer require dishark/metaeloquent | ||
``` | ||
or manually update your composer.json file | ||
|
||
``` | ||
{ | ||
"require": { | ||
"dishark/metaeloquent": "dev-master" | ||
} | ||
} | ||
``` | ||
|
||
## 2. Provider | ||
|
||
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: | ||
|
||
```php | ||
// file START omitted | ||
'providers' => [ | ||
// other providers omitted | ||
'Dishark\Metaeloquent\MetaEloquentServiceProvider', | ||
], | ||
// file END omitted | ||
``` | ||
|
||
#Usage | ||
|
||
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. | ||
|
||
Example 1: | ||
|
||
````php | ||
<?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); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php namespace Dishark\Metaeloquent; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
use Dishark\Metaeloquent\Providers\W3cProvider; | ||
use Dishark\Metaeloquent\Providers\OpenGraphProvider; | ||
|
||
class MetaEloquentServiceProvider extends ServiceProvider { | ||
public function register() | ||
{ | ||
$this->app->bind('dishark.metaeloquent.w3c', W3cProvider::class); | ||
$this->app->bind('dishark.metaeloquent.og', OpenGraphProvider::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php namespace Dishark\Metaeloquent\Providers; | ||
|
||
interface MetaProviderInterface { | ||
public function provide($attributes); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php namespace Dishark\Metaeloquent\Providers; | ||
|
||
class OpenGraphProvider implements MetaProviderInterface { | ||
public function provide($attributes) | ||
{ | ||
$output = ''; | ||
|
||
$ogKeys = array_filter(array_flip($attributes), function($key){ | ||
return strpos($key, 'og:') === 0; | ||
}); | ||
|
||
$ogKeys = array_flip($ogKeys); | ||
|
||
foreach ($ogKeys as $key => $value) { | ||
$output .= "<meta property=\"{$key}\" content=\"{$value}\" />"; | ||
} | ||
|
||
return $output; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php namespace Dishark\Metaeloquent\Providers; | ||
|
||
class W3cProvider implements MetaProviderInterface { | ||
public function provide($attributes) | ||
{ | ||
$output = ''; | ||
|
||
$output .= (isset($attributes['author'])) ? "<meta name=\"author\" content=\"{$attributes['author']}\">" : ''; | ||
$output .= (isset($attributes['description'])) ? "<meta name=\"description\" content=\"{$attributes['description']}\">" : ''; | ||
$output .= (isset($attributes['keywords'])) ? "<meta name=\"keywords\" content=\"{$attributes['keywords']}\">" : ''; | ||
|
||
return $output; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php namespace Dishark\Metaeloquent\Traits; | ||
|
||
trait MetaTrait { | ||
public function parseMetaAttributes() | ||
{ | ||
if(! is_array($this->metaAttributes)) return; | ||
|
||
foreach ($this->metaAttributes as $key => $attribute) { | ||
if (method_exists($this, 'getMeta' . $attribute)) { | ||
$this->metaAttributes[$key] = $this->{'getMeta' . $attribute}(); | ||
|
||
continue; | ||
} | ||
|
||
$this->metaAttributes[$key] = ($this->$attribute) ? : $attribute; | ||
} | ||
|
||
return $this->metaAttributes; | ||
} | ||
|
||
public function meta() | ||
{ | ||
$this->metaProviders = $this->metaProviders?:['w3c', 'og']; | ||
$metaAttributes = $this->parseMetaAttributes(); | ||
$metaTags = ''; | ||
|
||
foreach ($this->metaProviders as $provider) { | ||
if (app()->bound($provider)) { | ||
$metaTags .= app($provider)->provide($metaAttributes); | ||
|
||
continue; | ||
} | ||
|
||
if (app()->bound('dishark.metaeloquent.' . $provider)) { | ||
$metaTags .= app('dishark.metaeloquent.' . $provider)->provide($metaAttributes); | ||
} | ||
} | ||
|
||
return $metaTags; | ||
} | ||
} |