Change website

From Jan 16 2015,


All post content will be move to we's offical website with many content...

Can access website here: http://justox.com

Thanks for your visit!

Wednesday, 18 December 2013

Simple Zend Framework Meta Helper

When i started to work with Zend Framework, i actually didn’t understand plugins and loaders, now when i made my own CMS based on Zend i actually did everything with plugins and view helpers for making my life easier.
I will show you here how you can make plugin, view helper which will do all hard work for you so your web site can be more SEO friendlier. Also, i need to tell you that i don’t understand SEO too much and you will maybe need to add some features or tags based on your own knowledge about SEO.


So, let’s make first file in Library/My/View/Helper - Seo.php
class My_View_Helper_Seo extends Zend_View_Helper_Abstract {
     public function seo() {
         $concatTitle = function() {
            $title = null;
            if( null !== $this->view->title ){
                 $title = $this->view->title.' - ';
            }
            $section = null;
            if( null !== $this->view->section ) {
                 $section = $this->view->section.' - ';
            }
            return $title.$section.Config::getConfig('engine')->website->title;
        };

        if( null !== $this->view->description ) {
            $description = $this->view->description;
        } else {
            $description = Config::getConfig('engine')->website->description;
        }
        if( null !== $this->view->keywords ) {
            $keywords = $this->view->keywords;
        } else {
            $keywords = Config::getConfig('engine')->website->keywords;
        }
        if( null !== $this->view->image_src ) {
            $image_src = $this->view->image_src;
        } else {
            $image_src = Config::getConfig('engine')->website->image_src;
        }
        if( null !== $this->view->robots ) {
            $robots = $this->view->robots;
        } else {
            $robots = Config::getConfig('engine')->website->robots;
        }

        $html = '<html lang="'.substr(Config::getConfig('engine')->website->locale, 0, stripos(Config::getConfig('engine')->website->locale, "_")).'" xmlns="http://www.w3.org/1999/xhtml" itemscope itemtype="http://schema.org/Blog" xmlns:fb="http://ogp.me/ns/fb#">'. PHP_EOL;
        $html .= ''. PHP_EOL;
        $html .= ''.$concatTitle($options, $this).''. PHP_EOL;
        $html .= '<meta http-equiv="Content-Type" content="text/html; charset='.Config::getConfig('engine')->website->charset.'" />'. PHP_EOL;
        $html .= '<meta name="description" content="'.$description.'" />'. PHP_EOL;
        $html .= '<meta name="keywords" content="'.$keywords.'" />'. PHP_EOL;
        $html .= '<meta name="robots" content="'.$robots.'" />'. PHP_EOL;
        $html .= '<meta content="'.$concatTitle($options, $this).'" />'. PHP_EOL;
        $html .= '<meta content="'.Globals::getConfig('engine')->website->title.'" />'. PHP_EOL;
        $html .= '<meta content="'.$this->view->getAbsolutePath().$image_src.'" />'. PHP_EOL;
        $html .= '<meta content="article" />'. PHP_EOL;
        $html .= '<meta content="'.$description.'" />'. PHP_EOL;
        $html .= '<meta content="'.$this->view->getAbsolutePath().$_SERVER['REQUEST_URI'].'" />'. PHP_EOL;
        $html .= '<link href="'.$this->view->getAbsolutePath().$image_src.'" rel="image_src" />'. PHP_EOL;
        $html .= '<link href="'.$this->view->getAbsolutePath().$_SERVER['REQUEST_URI'].'" rel="canonical" />'. PHP_EOL;
        return $html;
    }
}
You will notice that i have Config class and getAbsolutePath() additional view helpers and i will not cover those helpers here. What you can do is, you can make your own helpers for doing the same thing
or figure out how to make absolute path helper etcor you can visit links to check and download those helpers.
Now what you can do is adding code to your Controller Action, example IndexController.php
class IndexController extends Zend_Controller_Action
{
    public function indexAction(){
        $this->view->robots = 'index, follow';
        $this->view->title = 'Some title pulled from database';
        $this->view->description = 'Description of post/page';
        $this->view->keywords = 'some, keywords, comma separated';
        $this->view->image_src = 'image/source/path';
        $this->view->section = 'Section - eg. Blog';
    }
}
And you need add one line of code to your layout.phtml in head section
$this->seo();
And you are done, if any question regarding to this plugin i will make additional changes to blog post to cover all questions. Thanks

No comments:

Post a Comment