Friday, June 08, 2012

YII: How to make Clean SEO-friendly URLs


* Create Slug field in your database table
If you prefer not to create slug every time someone goes to the homepage or the main news page, you can create slug field in your database table.  It is a lot easier to keep track and you can modify the slug later as well. 

* Use SlugBehaviors - http://www.yiiframework.com/extension/slug-behavior/
Installation was almost too easy.  You download and extract the release file under models/behaviors 
And in the model, add the following: (e.g. /protected/model/post.php)

public function behaviors(){
    return array(
        'SlugBehavior' => array(
            'class' => 'application.models.behaviors.SlugBehavior',
            'slug_col' => 'slug',
            'title_col' => 'title',
            'max_slug_chars' => 125,
            'overwrite' => false
        )
    );
}

That's it! 

* Modify config/main.php
The tricky part was how to modify the UrlManager in /protected/config/main.php as below: 

 'urlManager' => array(
….
                'post/<id:\d+>/<slug>/*' => 'post/view',
                'post/<id:\d+>/*' => 'post/view',
), 

Make sure the order is correct or else they wouldn't work because the first rule is applied first. 

* Use Chtml::link to create links
Then, you can use Chtml::link in the following way
 echo CHtml::link(CHtml::decode($data->title), array('/post/view/', 'id'=>$data->id, 'slug'=>$data->slug));
                
Here are some articles that was helpful!

Wednesday, June 06, 2012

YII: How to Customize YII pager in CGridView

If you want to customize YII pagination or pare in CGridView usually located in view files, you can specify cssFile in pager inside CGridView.
$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'post-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
        'cssFile' => Yii::app()->theme->baseUrl."/css/cgridviewcss.css",
    'columns'=>array(
                'id',
                'title',
                'content',
                array(
                   'class'=>'CButtonColumn',
                ),
    ),
        'htmlOptions'=>array('class'=>'add_css_class_name_for_CGridView_here'),
        'pager'=> array(
            'header' => '',  //if you want to get read of pager heading
            'cssFile' =>Yii::app()->theme->baseUrl."/css/style-of-pager.css",
        ),
Then, you can create style-of-pager.css and modify the pager.css (that is in the Yii Framework).
http://www.yiiframework.com/forum/index.php/topic/26055-change-the-general-pager-css-for-all-application/ 

YII: How to Add CSS Class in Form textArea

You can add css class in the _form.php in YII views folder.

In _form.php file (/protected/views/post/_form.php)

echo $form->textArea($model,'title',array('class'=>'put_class_name_here')); 

YII: How to Sort or Reverse CGridView Display Order

Here is how you can sort or reverse the order of CGridView (showing the newest on top by default).

In the Controller (/protected/controllers/postController.php)

public function actionAdmin() {    $model = new Post('search');    $model->unsetAttributes();    if (isset($_GET['Post']))        $model->attributes = $_GET['Post'];
    $this->render('admin', array(        'model' => $model,    ));

In the Model (/protected/models/post.php)

public function search()        {                // Warning: Please modify the following code to remove attributes that                // should not be searched.
                $criteria=new CDbCriteria;
                $criteria->compare('id',$this->id);                $criteria->compare('foo',$this->foo,true);                $criteria->compare('bar',$this->bar,true);                ....
                return new CActiveDataProvider($this, array(                        'criteria' => $criteria,                        'sort' => array(                             'defaultOrder' => 'post_date DESC',  // this is it.                        ),                        'pagination' => array(                                'pageSize' => 30,                        ),                ));        }
 Thank softark for the answer!!
http://www.yiiframework.com/forum/index.php/topic/21523-yii-reverse-cgridview-display-order/

Monday, June 04, 2012

How to Specify each row's id with Zii Widgets Grid CGridView in Yii


When I tried to use zii.widgets.grid.CGridView in a different controller, CButtonColumn would mess up because the id would return the controller the CGridView is placed.

To specify each row's id for the CButtonColumn, you can do the following.  ^^

$this->widget('zii.widgets.grid.CGridView', array(
 'id'=>'label-grid',
 'dataProvider'=>$labelmodel,
 'columns'=>array(
  'id',
  'label',
  array(
                    'class'=>'CButtonColumn',
                    'viewButtonUrl'=>
                     'Yii::app()->controller->createUrl("label/view", 
                                       array("id"=>$data->id))',
                    'updateButtonUrl'=>
                     'Yii::app()->controller->createUrl("label/update",       
                                       array("id"=>$data->id))',
                    'deleteButtonUrl'=>
                   'Yii::app()->controller->createUrl("label/delete", 
                                      array("id"=>$data->id))',
  ),
 ),
));  

Friday, June 01, 2012

Top Three Surefire Steps to Increase # of "Likes" on your Facebook page (Work in Progress)



These are small suggestions but surefire steps that works:


1. Photo size.  Know that 90% of Facebook users interact in their Newsfeed.  They aren't going to read tiny letters inside the photos.


2. New Likes.  Interact with them and reward them with something valuable.  They will be the key influencer for your page to reach friends of friends.  

3. Invite friends.  Look out for a few active supporters of your page and ask them to become an "ambassador" on behalf of the page.  Continue interacting with them and reach out to their networks of friends. 


Suggest below things that you've tried and worked! 


How to Set dynamic page titles in YII

One way to specify the page title in YII, you can go to controller/action, and set it this way.
public function actionIndex() {
     $this->pageTitle = "Put page title here";
     $this->render('index');
}

In the header template or layout template, you can do the following:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="language" content="en" />   
    <title><?php echo CHtml::encode($this->pageTitle); ?></title>
</head>
Or in the view page, you can specify the pageTitle. 
$this->setPageTitle('Put page title here');