Skip to content

Commit

Permalink
Restructured the package. This version is not backward compatible.
Browse files Browse the repository at this point in the history
  • Loading branch information
anagh-p committed Oct 14, 2016
1 parent 987ae5c commit 940df05
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 195 deletions.
68 changes: 28 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,53 +1,44 @@
# quintype-seo-php
A composer package for seo for all quintype projects
A composer package for SEO for all quintype projects

###Important : If any change is made to the package, do the following.
* Create a new release.
* Update the package in [Packagist](https://packagist.org/).
* To use the new version of the package in any project, change the version number in composer.json file and run
* To use the new version of the package in any project, change the version number in composer.json file and run
``` $ composer update ```

Note : We are making use of a package called Meta (https://github.com/quintype/meta) forked from https://github.com/RyanNielson/meta for dynamically adding the meta tags into the pages.

Instructions to include the package into a project.

### In composer.json, add a repository pointing to the forked meta repository.
### In composer.json, require Meta and SEO packages.
```sh
"repositories": [
"require": {
...
...
{
"type": "vcs",
"url": "https://github.com/quintype/meta"
}
],
"quintype/seo":"2.0.0",
"quintype/meta":"2.0.0"
},
```

### In composer.json, require the original Meta package and this seo package.
### Install or update the composer packages.
```sh
"require": {
...
...
"quintype/seo":"0.0.5",
"ryannielson/meta":"dev-master"
},
$ composer install
or
$ composer update
```
### In the Laravel config/app.php file, include Meta provider and give an alias to it.

### In the Laravel config/app.php file, add aliases to the packages for convenience.
```sh
'providers' => [
...
...
RyanNielson\Meta\MetaServiceProvider::class
],

'aliases' => [
...
...
'Meta' => RyanNielson\Meta\Meta::class
'Meta' => Quintype\Meta\Meta::class,
'Seo' => Quintype\Seo\Seo::class
],
```

### Add an attribute called 'title' in config/quintype.php file as a fall-back value if it is not recieved from the meta data.
### Add an attribute called 'title' in config/quintype.php file as a fall-back value if it is not received from the meta data.
```sh
return [
...
Expand All @@ -56,31 +47,28 @@ return [
];
```

### Install or update the composer packages.
```sh
$ composer install
or
$ composer update
```
### Include both Meta and SEO classes in the necessary controllers.
```sh
use Meta;
use Quintype\Seo;
use Seo;
```
### Create a constructor function to initialize the Meta object and config variable if necessary.

### Create a constructor function to initialize the Meta and SEO objects. Note: Do not forget to pass the config(merge local config and config from API response) to SEO package.
```sh
public function __construct(){
parent::__construct();
$this->meta = new Meta;
$this->config = $this->client->config();
$this->client = new Api(config("quintype.api-host"));
$this->config = array_merge($this->client->config(), config('quintype'));
$this->meta = new Meta();
$this->seo = new Seo($this->config);
}
```
### Prepare the data required for meta tags.
```sh
// Setting Seo meta tags
$page = ["type" => "home"];//Type of the page. In this case, it is the home page.
$home = new Seo\Home(array_merge($this->config, config('quintype')), $page["type"]);//Since it is the home page, initialize the Home object.
$this->meta->set($home->tags());//Set the tags received in the above step.

$page = ["type" => PAGE_TYPE];//eg. home, section, story etc.
//Set SEO meta tags.
$setSeo = $this->seo->FUNCTION_CORRESPONDING_TO_PAGE(ARGUMENTS);//eg. home($arguments), section($arguments), story($arguments) etc.
$this->meta->set($setSeo->prepareTags());
```

### In the function to render the view, include the meta data.
Expand Down
37 changes: 37 additions & 0 deletions src/Quintype/Seo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Quintype\Seo;

class Seo
{
public function __construct($config){
$this->config = $config;
}
public function home($pageType){
return new Home($this->config, $pageType);
}

public function search($query){
return new Search($query);
}

public function section($pageType, $sectionName, $sectionId){
return new Section($this->config, $pageType, $sectionName, $sectionId);
}

public function staticPage($title){
return new StaticPage($title);
}

public function story($pageType, $story){
return new Story($this->config, $pageType, $story);
}

public function storyElement($pageType, $story, $element){
return new StoryElement($this->config, $pageType, $story, $element);
}

public function tag($tag){
return new Tag($this->config, $tag);
}
}
23 changes: 11 additions & 12 deletions src/Quintype/Seo/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,34 @@
namespace Quintype\Seo;

class Base {
function __construct($config, $pageType, $section_id=''){
function __construct($config, $pageType, $sectionId = ''){
$this->config = $config;
$this->pageType = $pageType;
$this->section_id = $section_id;
$this->sectionId = $sectionId;
$this->seoMetadata = $this->getSeoMetadata();
}

private function getSeoMetadata() {

if(sizeof($this->config['seo-metadata'])>0){
$key = 'owner-type';
$found = 0;

if($this->pageType == 'section'){//If it is a section page.
foreach ($this->config['seo-metadata'] as $metadata) {
if (array_key_exists($key, $metadata) && $metadata[$key]==$this->pageType && $metadata['owner-id']==$this->section_id) {
$found = 1;
return $metadata['data'];
}
if (array_key_exists($key, $metadata) && $metadata[$key]==$this->pageType && $metadata['owner-id']==$this->sectionId) {
$found = 1;
return $metadata['data'];
}
}
if(!$found){
return array();
}
} else {
foreach ($this->config['seo-metadata'] as $metadata) {
if (array_key_exists($key, $metadata) && $metadata[$key]==$this->pageType) {
$found = 1;
return $metadata['data'];
}
if (array_key_exists($key, $metadata) && $metadata[$key]==$this->pageType) {
$found = 1;
return $metadata['data'];
}
}
if(!$found){
return array();
Expand Down Expand Up @@ -101,4 +100,4 @@ protected function getCanonicalUrl(){
return $this->config['sketches-host'] . "/". $this->story['slug'];
}
}
}
}
55 changes: 25 additions & 30 deletions src/Quintype/Seo/Home.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,35 @@

namespace Quintype\Seo;

require "Base.php";

class Home extends Base {

function tags() {
class Home extends Base
{
function prepareTags() {
if (sizeof($this->seoMetadata)>0){

return [
'title' => trim($this->getPageTitle()),
'description' => trim($this->getDescription()),
'keywords' => trim($this->getKeywords()),
'og' => [
'title' => trim($this->getTitle()),
'description' => trim($this->getDescription())
],
'twitter' => [
'title' => trim($this->getTitle()),
'description' => trim($this->getDescription())
],
'msvalidate.01' => $this->getBingId(),
'fb' => [
'app_id' => $this->getFacebookData('app-id'),
'pages' => $this->getFacebookData('pages')
],
'alternate' => [
'href' => '/feed',
'type' => 'application/atom+xml',
'title' => trim($this->getPageTitle()) . " ATOM Feed"
]
];
'description' => trim($this->getDescription()),
'keywords' => trim($this->getKeywords()),
'og' => [
'title' => trim($this->getTitle()),
'description' => trim($this->getDescription())
],
'twitter' => [
'title' => trim($this->getTitle()),
'description' => trim($this->getDescription())
],
'msvalidate.01' => $this->getBingId(),
'fb' => [
'app_id' => $this->getFacebookData('app-id'),
'pages' => $this->getFacebookData('pages')
],
'alternate' => [
'href' => '/feed',
'type' => 'application/atom+xml',
'title' => trim($this->getPageTitle()) . " ATOM Feed"
]
];
} else {
return ['title' => trim($this->getPageTitle())];
}
}


}
}
13 changes: 5 additions & 8 deletions src/Quintype/Seo/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

namespace Quintype\Seo;

require "Base.php";

class Search extends Base {

function __construct($config, $pageType, $query){
class Search
{
function __construct($query){
$this->query = $query;
}

function tags() {
return ['title' => trim($this->query) ." - Search Results"];
function prepareTags() {
return ['title' => trim($this->query) ." - Search Results"];
}

}
45 changes: 20 additions & 25 deletions src/Quintype/Seo/Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,28 @@

namespace Quintype\Seo;

require "Base.php";

class Section extends Base {

function __construct($config, $pageType, $section, $section_id = ''){
parent::__construct($config, $pageType, $section_id);
$this->section = $section;
class Section extends Base
{
function __construct($config, $pageType, $sectionName, $sectionId){
parent::__construct($config, $pageType, $sectionId);
$this->sectionName = $sectionName;
}

function tags() {
function prepareTags() {
if (sizeof($this->seoMetadata)>0){

return [
'title' => trim($this->getPageTitle()),
'description' => trim($this->getDescription()),
'keywords' => trim($this->getKeywords()),
'og' => [
'title' => trim($this->getTitle()),
'description' => trim($this->getDescription())
],
'twitter' => [
'title' => trim($this->getTitle()),
'description' => trim($this->getDescription())
]
];
'description' => trim($this->getDescription()),
'keywords' => trim($this->getKeywords()),
'og' => [
'title' => trim($this->getTitle()),
'description' => trim($this->getDescription())
],
'twitter' => [
'title' => trim($this->getTitle()),
'description' => trim($this->getDescription())
]
];
} else {
return ['title' => trim($this->getPageTitle())];
}
Expand All @@ -35,14 +32,12 @@ function tags() {
protected function getPageTitle(){
if(isset($this->seoMetadata['page-title'])){
if($this->seoMetadata['page-title']==''){
return $this->section . " - " . $this->config['title'];
return $this->sectionName . " - " . $this->config['title'];
}else{
return $this->seoMetadata['page-title'];
}
} else {
return $this->section . " - " . $this->config['title'];
return $this->sectionName . " - " . $this->config['title'];
}
}


}
}
11 changes: 4 additions & 7 deletions src/Quintype/Seo/StaticPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

namespace Quintype\Seo;

require "Base.php";

class StaticPage extends Base {

class StaticPage extends Base
{
function __construct($title){
$this->title = $title;
}

function tags() {
return ['title' => trim($this->title)];
function prepareTags() {
return ['title' => trim($this->title)];
}

}
Loading

0 comments on commit 940df05

Please sign in to comment.