├── .gitignore ├── LICENSE ├── OpenGraph.php ├── README.md ├── TwitterCard.php └── composer.json /.gitignore: -------------------------------------------------------------------------------- 1 | $devFiles -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 dragonjet 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /OpenGraph.php: -------------------------------------------------------------------------------- 1 | title = Yii::$app->name; 18 | $this->site_name = Yii::$app->name; 19 | $this->url = Yii::$app->request->absoluteUrl; 20 | $this->description = null; 21 | $this->type = 'article'; 22 | $this->locale = str_replace('-','_',Yii::$app->language); 23 | $this->image = null; 24 | 25 | // Twitter Card 26 | $this->twitter = new TwitterCard; 27 | 28 | // Listed to Begin Page View event to start adding meta 29 | Yii::$app->view->on(View::EVENT_BEGIN_PAGE, function(){ 30 | // Register required and easily determined open graph data 31 | Yii::$app->controller->view->registerMetaTag(['property'=>'og:title', 'content'=>$this->title], 'og:title'); 32 | Yii::$app->controller->view->registerMetaTag(['property'=>'og:site_name', 'content'=>$this->site_name], 'og:site_name'); 33 | Yii::$app->controller->view->registerMetaTag(['property'=>'og:url', 'content'=>$this->url], 'og:url'); 34 | Yii::$app->controller->view->registerMetaTag(['property'=>'og:type', 'content'=>$this->type], 'og:type'); 35 | 36 | // Locale issafe to be specifued since it has default value on Yii applications 37 | Yii::$app->controller->view->registerMetaTag(['property'=>'og:locale', 'content'=>$this->locale], 'og:locale'); 38 | 39 | // Only add a description meta if specified 40 | if($this->description!==null){ 41 | Yii::$app->controller->view->registerMetaTag(['property'=>'og:description', 'content'=>$this->description], 'og:description'); 42 | } 43 | 44 | // Only add an image meta if specified 45 | if($this->image!==null){ 46 | Yii::$app->controller->view->registerMetaTag(['property'=>'og:image', 'content'=>$this->image], 'og:image'); 47 | } 48 | 49 | $this->twitter->registerTags(); 50 | }); 51 | } 52 | 53 | 54 | public function set($metas=[]){ 55 | // Massive assignment by array 56 | foreach($metas as $property=>$content){ 57 | if($property=='twitter'){ 58 | $this->twitter->set($content); 59 | }else if(property_exists($this, $property)){ 60 | $this->$property = $content; 61 | } 62 | } 63 | } 64 | 65 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Open Graph for Yii 2.x 2 | Open Graph implementation for Yii 2 which adds valid meta tags to your HTML output. 3 | 4 | ## Configuration 5 | ``` 6 | 'components' => [ 7 | 'opengraph' => [ 8 | 'class' => 'dragonjet\opengraph\OpenGraph', 9 | ], 10 | //.... 11 | ], 12 | ``` 13 | 14 | ## Usage 15 | The following codes must be used on controller actions before rendering the view. 16 | 17 | ### Usage via Object 18 | ``` 19 | Yii::$app->opengraph->title = 'My_Article'; 20 | Yii::$app->opengraph->description = 'My_Article_Description'; 21 | Yii::$app->opengraph->image = 'http://image.for.my/article'; 22 | return $this->render('My_View_Name'); 23 | ``` 24 | 25 | ### Usage via Array 26 | ``` 27 | Yii::$app->opengraph->set([ 28 | 'title' => 'My_Article', 29 | 'description' => 'My_Article_Description', 30 | 'image' => 'http://image.for.my/article', 31 | ]); 32 | return $this->render('My_View_Name'); 33 | ``` 34 | 35 | ### Twitter Cards 36 | ``` 37 | Yii::$app->opengraph->title = 'My_Article'; 38 | Yii::$app->opengraph->description = 'My_Article_Description'; 39 | Yii::$app->opengraph->image = 'http://image.for.my/article'; 40 | Yii::$app->opengraph->twitter->card = 'summary'; 41 | Yii::$app->opengraph->twitter->site = 'My_Site_Twitter_Username'; 42 | Yii::$app->opengraph->twitter->creator = 'Author_Username'; 43 | return $this->render('My_View_Name'); 44 | ``` 45 | or 46 | ``` 47 | Yii::$app->opengraph->set([ 48 | 'title' => 'My_Article', 49 | 'description' => 'My_Article_Description', 50 | 'image' => 'http://image.for.my/article', 51 | 'twitter' => [ 52 | 'card' => 'summary', 53 | 'site' => 'My_Site_Twitter_Username', 54 | 'creator' => 'Author_Username', 55 | ], 56 | ]); 57 | return $this->render('My_View_Name'); 58 | ``` 59 | 60 | ## Available Properties 61 | #### Title 62 | `Yii::$app->opengraph->title` 63 | 64 | This is the title that shows up on social sharing. In contrast to the view title, this should be simpler and should not contain your branding for best practice, as mentioned on the *Facebook Sharing Guidelines*: 65 | 66 | * "*The title of your article, excluding any branding.*" 67 | * "*The title should not have branding or extraneous information.*" 68 | 69 | e.g. "*MySite.com - Blog - Hello world!*" should just be "*Hello World!*" 70 | 71 | #### Site Name 72 | `Yii::$app->opengraph->site_name` 73 | 74 | [**Automatic**] Your website's name. You do not need to specify this on every controller action if you have an application `name` in your Yii config: 75 | 76 | ``` 77 | return [ 78 | 'id' => 'yiiappid', 79 | 'name' => 'My Website', 80 | //.... 81 | ] 82 | ``` 83 | 84 | #### URL 85 | `Yii::$app->opengraph->url` 86 | 87 | [**Automatic**] This is automatically prefilled with the current URL. You do not need to specify this on every controller action. 88 | 89 | #### Description 90 | `Yii::$app->opengraph->description` 91 | 92 | Description of the current page. Optional but recommended for best results in social sharing. 93 | 94 | #### Object Type 95 | `Yii::$app->opengraph->type` 96 | 97 | The type of object this page will appear on social media. Defaults to `article`. 98 | 99 | #### Locale 100 | `Yii::$app->opengraph->locale` 101 | 102 | [**Automatic**] This is the locale (language) of the open graph object. This defaults to your Yii application language. 103 | 104 | #### Image 105 | `Yii::$app->opengraph->image` 106 | 107 | Image for the graph object. This is highly recommended for best results when shared onto the social media. For best results in Facebook, make this at least `600x315px` -------------------------------------------------------------------------------- /TwitterCard.php: -------------------------------------------------------------------------------- 1 | card = null; 28 | $this->site = null; 29 | $this->site_id = null; 30 | $this->creator = null; 31 | $this->creator_id = null; 32 | } 33 | 34 | public function set($metas=[]){ 35 | // Massive assignment by array 36 | foreach($metas as $property=>$content){ 37 | if(property_exists($this, $property)){ 38 | $this->$property = $content; 39 | } 40 | } 41 | } 42 | 43 | public function registerTags(){ 44 | $this->checkTag('card'); 45 | $this->checkTag('site'); 46 | $this->checkTag('site_id'); 47 | $this->checkTag('creator'); 48 | $this->checkTag('creator_id'); 49 | } 50 | 51 | private function checkTag($property){ 52 | if($this->$property!==null){ 53 | $property = str_replace('_', ':', $property); 54 | Yii::$app->controller->view->registerMetaTag([ 55 | 'property' => 'twitter:'.$property, 56 | 'content' => $this->$property, 57 | ], 'twitter:'.$property); 58 | } 59 | } 60 | 61 | } -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dragonjet/yii2-opengraph", 3 | "description": "Open Graph implementation for Yii 2", 4 | "keywords": ["yii2", "extension", "open-graph", "facebook"], 5 | "homepage": "https://github.com/dragonjet/yii2-opengraph", 6 | "type": "yii2-extension", 7 | "license": "MIT", 8 | "authors": [ 9 | { 10 | "name": "dragonjet", 11 | "email": "jetriconew@gmail.com", 12 | "homepage": "http://jetri.co" 13 | } 14 | ], 15 | "minimum-stability": "stable", 16 | "require": { 17 | "yiisoft/yii2": "@dev" 18 | }, 19 | "autoload": { 20 | "psr-4": { 21 | "dragonjet\\opengraph\\": "" 22 | } 23 | } 24 | } 25 | --------------------------------------------------------------------------------