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'; 









Yii 2.0 权威指南 本教程的发布遵循 Yii 文档使用许可. 版权所有 2014 (c) Yii Software LLC. 介绍 已定稿 关于 Yii 已定稿 从 Yii 1.1 升级 入门 已定稿 安装 Yii 已定稿 运行应用 已定稿 第一次问候 已定稿 使用 Forms 已定稿 玩转 Databases 已定稿 用 Gii 生成代码 已定稿 更上一层楼 应用结构 已定稿 结构概述 已定稿 入口脚本 已定稿 应用 已定稿 应用组件 已定稿 控制器(Controller) 已定稿 视图(View) 已定稿 模型(Model) 已定稿 过滤器 已定稿 小部件(Widget) 已定稿 模块(Module) 已定稿 前端资源(Asset) 已定稿 扩展(extensions) 请求处理 已定稿 运行概述 已定稿 引导(Bootstrapping) 已定稿 路由(Route)引导与创建 URL 已定稿 请求(Request) 已定稿 响应(Response) 已定稿 Sessions(会话)和 Cookies 已定稿 错误处理 已定稿 日志 关键概念 已定稿 组件(Component) 已定稿 属性(Property) 已定稿 事件(Event) 已定稿 行为(Behavior) 已定稿 配置(Configurations) 已定稿 类自动加载(Autoloading) 已定稿 别名(Alias) 已定稿 服务定位器(Service Locator) 已定稿 依赖注入容器(DI Container) 配合数据库工作 编撰中 数据访问对象(DAO) - 数据库连接、基本查询、事务和模式操作 编撰中 查询生成器(Query Builder) - 使用简单抽象层查询数据库 编撰中 活动记录(Active Record) - 活动记录对象关系映射(ORM),检索和操作记录、定义关联关系 编撰中 数据库迁移(Migration) - 在团体开发中对你的数据库使用版本控制 待定中 Sphinx 待定中 Redis 待定中 MongoDB 待定中 ElasticSearch 接收用户数据 编撰中 创建表单 已定稿 输入验证 编撰中 文件上传 待定中 多模型同时输入 显示数据 编撰中 格式化输出数据 待定中 分页(Pagination) 待定中 排序(Sorting) 编撰中 数据提供器 编撰中 数据小部件 编撰中 主题 安全 编撰中 认证(Authentication) 编撰中 授权(Authorization) 编撰中 处理密码 待定中 客户端认证 待定中 安全领域的最佳实践 缓存 已定稿 概述 已定稿 数据缓存 已定稿 片段缓存 已定稿 分页缓存 已定稿 HTTP 缓存 RESTful Web 服务 已定稿 快速入门 已定稿 资源 已定稿 路由 已定稿 格式化响应 已定稿 授权验证 已定稿 速率限制 已定稿 版本化 已定稿 错误处理 已定稿 测试
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值