From 5f80594c0de866b3f95bc07c3fff6a9e15a79164 Mon Sep 17 00:00:00 2001 From: Jack Wohlfert Date: Tue, 30 Jan 2018 13:34:52 -0500 Subject: [PATCH] first commit --- .gitignore | 3 + composer.json | 47 ++++ composer.lock | 509 +++++++++++++++++++++++++++++++++++++++ readme.md | 33 +++ src/Seo.php | 102 ++++++++ src/SeoTrait.php | 14 ++ src/ServiceProvider.php | 39 +++ src/config/seo.php | 6 + src/views/meta.blade.php | 89 +++++++ 9 files changed, 842 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 composer.lock create mode 100644 readme.md create mode 100644 src/Seo.php create mode 100644 src/SeoTrait.php create mode 100644 src/ServiceProvider.php create mode 100644 src/config/seo.php create mode 100644 src/views/meta.blade.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..66edbdb --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/vendor +.DS_Store +.idea/ \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..a5ddd34 --- /dev/null +++ b/composer.json @@ -0,0 +1,47 @@ +{ + "name": "jwohlfert23/laravel-seo", + "description": "SEO tools to insert meta data in laravel projects", + "keywords": [ + "laravel", + "seo" + ], + "license": "MIT", + "authors": [ + { + "name": "Jack Wohlfert", + "email": "jack.wohlfert@gmail.com" + } + ], + "repositories": [ + { + "type": "git", + "url": "https://github.com/jwohlfert23/laravel-seo.git" + } + ], + "autoload": { + "psr-4": { + "Jwohlfert23\\LaravelSeo\\": "src/" + } + }, + "require": { + "php": ">=5.5.9", + "nesbot/carbon": "^1.22", + "illuminate/config": "~5", + "illuminate/support": "~5" + }, + "require-dev": { + }, + "extra": { + "laravel": { + "providers": [ + "Jwohlfert23\\LaravelSeo\\ServiceProvider" + ] + } + }, + "config": { + "preferred-install": "dist" + }, + "minimum-stability": "dev", + "prefer-stable": true + +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..8669f0d --- /dev/null +++ b/composer.lock @@ -0,0 +1,509 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" + ], + "content-hash": "d61cf0f9f9e969962e4ac8f51e316d9e", + "packages": [ + { + "name": "doctrine/inflector", + "version": "v1.3.0", + "source": { + "type": "git", + "url": "https://github.com/doctrine/inflector.git", + "reference": "5527a48b7313d15261292c149e55e26eae771b0a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/inflector/zipball/5527a48b7313d15261292c149e55e26eae771b0a", + "reference": "5527a48b7313d15261292c149e55e26eae771b0a", + "shasum": "" + }, + "require": { + "php": "^7.1" + }, + "require-dev": { + "phpunit/phpunit": "^6.2" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.3.x-dev" + } + }, + "autoload": { + "psr-4": { + "Doctrine\\Common\\Inflector\\": "lib/Doctrine/Common/Inflector" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Roman Borschel", + "email": "roman@code-factory.org" + }, + { + "name": "Benjamin Eberlei", + "email": "kontakt@beberlei.de" + }, + { + "name": "Guilherme Blanco", + "email": "guilhermeblanco@gmail.com" + }, + { + "name": "Jonathan Wage", + "email": "jonwage@gmail.com" + }, + { + "name": "Johannes Schmitt", + "email": "schmittjoh@gmail.com" + } + ], + "description": "Common String Manipulations with regard to casing and singular/plural rules.", + "homepage": "http://www.doctrine-project.org", + "keywords": [ + "inflection", + "pluralize", + "singularize", + "string" + ], + "time": "2018-01-09T20:05:19+00:00" + }, + { + "name": "illuminate/config", + "version": "v5.5.33", + "source": { + "type": "git", + "url": "https://github.com/illuminate/config.git", + "reference": "09bb7534fb6c33c2bf5a4157a539c8e2c876a1fa" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/illuminate/config/zipball/09bb7534fb6c33c2bf5a4157a539c8e2c876a1fa", + "reference": "09bb7534fb6c33c2bf5a4157a539c8e2c876a1fa", + "shasum": "" + }, + "require": { + "illuminate/contracts": "5.5.*", + "illuminate/support": "5.5.*", + "php": ">=7.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "5.5-dev" + } + }, + "autoload": { + "psr-4": { + "Illuminate\\Config\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Taylor Otwell", + "email": "taylor@laravel.com" + } + ], + "description": "The Illuminate Config package.", + "homepage": "https://laravel.com", + "time": "2017-06-26T13:15:04+00:00" + }, + { + "name": "illuminate/contracts", + "version": "v5.5.33", + "source": { + "type": "git", + "url": "https://github.com/illuminate/contracts.git", + "reference": "eb9a0171866fca0669c9acab6c7441e19b4694ca" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/illuminate/contracts/zipball/eb9a0171866fca0669c9acab6c7441e19b4694ca", + "reference": "eb9a0171866fca0669c9acab6c7441e19b4694ca", + "shasum": "" + }, + "require": { + "php": ">=7.0", + "psr/container": "~1.0", + "psr/simple-cache": "~1.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "5.5-dev" + } + }, + "autoload": { + "psr-4": { + "Illuminate\\Contracts\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Taylor Otwell", + "email": "taylor@laravel.com" + } + ], + "description": "The Illuminate Contracts package.", + "homepage": "https://laravel.com", + "time": "2018-01-19T17:59:58+00:00" + }, + { + "name": "illuminate/support", + "version": "v5.5.33", + "source": { + "type": "git", + "url": "https://github.com/illuminate/support.git", + "reference": "f6eb4fc687a1956cb44007604cd31dc50858eca9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/illuminate/support/zipball/f6eb4fc687a1956cb44007604cd31dc50858eca9", + "reference": "f6eb4fc687a1956cb44007604cd31dc50858eca9", + "shasum": "" + }, + "require": { + "doctrine/inflector": "~1.1", + "ext-mbstring": "*", + "illuminate/contracts": "5.5.*", + "nesbot/carbon": "^1.20", + "php": ">=7.0" + }, + "replace": { + "tightenco/collect": "self.version" + }, + "suggest": { + "illuminate/filesystem": "Required to use the composer class (5.2.*).", + "symfony/process": "Required to use the composer class (~3.3).", + "symfony/var-dumper": "Required to use the dd function (~3.3)." + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "5.5-dev" + } + }, + "autoload": { + "psr-4": { + "Illuminate\\Support\\": "" + }, + "files": [ + "helpers.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Taylor Otwell", + "email": "taylor@laravel.com" + } + ], + "description": "The Illuminate Support package.", + "homepage": "https://laravel.com", + "time": "2018-01-19T13:47:08+00:00" + }, + { + "name": "nesbot/carbon", + "version": "1.22.1", + "source": { + "type": "git", + "url": "https://github.com/briannesbitt/Carbon.git", + "reference": "7cdf42c0b1cc763ab7e4c33c47a24e27c66bfccc" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/briannesbitt/Carbon/zipball/7cdf42c0b1cc763ab7e4c33c47a24e27c66bfccc", + "reference": "7cdf42c0b1cc763ab7e4c33c47a24e27c66bfccc", + "shasum": "" + }, + "require": { + "php": ">=5.3.0", + "symfony/translation": "~2.6 || ~3.0" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "~2", + "phpunit/phpunit": "~4.0 || ~5.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.23-dev" + } + }, + "autoload": { + "psr-4": { + "Carbon\\": "src/Carbon/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Brian Nesbitt", + "email": "brian@nesbot.com", + "homepage": "http://nesbot.com" + } + ], + "description": "A simple API extension for DateTime.", + "homepage": "http://carbon.nesbot.com", + "keywords": [ + "date", + "datetime", + "time" + ], + "time": "2017-01-16T07:55:07+00:00" + }, + { + "name": "psr/container", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/container.git", + "reference": "b7ce3b176482dbbc1245ebf52b181af44c2cf55f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/container/zipball/b7ce3b176482dbbc1245ebf52b181af44c2cf55f", + "reference": "b7ce3b176482dbbc1245ebf52b181af44c2cf55f", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Container\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common Container Interface (PHP FIG PSR-11)", + "homepage": "https://github.com/php-fig/container", + "keywords": [ + "PSR-11", + "container", + "container-interface", + "container-interop", + "psr" + ], + "time": "2017-02-14T16:28:37+00:00" + }, + { + "name": "psr/simple-cache", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/simple-cache.git", + "reference": "753fa598e8f3b9966c886fe13f370baa45ef0e24" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/simple-cache/zipball/753fa598e8f3b9966c886fe13f370baa45ef0e24", + "reference": "753fa598e8f3b9966c886fe13f370baa45ef0e24", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\SimpleCache\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interfaces for simple caching", + "keywords": [ + "cache", + "caching", + "psr", + "psr-16", + "simple-cache" + ], + "time": "2017-01-02T13:31:39+00:00" + }, + { + "name": "symfony/polyfill-mbstring", + "version": "v1.6.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-mbstring.git", + "reference": "2ec8b39c38cb16674bbf3fea2b6ce5bf117e1296" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/2ec8b39c38cb16674bbf3fea2b6ce5bf117e1296", + "reference": "2ec8b39c38cb16674bbf3fea2b6ce5bf117e1296", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "suggest": { + "ext-mbstring": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.6-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Polyfill\\Mbstring\\": "" + }, + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for the Mbstring extension", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "mbstring", + "polyfill", + "portable", + "shim" + ], + "time": "2017-10-11T12:05:26+00:00" + }, + { + "name": "symfony/translation", + "version": "v3.4.4", + "source": { + "type": "git", + "url": "https://github.com/symfony/translation.git", + "reference": "10b32cf0eae28b9b39fe26c456c42b19854c4b84" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/translation/zipball/10b32cf0eae28b9b39fe26c456c42b19854c4b84", + "reference": "10b32cf0eae28b9b39fe26c456c42b19854c4b84", + "shasum": "" + }, + "require": { + "php": "^5.5.9|>=7.0.8", + "symfony/polyfill-mbstring": "~1.0" + }, + "conflict": { + "symfony/config": "<2.8", + "symfony/dependency-injection": "<3.4", + "symfony/yaml": "<3.4" + }, + "require-dev": { + "psr/log": "~1.0", + "symfony/config": "~2.8|~3.0|~4.0", + "symfony/dependency-injection": "~3.4|~4.0", + "symfony/finder": "~2.8|~3.0|~4.0", + "symfony/intl": "^2.8.18|^3.2.5|~4.0", + "symfony/yaml": "~3.4|~4.0" + }, + "suggest": { + "psr/log": "To use logging capability in translator", + "symfony/config": "", + "symfony/yaml": "" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.4-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Translation\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Translation Component", + "homepage": "https://symfony.com", + "time": "2018-01-18T22:16:57+00:00" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "dev", + "stability-flags": [], + "prefer-stable": true, + "prefer-lowest": false, + "platform": { + "php": ">=5.5.9" + }, + "platform-dev": [] +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..007c182 --- /dev/null +++ b/readme.md @@ -0,0 +1,33 @@ +### Laravel SEO + + +##### Installation + +Install package + +`composer require jwohlfert23/laravel-seo` + +You can publish the config to change default setting like the base of your title, twitter handle, etc. + +`php artisan vendor:publish --provider="Jwohlfert23\LaravelSeo\ServiceProvider` + +Add `Jwohlfert23\LaravelSeo\SeoTrait` to your base controller. This will allow you to easily change the page title and other meta information directly from your controller like so... +``` +public function index() +{ + $this->seo()->setTitle('Home')->setDescription('This is out home page') + + return view('home') +} +``` + +Insert `app('seo')->render()` into the head of your layout like so... + +``` + +.... +{!! app('seo')->render() !}} +... + +``` + diff --git a/src/Seo.php b/src/Seo.php new file mode 100644 index 0000000..fbeca8f --- /dev/null +++ b/src/Seo.php @@ -0,0 +1,102 @@ +meta, 'absolute_title', $bool); + return $this; + } + + + public function setOgTitle($bool = true) + { + array_set($this->meta, 'og_title', $bool); + return $this; + } + + public function setOgDescription($bool = true) + { + array_set($this->meta, 'og_description', $bool); + return $this; + } + + public function setTitle($value) + { + if (is_array($value)) + $value = implode($this->separator, $value); + + array_set($this->meta, 'title', $value); + return $this; + } + + public function prependTitle($value, $separator = ' - ') + { + $title = array_get($this->meta, 'title'); + array_set($this->meta, 'title', $value . $separator . $title); + return $this; + } + + public function setImage($value, $width = null, $height = null) + { + array_set($this->meta, 'image', $value); + + if ($width) { + array_set($this->meta, 'image_width', (int)$width); + } + if ($height) { + array_set($this->meta, 'image_height', (int)$height); + } + return $this; + } + + public function setDescription($value) + { + array_set($this->meta, 'description', $value); + return $this; + } + + public function setAuthor($value) + { + array_set($this->meta, 'author', $value); + return $this; + } + + public function setKeywords($value) + { + array_set($this->meta, 'keywords', $value); + return $this; + } + + public function setUrl($value) + { + array_set($this->meta, 'url', $value); + return $this; + } + + public function setPublishedTime($value) + { + array_set($this->meta, 'published_time', $value); + return $this; + } + + public function setType($value) + { + array_set($this->meta, 'type', $value); + return $this; + } + + public function render() + { + $view = view('seo::meta', ['meta' => $this->meta])->render(); + + return preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $view); + + } + +} \ No newline at end of file diff --git a/src/SeoTrait.php b/src/SeoTrait.php new file mode 100644 index 0000000..9936c23 --- /dev/null +++ b/src/SeoTrait.php @@ -0,0 +1,14 @@ +loadViewsFrom(__DIR__.'/views', 'seo'); + + $this->publishes([ + __DIR__.'/config/seo.php' => config_path('seo.php'), + ]); + } + + /** + * Register any application services. + * + * This service provider is a great spot to register your various container + * bindings with the application. As you can see, we are registering our + * "Registrar" implementation here. You can add your own bindings too! + * + * @return void + */ + public function register() + { + $this->app->singleton('seo', function ($app) { + return new Seo(); + }); + + } + + +} diff --git a/src/config/seo.php b/src/config/seo.php new file mode 100644 index 0000000..dc87300 --- /dev/null +++ b/src/config/seo.php @@ -0,0 +1,6 @@ + '', + 'twitter_handle' => '' +]; \ No newline at end of file diff --git a/src/views/meta.blade.php b/src/views/meta.blade.php new file mode 100644 index 0000000..e3febf8 --- /dev/null +++ b/src/views/meta.blade.php @@ -0,0 +1,89 @@ + +@if(!empty($meta['title'])) + @if(empty($meta['absolute_title'])) + {{ $meta['title'] . ' - '.$title }} + @else + {{ $meta['title'] }} + @endif +@else + {{$title}} +@endif + + +{{--Google Metadata --}} +@if(!empty($meta['title'])) + +@endif + +@if(!empty($meta['description'])) + +@endif + +@if(!empty($meta['keywords'])) + +@endif + +@if(!empty($meta['author'])) + +@endif + + +{{-- Facebook Metadata --}} + + +@if(!empty($meta['og_title'])) + + + +@elseif(!empty($meta['title'])) + + +@endif + +@if(!empty($meta['og_description'])) + + + + +@elseif(!empty($meta['description'])) + + + +@endif + +@if(!empty($meta['image'])) + + + +@endif + +@if(!empty($meta['image_width'])) + +@endif{{--Facebook Metadata /--}} +@if(!empty($meta['image_height'])) + +@endif + +{{-- Twitter Metadata --}} + +@if($handle = config('seo.twitter_handle')) + +@endif + + +{{-- URL/Canonical Metadata --}} +@if(!empty($meta['url'])) + +@else + +@endif + +{{-- Article Metadata --}} +@if(!empty($meta['published_time'])) + +@endif + +@if(!empty($meta['type'])) + +@endif