Skip to content

Instantly share code, notes, and snippets.

@mi-dexigner
Created November 18, 2025 12:50
Show Gist options
  • Select an option

  • Save mi-dexigner/bedc92294d2c277fdb919f2fcbd5f549 to your computer and use it in GitHub Desktop.

Select an option

Save mi-dexigner/bedc92294d2c277fdb919f2fcbd5f549 to your computer and use it in GitHub Desktop.
Yii Framework php

#YII Framwwork

basic command Advance project

  1. Intro & install

  2. directory

  3. controller FirstCOntroller.php url index.php?r=first/index first controller name index function name

  4. route

  5. model

  6. view -- conreoller code public function actionIndex(){ // $this->layout = false; $token = bin2hex(random_bytes(16)); $response = []; $response['name']="Code With Idris"; $response['list']=["Test","Demo","CRUD"];

    return $this->renderPartial('index',$response); // with layout // return $this->render('index'); // without layout }

-- view code passing

Hello

use yii\helpers\VarDumper;

VarDumper::dump($list);

?>

7) layout

  • open web.php
  • set the layout path and layout file
'layoutPath'=>'@app/views/layouts2',
'layout'=>'main2',

AppAsset.php clone name is MainAsset.php then open MainAssets.php

<?php
/**
 * @link https://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license https://www.yiiframework.com/license/
 */

namespace app\assets;

use yii\web\AssetBundle;

/**
 * Main application asset bundle.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class MainAsset extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [
      'https://cdn.jsdelivr.net/npm/tailwindcss@3.3.3/dist/tailwind.min.css',
        'css/app.css'
    ];
    public $js = [
      'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.0/jquery.min.js',
        'https://cdn.jsdelivr.net/npm/alpinejs@3.12.0/dist/cdn.min.js',
        'https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4',
        'js/app.js'
    ];
    // Add CDN links instead of local files
    public $cssOptions = ['position' => \yii\web\View::POS_HEAD];
    public $jsOptions = ['position' => \yii\web\View::POS_END];

}
  • view layour file or others files layout set
<li><a href="<?= Url::home() ?>" class="nav-link px-2 link-secondary">Home</a></li>
          <li><a href="<?= Url::to(['site/index']) ?>" class="nav-link px-2 link-secondary">Overview</a></li>
          <li><a href="<?= Url::to(['site/inventory']) ?>" class="nav-link px-2 link-secondary">Inventory</a></li>
          <li><a href="<?= Url::to(['site/customers']) ?>" class="nav-link px-2 link-secondary">Customers</a></li>
          <li><a href="<?= Url::to(['site/products']) ?>" class="nav-link px-2 link-secondary">Products</a></li>
          <li><?php echo Html::a('About', ['site/about'], ['class' => 'nav-link px-2 link-body-emphasis']);?></li>

ROutes set

  • open web.php
 'rules' => [
                // Define your routes explicitly
        '' => 'site/index',
        'first/index' => 'first/index',
        // Add more explicit routes here
             'say/<message>' => 'site/say',
             'mytest'=>'first/test',
             [
                'pattern' => 'firsts/<other>',
                    'route' => 'first/info',
               'defaults' => ['other' => 'default','key'=>'123456'], // optional

             ],
             'mytest2'=>'first/test2'

            ],
- open controller `FirstController.php`
```php
 public function actionInfo(){
      $data = Yii::$app->request->get();
      var_dump($data);
      return "Inforamtion";
    }

Database

$data = Yii::$app->db->createCommand('select * from users')->queryAll();
      var_dump($data);

Multiple Database

  • clone the file db.php to db2.php
  • and open web.php find the db key and variable for duplicate like below
$db= require __DIR__ . '/db.php';
$db2 = require __DIR__ . '/db2.php';
'db' => $db,
'db2' => $db2,
  1. query builder query builder open first disable the pretty url and type url like localhost:8080/index.php?r=gii

  2. gii

  3. advanced

  4. advanced directory

  5. load css / js separate page like register

  6. session

  7. component

  8. widget

  9. pagination

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