Skip to content

Commit 875defa

Browse files
author
mostafa
committed
laravel head
Co-Authored-By: Mostafa Kamal <[email protected]>
1 parent 782f618 commit 875defa

File tree

5 files changed

+250
-2
lines changed

5 files changed

+250
-2
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1-
# lara-head
2-
Laravel meta tag || Laravel seo with lara-head
1+
2+
3+
4+
# Head tags
5+
6+
* [gist link](https://gist.github.com/lancejpollard/1978404)
7+
* [gethead](https://gethead.info/)

composer.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "code4mk/lara-head",
3+
"description": "Easily setup seo meta tag with lara-head",
4+
"keywords": [
5+
"seo",
6+
"meta-tag",
7+
"meta",
8+
"head",
9+
"khead",
10+
"code4mk",
11+
"0devco"
12+
],
13+
"homepage": "https://github.com/code4mk/lara-head",
14+
"support": {
15+
"issues": "https://github.com/code4mk/lara-head/issues",
16+
"source": "https://github.com/code4mk/lara-head"
17+
},
18+
"license": "MIT",
19+
"authors": [
20+
{
21+
"name": "code4mk",
22+
"email": "[email protected]",
23+
"website": "https://code4mk.org"
24+
}
25+
],
26+
"autoload": {
27+
"psr-4": {
28+
"Code4mk\\LaraHead": "code4mk/lara-head/src/"
29+
}
30+
},
31+
"extra": {
32+
"laravel": {
33+
"providers": [
34+
"Code4mk\\LaraHead\\LaraHeadServiceProvider"
35+
]
36+
}
37+
},
38+
"prefer-stable": true,
39+
"minimum-stability": "dev"
40+
}

src/Facades/Khead.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Code4mk\LaraHead\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
/**
8+
* @author @code4mk <[email protected]>
9+
* @author @0devco <[email protected]>
10+
* @since 2019
11+
* @copyright 0dev.co (https://0dev.co)
12+
*/
13+
14+
class Khead extends Facade
15+
{
16+
/**
17+
* Get the registered name of the component.
18+
*
19+
* @return string
20+
*/
21+
protected static function getFacadeAccessor()
22+
{
23+
return 'khead';
24+
}
25+
}

src/LaraHeadServiceProvider.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Code4mk\LaraHead;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
use Illuminate\Foundation\AliasLoader;
7+
use Code4mk\LaraHead\Og;
8+
9+
/**
10+
* @author @code4mk <[email protected]>
11+
* @author @0devco <[email protected]>
12+
* @since 2019
13+
* @copyright 0dev.co (https://0dev.co)
14+
*/
15+
16+
class LaraHeadServiceProvider extends ServiceProvider
17+
{
18+
/**
19+
* Bootstrap any application services.
20+
*
21+
* @return void
22+
*/
23+
public function boot()
24+
{
25+
26+
27+
// publish config
28+
29+
AliasLoader::getInstance()->alias('Khead', 'Code4mk\LaraHead\Facades\Khead');
30+
}
31+
32+
/**
33+
* Register any application services.
34+
*
35+
* @return void
36+
*/
37+
public function register()
38+
{
39+
$this->app->bind('khead', function () {
40+
return new Og;
41+
});
42+
}
43+
}

src/Og.php

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
3+
namespace Code4mk\LaraHead;
4+
5+
use Illuminate\Support\HtmlString;
6+
use Illuminate\Support\Arr;
7+
8+
class Og
9+
{
10+
public $metas = [];
11+
public $ogs = [];
12+
public $cards = [];
13+
public $title;
14+
15+
public function setTitle($data)
16+
{
17+
$this->title = $this->toHtmlString('<title>' . $data . '</title>');
18+
}
19+
20+
public function getTitle()
21+
{
22+
return $this->title;
23+
}
24+
25+
26+
27+
public function setMeta($name,$data = [])
28+
{
29+
$this->metas[$name] = $this->toHtmlString('<meta' . $this->attributes($data) . '>');
30+
}
31+
32+
public function getMeta($key)
33+
{
34+
return Arr::get($this->metas, $key, '');
35+
}
36+
37+
public function setLink($name,$data = [])
38+
{
39+
$this->metas[$name] = $this->toHtmlString('<link' . $this->attributes($data) . '>');
40+
}
41+
42+
public function getLink($key)
43+
{
44+
return Arr::get($this->metas, $key, '');
45+
}
46+
47+
public function setOg($data = [])
48+
{
49+
foreach ($data as $key => $value) {
50+
$this->ogs[] = $this->toHtmlString('<meta' . $this->attributes($value) . '>');
51+
}
52+
}
53+
54+
public function getOg()
55+
{
56+
//$m = implode("\n",$this->ogs)."</pre>";
57+
$m = "\t\t";
58+
59+
return implode("\n{$m}",$this->ogs);
60+
}
61+
62+
public function setTwitCards($data = [])
63+
{
64+
foreach ($data as $key => $value) {
65+
$this->cards[] = $this->toHtmlString('<meta' . $this->attributes($value) . '>');
66+
}
67+
}
68+
69+
public function getTwitCards()
70+
{
71+
//$m = implode("\n",$this->ogs)."</pre>";
72+
$m = "\t\t";
73+
74+
return implode("\n{$m}",$this->cards);
75+
}
76+
77+
78+
79+
/**
80+
* Build an HTML attribute string from an array.
81+
*
82+
* @param array $attributes
83+
*
84+
* @return string
85+
*/
86+
public function attributes($attributes)
87+
{
88+
$html = [];
89+
foreach ((array) $attributes as $key => $value) {
90+
$element = $this->attributeElement($key, $value);
91+
if (! is_null($element)) {
92+
$html[] = $element;
93+
}
94+
}
95+
return count($html) > 0 ? ' ' . implode(' ', $html) : '';
96+
}
97+
98+
/**
99+
* Build a single attribute element.
100+
*
101+
* @param string $key
102+
* @param string $value
103+
*
104+
* @return string
105+
*/
106+
protected function attributeElement($key, $value)
107+
{
108+
// For numeric keys we will assume that the value is a boolean attribute
109+
// where the presence of the attribute represents a true value and the
110+
// absence represents a false value.
111+
// This will convert HTML attributes such as "required" to a correct
112+
// form instead of using incorrect numerics.
113+
114+
115+
// Treat boolean attributes as HTML properties
116+
117+
118+
if (! is_null($value)) {
119+
return $key . '="' . e($value, false) . '"';
120+
}
121+
}
122+
123+
/**
124+
* Transform the string to an Html serializable object
125+
*
126+
* @param $html
127+
*
128+
* @return \Illuminate\Support\HtmlString
129+
*/
130+
protected function toHtmlString($html)
131+
{
132+
return new HtmlString($html);
133+
}
134+
135+
}

0 commit comments

Comments
 (0)