Skip to content

Instantly share code, notes, and snippets.

@dungphanxuan
Last active August 28, 2017 08:44
Show Gist options
  • Save dungphanxuan/7f34f29380fd1aae56fc to your computer and use it in GitHub Desktop.
Save dungphanxuan/7f34f29380fd1aae56fc to your computer and use it in GitHub Desktop.
Yii2 Snippets

Yii2 Snippet

Link Document

Github: https://github.com/yiisoft/yii2/blob/master/docs/guide/README.md

Install Yii

 composer global require "fxp/composer-asset-plugin:~1.1.1"
 composer create-project --prefer-dist yiisoft/yii2-app-basic basic

Yii2 Documents

Yii2 Awesome project for starter

Config Apache

 # Set document root to be "basic/web"
 DocumentRoot "path/to/basic/web"
 
 <Directory "path/to/basic/web">
     # use mod_rewrite for pretty URL support
     RewriteEngine on
     # If a directory or a file exists, use the request directly
     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteCond %{REQUEST_FILENAME} !-d
     # Otherwise forward the request to index.php
     RewriteRule . index.php
 
     # ...other settings...
 </Directory>

ListView Example

 <?php echo \yii\widgets\ListView::widget([
        'dataProvider'=>$dataProvider,
        'pager'=>[
            'hideOnSinglePage'=>true,
        ],
        'summary'=>'',
        'itemView'=>'_item',
        'layout' => "{items}\n <tr class='lpagination'><td colspan=\"8\">{pager}</td></tr>",
        'options' => [
            'class'=>'main-wrapper'
        ],
        'itemOptions' => [
            'tag' => false,
        ],
        'viewParams'=>[
            'param' => $param
        ]
    ])?>

Form Error Summary

  <?php echo $form->errorSummary($model,[
        'class'=>'alert alert-warning alert-dismissible',
        'header'=>' <button type="button" class="close" data-dismiss="alert" aria-hidden="true">�</button><h4><i class="icon fa fa-warning"></i> Please fix error!</h4>'
    ]); ?>
   

OrderBy Field

  $recipes = Model::find()
    ->where(['in', 'id', $id])
    ->orderBy([\yii\db\Expression('FIELD (id, 1,2,3')])
    ->all();
   

DataProvince soft and pagination

  $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => [
                'defaultOrder' => [
                    'id' => SORT_DESC
                ]
            ],
            'pagination' => [
                'pageSize' => Yii::$app->keyStorage->get('pageSize', 15),
            ],
        ]);
   

Config Mailer for using PHP mail

    'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'useFileTransport' => false,
            'messageConfig' => [
                'charset' => 'UTF-8',
                'from' => ['admin@app.com' => 'App Name'],
            ],
            'transport' => [
                'class' => 'Swift_MailTransport',
            ],
        ],
   

Config Mailer for using Gmail

    'mailer' => [
            'class'            => 'yii\swiftmailer\Mailer',
            'useFileTransport' => false,
            'messageConfig'    => [
                'charset' => 'UTF-8',
                'from'    => [ 'contact@gmail.com' => 'App Name'],
            ],
            'transport' => [
                'class' => 'Swift_SmtpTransport',
                'host' => 'smtp.gmail.com',
                'username' => 'contact@gmail.com',
                'password' => 'YourAppPass',
                'port' => '587',
                'encryption' => 'tls',
            ],
        ],
        
   

Send email

 Yii::$app->mailer->compose()
    ->setTo('client@email.com')
    ->setFrom(['admin@app.com' => 'App Name'])
    ....

Check File System exits

Yii::$app->fileStorage->getFilesystem()->has(FileBaseUrl)

Clear Filesystem cache

$path = Yii::getAlias('@storage'). '\cache';
                $files = glob($path.'\*');
                foreach($files as $file){
                    if(is_file($file))
                        unlink($file);
                }

Big data

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment