Yii2-常用应用配置

基础版配置 web.php
高级版配置 main.php

1. 资源 装配

  /assets/AppAsset.php

2.入口index.php配置 

  /web/index.php配置
  defined('YII_DEBUG') or define('YII_DEBUG', false);      //debug mode: true
  defined('YII_ENV') or define('YII_ENV', 'prod');         //debug mode: dev

3.  配置数据库

    3.1系统默认配置

   /config/db.php
   return [
        'class' => 'yii\db\Connection',
        'dsn' => 'mysql:host=localhost;dbname=yii2basic',
        'username' => 'root',
        'password' => '',
        'charset' => 'utf8',
    ];




 3.2常用数据库配置

      /config/db.php
   return [
        'class' => 'yii\db\Connection',
        //'dsn' => 'mssql:host=localhost;dbname=shop_obd2', // MySQL, MariaDB
        //'dsn' => 'sqlite:/path/to/database/file', // SQLite
        //'dsn' => 'pgsql:host=localhost;port=5432;dbname=mydatabase', // PostgreSQL
        //'dsn' => 'cubrid:dbname=demodb;host=localhost;port=33000', // CUBRID
        'dsn' => 'sqlsrv:Server=184.168.194.53;Database=xms_mbstarshop', // MS SQL Server, sqlsrv driver
        //'dsn' => 'dblib:host=localhost;dbname=mydatabase', // MS SQL Server, dblib driver
        //'dsn' => 'mssql:host=localhost;dbname=Sql_CarSets_co_uk', // MS SQL Server, mssql driver
        //'dsn' => 'oci:dbname=//localhost:1521/mydatabase', // Oracle
       
        'username' => 'xmu_mbstarshop',
        'password' => ' ',
        'charset' => 'utf8',
        //'tablePrefix' => 'tb_',
    ];




4.WEB应用配置config/web.php

   4.1 别名配置


    'aliases' => [
       '@name1' => 'path/to/path1',        // @xxx 表示别名
       '@name2' => 'path/to/path2',
     ],

 4.2 修改布局模板

   --- 框架默认定义 ---


   yii2/base/application.php
         public $layout = 'main';

           --- 自定义配置 ---


            /config/web.php
            'layout' => 'main',
    4.3 修改默认路由

  --- 框架默认路由 ---

        yii2/web/application.php
        public $defaultRoute = 'site';

        --- 自定义修改路由 ---

        /config/web.php
        'defaultRoute' => 'index',

  4.4 配置路由 ( 配置reUrl, 和  /config/web.php 只能二选一,否则其中一个不工作)


     4.4.1 /config/web.php配置

  'urlManager' => [
            'showScriptName' => false,   // Disable index.php
            'enablePrettyUrl' => true,   // Disable r= routes
            'enableStrictParsing' => false,
            'rules'=>array(
                [
                'pattern' => 'test/about',
                'route' => 'site/about',
                //'suffix' => '.do',
                ],
                'wholesale/<title:\w+>'=>'site/<title>',
            '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
            '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
             ),
           
        ],


  4.4.2  在/web/.htaccess

      Options FollowSymLinks
      RewriteEngine on
      RewriteRule test/abc.do$ index.php?r=/site/contact [L]
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule !\.(js|ico|txt|gif|jpg|png|css|html|xml)$ /index.php
      Options -Indexes




  4.5 设置Cookie加密key

         /config/web.php
         'components' => [
             'request' => [
                // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
                'cookieValidationKey' => 'life2015',
             ],

         ],




 





5. 资源装配配置

  5.1 /assets/AppAsset.php
    public $css = [
        'css/site.css',        //附加到header
    ];
    public $js = [
                    //'test/test1.js',默认应用根目录前缀:/web/  ,附加到footer
    ]; 

 

   5.2 --- 在视图脚本中注册,调用 ---

    方法一
    AppAsset::register($this);   //调用已资源管理器中已配置的文件
    ?>
    <?php $this->beginPage() ?>
    <!DOCTYPE html>
    <html lang="<?= Yii::$app->language ?>">
        <head>
        ...
        </head>
        <body>
        <?php $this->beginBody() ?>
        ...

        <?php $this->endBody() ?>    <!-- 相当于点位符 -->
    </body>
    </html>
    <?php $this->endPage() ?>


    方法二,注册文件


    $this->registerJsFile('abc.js');   //加载在尾部,但在jquery.js之前
    $this->registerJsFile('aaa.js', ['depends' => 'app\assets\AppAsset']);  //加载在jquery.js之后

    方法三,注册 code block

    可以直接写脚本,或

      <?php$this->beginBlock('test')?> 

$(function($){

$('#mybutton').click(function(){

alert('OK');

});

});

<?php$this->endBlock()?>

<?php$this->registerJs($this->blocks['test'], \yii\web\View::POS_END); ?>

    $this->registerJs($this->blocks['test']);   //加载在尾部,但在jquery.js之前
    $this->registerJs($this->blocks['test'],  ['depends' => 'app\assets\AppAsset']);  //加载在jquery.js之后




6. /config/params.php 配置参数:
  \Yii::$app->params['recentPosts']




--- 在控制器中修改视图中的 $this->title
      \Yii::$app->view->title= 'from controller abc'; 









  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值