Yii 1.1: seo

SEO - Search engine optimization in no time
87 followers

Current version 0.9.2c

SEO is a modest extension which allows for easy search engine optimization within your Yii-application.

Links

What's included?

  • SeoRecordBehavior - Active record behavior for defining the model URL
  • SeoControllerBehavior - Controller behavior for setting page meta data
  • SeoFilter - Controller filter for correcting incorrect URLs
  • SeoMetaWidget - Widget for rendering page meta data

Usage

I assume that you have some basic knowledge about search engine optimization.

SeoRecordBehavior

To define the URL for your model add the SeoRecordBehavior to your model:

public function behaviors()
{
    return array(
        .....
        array(
            'class'=>'ext.seo.components.SeoRecordBehavior',
            'route'=>'model/view',
            'params'=>array('id'=>$this->id, 'name'=>$this->name),
        ),
    );
}

In this case we add the following rule to the UrlManager configuration:

'urlManager'=>array(
    'urlFormat'=>'path', // when doing SEO, this should always be set to 'path'
    'rules'=>array(
        .....
        'model/<id:\d+>-<name>.html'=>'model/view', // e.g. model/1-model+name.html
    ),
),

Note that you need to include all params in your URL rewrite rule that you specified for the SeoRecordBehavior, in this case id and name.

When rendering a link to 'model/view', instead of using the array format:

array('model/view', 'id'=>$model->id, 'foo'=>'bar')

Use the getUrl or getAbsoluteUrl method:

$model->getUrl(array('foo'=>'bar')

This will ensure that the correct URL is displayed everywhere.

SeoFilter

To automatically do 301 redirects for incorrect URLs add the SeoFilter to your controller:

public function filters()
{
    return array(
        .....
        array('ext.seo.components.SeoFilter + view'), // apply the filter to the view-action
    );
}

This is useful when the model name has been updated an it's used in the SEO optimized URL. In other words this will automatically correct the URL with a proper redirect when e.g. a crawler visits an old URL.

SeoControllerBehavior

To allow for setting page meta data add the SeoControllerBehavior to your controller:

public function behaviors()
{
    return array(
        .....
        'seo'=>array('class'=>'ext.seo.components.SeoControllerBehavior'),
    );
}

You can now set the page meta data and canonical in your view:

$this->metaKeywords = 'these, are, my, sample, page, meta, keywords';
$this->metaDescription = 'This is a sample page description';
$this->addMetaProperty('fb:app_id',Yii::app()->params['fbAppId']);
$this->canonical = $model->getAbsoluteUrl(); // canonical URLs should always be absolute

SeoMeta

To render SEO related tags call the SeoHead widget in your layout:

<?php Yii::app()->controller->widget('ext.seo.widgets.SeoHead',array(
    'httpEquivs'=>array(
        'Content-Type'=>'text/html; charset=utf-8',
        'Content-Language'=>'en-US'
    ),
    'defaultDescription'=>'This is a sample page description.',
    'defaultKeywords'=>'these, are, my, default, sample, page, meta, keywords',
)); ?>

In version 0.9.2 SeoMeta was renamed to SeoHead.

SeoTitle

Since 0.9.2 SeoTitle is rendered by SeoHead so you no longer need to call it yourself.

You don't have to set the page title for every single view, instead the widget will build the title from your breadcrumbs.

If you wish you can still set the page title as a string in your view:

<?php $this->pageTitle = 'Model name - Parent name - Application name'; ?>

Or you can set the page title as an array:

<?php $this->pageTitle = array(
    'Model name',
    'Parent name',
    'Application name',
); ?>

Changes

Oct 21, 2011

  • Release 0.9.2c
    • Added a check to prevent recursive canonical redirects

Oct 20, 2011

  • Release 0.9.2b
    • Fixed a typo in the SeoHead widget

Oct 20, 2011

  • Release 0.9.2
    • Renamed the SeoMeta widget to SeoHead
    • SeoHead now renders the SeoTitle as well

Oct 18, 2011

  • Release 0.9.1b
    • Fixed an issue with parsing breadcrumbs for the SeoTitle widget

Oct 14, 2011

  • Release 0.9.1
    • Added the SeoTitle widget
    • Renamed the SeoMetaWidget to SeoMeta

Oct 12, 2011

  • Release 0.9.0
    • Initial release

Total 20 comments

#10937 report it
Speeedfire at 2012/12/05 06:31am
Meta property

Hy, i use the MetaHead widget with the metaproperty og:image. But i wanna overwrite in another controller this property, dont overwrite this.

Example controller:

$this->metaKeywords = Yii::app()->params['metaKeywords'].','.$model->getTags();
$this->metaDescription = $model->teaser;
$this->addMetaProperty('og:image',$og_image);
#10870 report it
asifrafeeq at 2012/11/29 03:18am
i m facing the following problem

include(Controller.php) [function.include]: failed to open stream: No such file or directory

#10573 report it
jiaming at 2012/11/05 01:09pm
How to implement in the index page....?

Thanks for this great extension...

But I want to implement this into the index page too.

For example , I have model "questions" belongs to "category"..(cid=>category_id)

So my url is something like

www.123.com/questions/index/cid/1

How can I convert it into:

www.123.com/questions/index/cid/1?category=first+category

Thanks so much for the help!

#7604 report it
rookie84 at 2012/04/02 02:37pm
great work.

Great work as always..! Thanks.

#5567 report it
Chris83 at 2011/10/20 07:44pm
@Asgaroth

Thanks again!

Sorry for the inconvenience. Seems like a merge gone bad.

You can report issues here: https://bitbucket.org/Crisu83/yii-seo/issues/new

#5566 report it
Asgaroth at 2011/10/20 07:37pm
Thanks

Thansk to you for such an amazing extension!!! I just love it

You need to update your files again, I think your upload missed the canonical part described in the documentation

$this->canonical = $model->getAbsoluteUrl(); // canonical URLs should always be absolute

that throws an error, I had to add the attribute to the Controller Behavior and modify the SeoHead acordinly.

pd: should i be posting issues/bug somewhere else? Im feeling like this is not the right place :P

#5564 report it
Chris83 at 2011/10/20 07:05pm
@Asgaroth

Thanks for reporting this issue. It has been fixed in the newest release (0.9.2b).

#5559 report it
Asgaroth at 2011/10/20 10:10am
Bug

There is a bug in the new SeoHead widget on line 109:

if (isset($pager['class']))

should be:

if (isset($title['class']))
#5540 report it
Chris83 at 2011/10/18 02:10pm
@Asgaroth

I actually had it like that in the beginning, but you're right that it would probably be better to use a single method.

The reason why I choose to have to separate methods was because Yii has two different methods. Could you explain in detail why you'd prefer a single method for this?

#5539 report it
Asgaroth at 2011/10/18 01:32pm
Absolute URLs

I have modified the SeoRecordBehavior a little, to allow the generation of Absolute URLs, i needed this when generating my sitemap, and could be usefull for other cases:

/**
     * Returns the URL for this model.
     * @param array $params additional GET parameters (name=>value)
     * @param boolean $absolute wheter or not the returned URL should be absolute
     * @return string the URL
     */
    public function getUrl($params=array(), $absolute = false)
    {
        $fn = $absolute ? "createAbsoluteUrl" : "createUrl";
        return Yii::app()->$fn($this->route, CMap::mergeArray($params, $this->params));
    }
#5533 report it
Chris83 at 2011/10/18 07:53am
@roopz

Now I understand what you're getting at. Updating of the content is done when e.g. the Googlebot crawls your site, if content has changed it will notice it. However, if you use the name in the URL and you change the name, then the old URL will be incorrect and we need to tell the crawlers that the page has been moved. This can be done with the SeoFilter.

Does this answer your question?

#5532 report it
Roopz at 2011/10/18 06:37am
Google SEO

Google says frequent updation of files will be an advantage in case of SEO. Updation files means, actually what kind of updation, if we changed the file name its an updation right ? if we changing the content it would be an updation right ? if we cahnge the content of a home page (i mean index page ) by executing one crone file in my server, i mean i will we done on every day morning at a constant time, which will open the index.php file, and edit the index.php by removing somelines, and then adding some lines (some times it may the same). have you got the point ? does it works fine in case of SEO ? this is my question ...!!

#5531 report it
Chris83 at 2011/10/18 06:25am
@roopz

I'm sorry but I'm not really sure what you mean, could you try rephrase your question?

#5527 report it
Roopz at 2011/10/18 05:23am
Now, Title is ok.. :)

