= Yii 2 Support for Social Networks = == Twitter == * Share a Link button: [[https://about.twitter.com/resources/buttons]] // Twitter: Share a link $curUri = Html::encode(Yii::$app->params['companyWebsite'].Yii::$app->request->getUrl()); echo 'Tweet'; echo Html::script( "!function(d,s,id){ var js, fjs=d.getElementsByTagName(s)[0], p=/^http:/.test(d.location) ? 'http' : 'https' ; if(!d.getElementById(id)){ js=d.createElement(s); js.id=id; js.src=p+'://platform.twitter.com/widgets.js'; fjs.parentNode.insertBefore(js,fjs); } }(document, 'script', 'twitter-wjs');" ); echo "\n\n"; == LinkedIn == * Share button: [[https://developer.linkedin.com/plugins/share]] // LinkedIn Share button $curUri = Html::encode(Yii::$app->params['companyWebsite'].Yii::$app->request->getUrl()); echo ''; echo ''; == Pinterest == * Pin-it button: [[https://developers.pinterest.com/tools/widget-builder/]] // Pinterest Pin-it button $curUri = Html::encode(Yii::$app->params['companyWebsite'].Yii::$app->request->getUrl()); echo ' '; echo ''; == Google+ == * +1 button: [[https://developers.google.com/+/web/+1button/]] $curUri = Html::encode(Yii::$app->params['companyWebsite'].Yii::$app->request->getUrl()); // Google+ G+1 button //echo ''; // Place this tag in your head or just before your close body tag. //echo ''; // Place this tag where you want the +1 button to render. //echo '
'; echo ''; // Place this tag where you want the +1 button to render. echo '
'; // Place this tag after the last +1 button tag echo Html::script(" (function() { var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true; po.src = 'https://apis.google.com/js/platform.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s); })();");
== Facebook == * Like Button: [[https://developers.facebook.com/docs/plugins/like-button]] * Share Button: [[https://developers.facebook.com/docs/plugins/share-button]] Step 1: Include the JavaScript SDK on your page once, ideally right after the opening tag.
Step 2: Place this code wherever you want the plugin to appear on your page.
Alternatively, when using ''iframe'': === Custom Metatags in Views === You need the following metatags for the pages being generated: Your Website Title ... In Yii, these meta tags have to be inserted in the required view: $curUri = Html::encode(Yii::$app->params['companyWebsite'].Yii::$app->request->getUrl()); $this->registerMetaTag(['property' => 'og:url', 'content' => $curUri]); $this->registerMetaTag(['property' => 'og:type', 'content' => 'article']); $this->registerMetaTag(['property' => 'og:title', 'content' => Html::encode($this->title)]); $this->registerMetaTag(['property' => 'og:description', 'content' => strip_tags($model['intro_text'])]); $this->registerMetaTag(['property' => 'og:image', 'content' => Html::encode(Yii::$app->params['companyWebsite'] . Yii::$app->urlManagerBackend->createUrl('').'media/'.$model['main_image'] )]); echo "\n\n"; // Load Facebook SDK for JavaScript echo '
'; echo "\n\n"; // Facebook 'Like' button code echo '
'; echo "\n\n";
=== Custom Metatags in Controllers === Alternatively, you can automatically add it to each view. First, add it to your controller, or create your own extended version of ''Controller'', such as ''MyController'': class MyController extends Controller { public function beforeAction($event) { $this->view->title = Yii::$app->params['pageTitle']; // Get customized meta tags (keywords, author, etc.) from the params config file as a default. \Yii::$app->view->registerMetaTag(Yii::$app->params['pageDefaultDescription'], "default_description"); \Yii::$app->view->registerMetaTag(Yii::$app->params['pageDefaultKeywords'], "default_keywords"); \Yii::$app->view->registerMetaTag(Yii::$app->params['pageDefaultAuthor'], "default_author"); // Open Graph Tags \Yii::$app->view->registerMetaTag(Yii::$app->params['pageDefaultOG_Description'], "default_og_description"); \Yii::$app->view->registerMetaTag(Yii::$app->params['pageDefaultOG_SiteName'], "default_og_sitename"); \Yii::$app->view->registerMetaTag(Yii::$app->params['pageDefaultOG_Title'], "default_og_title"); \Yii::$app->view->registerMetaTag(Yii::$app->params['pageDefaultOG_Type'], "default_og_type"); return parent::beforeAction($event); } } Any new controllers created would extend ''MyController'' (instead of ''Controller''), to get the defaults meta tags attached automatically. If you need to customize a meta tag in some actions, you can override it: class FancyController extends MyController { ... public function actionIndex() { // Replace the default value for 'description' \Yii::$app->view->registerMetaTag([ 'name' => 'description', 'content' => 'A custom description for Index view', 'id' => 'main_index' ], 'main_index' ); return $this->render('index'); } } Source: [[http://stackoverflow.com/questions/30171328/yii2-how-to-replace-customize-meta-tag-from-view-with-default-meta-tags-in-la|Stackoverflow: Customer Meta Tags in Yii2]]