Is this SEO Extension will do the all the SEO things, I means its enough right? does it need any thing else for SEO purpose ? how to make the regular updation of the files made easy ? is it right to put one crone file and and update the file ?

#5526 report it
Chris83 at 2011/10/18 05:16am
@rrbot

No it doesn't but it could, I'll add this for the next release. I'm also thinking that maybe a single widget would be enough for outputting the SEO tags in the layout.

#5525 report it
Chris83 at 2011/10/18 05:05am
@Asgaroth

This was caused by a bug in the widget. I've fixed the issue in the newest version (0.9.1b). Sorry for the inconvenience.

#5486 report it
Asgaroth at 2011/10/15 03:41pm
Array to string conversion

Im getting Array to string conversion when using arrays for breadcrumbs like:

<?php
$this->breadcrumbs=array(
    'Shop' => array('shop/index'),
    'Categories' => array('index'),
    $model->title,
);
 
?>
protected/extensions/seo/widgets/SeoTitle.php(69): implode(array("Cardiologia", array("index"), array("shop/index"), "My App"), " | ")

Chaging line 43 in SeoTitle.php to:

$parts[] = !empty($value) ? (is_array($value) ? $key : $value) : $key;

Fixes the problem

#5484 report it
rrbot at 2011/10/15 12:47pm
Conical

Does this extension handle conical meta?

http://googlewebmastercentral.blogspot.com/2009/02/specify-your-canonical.html

#5452 report it
Chris83 at 2011/10/13 03:55am
@roopz

I tried to explain it in the "Usage" section. Could you please be more specific on what you don't understand I'll try to answer your questions.

#5451 report it
Roopz at 2011/10/13 02:45am
Really appreciate you ... But i does not know how to use it . Can you pleas explain it ?

Thanks in advance.

Leave a comment

Please to leave your comment.

Create extension