Parameter

  1. symfony获取系统配置

     # 系统配置在3个位置定义
     # 后面文件出现相同参数时会覆盖之前的
     # 后面文件出现不同同参数时会在之前的配置上追加
     /app/config/parameters.yml
     parameters:
         parameter: 123
    
     /app/config/services.yml
     parameters:
         parameter: 123
    
     /app/config/config.yml
     parameters:
         parameter: 123
    
  2. symfony传递系统配置

     # 从控制器获取
     $parameter = $this->getParameter('parameter');
    
     # 从命令行文件获取
     $parameter = $this->getContainer()->getParameter('parameter');
    
     # 从服务文件传递,服务文件位置默认位置为:
     /app/config/service.yml
     /src/XxxBundle/Resources/config/services.yml
     # /app/config/config.yml中也可以自定义服务文件的位置,文件格式可以是yml、php、xml
    

配置数据库,创建Bundle

  1. 安装symfony3.1.4
  2. 打开项目/app/config/parameters.yml,加入如下多行:
     # This file is auto-generated during the composer install
     parameters:
         database_host: 127.0.0.1
         database_port: 3306
         database_name: test
         database_user: root
         database_password: null
         mailer_transport: smtp
         mailer_host: 127.0.0.1
         mailer_user: null
         mailer_password: null
         secret: 834e393f935f542bd5004877e1e5df8c42f44f8c
    
  3. 打开项目/app/config/config.yml,修改如下多行:
     # Doctrine Configuration
     doctrine:
         dbal:
             default_connection: default
             connections:
                 default:
                     driver:   pdo_mysql
                     host:     '%database_host1%'
                     port:     '%database_port1%'
                     dbname:   '%database_name1%'
                     user:     '%database_user1%'
                     password: '%database_password1%'
                     charset:  UTF8
                 slave:
                     driver:   pdo_mysql
                     host:     '%database_host2%'
                     port:     '%database_port2%'
                     dbname:   '%database_name2%'
                     user:     '%database_user2%'
                     password: '%database_password2%'
                     charset:  UTF8
                     mapping_types:
                         enum: string
                         set: string
                         varbinary: string
                         tinyblob: text
         orm:
             default_entity_manager: default
             entity_managers:
                 default:
                     connection: default
                     mappings: ~
                         # DBBundle: ~
                 slave:
                     connection: slave
                     mappings: ~
                         # DBBundle: ~
    

    常用全局配置文件

  4. 读取配置
     app\config\config.yml
    
  5. 添加配置
     app\config\parameters.yml
    
  6. 配置路由
     app\config\routing.yml
    

    路由相关记录

  7. 路由前缀文件在:

    项目/app/config/routing.yml

    Bundle名字

     sina:
         #Bundle文件位置
         resource: "@SinaBundle/Controller/"
         #Bundle配置类型:注释
         type:     annotation
         #Bundle路由前缀
         prefix:   /
    
  8. 路由配置在:

    项目/src/SinaBundle/Resources/config/routing.yml

  9. 方法的路由别名

     index:
         #方法路由路径/参数/参数
         path:      /add/{BookType}/{BookName}
         #方法的路径
         defaults: { _controller: TormBundle:Torm:add }
         #参数格式化
         requirements:
             #参数1为正整数
             BookType: \d+
             #参数2为正整数
             BookName: \d+
    
  10. 查看所有路由

     php bin/console debug:router
    
  11. 读取yml配置

     use Symfony\Component\Yaml\Yaml;
    
     $config = Yaml::parse(file_get_contents('yml文件路径'));
    
  12. 配置多数据库

    • 多数据库
    • mongodb
    • composer
      composer require alcaeus/mongo-php-adapter doctrine/mongodb-odm doctrine/mongodb-odm-bundle
      
    • redis
      https://github.com/snc/SncRedisBundle
      https://github.com/snc/SncRedisBundle/blob/master/Resources/doc/index.md
      http://www.redis.cn/
      https://redis.io/commands
      
  13. 为symfony添加Mongodb支持

    • 参考链接
      # 换源
      composer config repo.packagist composer https://packagist.phpcomposer.com
      # 安装Bundle
      composer require alcaeus/mongo-php-adapter
      composer require doctrine/mongodb
      composer require doctrine/mongodb-odm
      composer require doctrine/mongodb-odm-bundle
      
  14. 自动加载:修改 app/autoload.php,加上

     use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
     AnnotationDriver::registerAnnotationClasses();
    
  15. 内核处理:修改 app/AppKernel.php,加上

     public function registerBundles()
     {
         $bundles = array(
             new Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle(),
         );
     }
    
  16. 配置文件:修改 app/config/parameters.yml,加上

     parameters:
         mongodb_server: "mongodb://127.0.0.1:27017"
    
  17. 配置文件:修改 app/config/config.yml,加上

     doctrine_mongodb:
         connections:
             default:
                 server: "%mongodb_server%"
                 options: {}
         default_database: database
         document_managers:
             default:
                 auto_mapping: true
    
  18. 为symfony添加Redis支持

    • 参考链接
      composer require snc/redis-bundle
      composer require predis/predis
      
  19. 内核处理:修改 app/AppKernel.php,加上

     public function registerBundles()
     {
         $bundles = array(
             new Snc\RedisBundle\SncRedisBundle(),
         );
     }
    
  20. 配置文件:修改 app/config/config.yml,加上

     snc_redis:
         clients:
             default:
                 type: predis
                 alias: default
                 dsn: redis://127.0.0.1
                 logging: %kernel.debug%
             cache:
                 type: predis
                 alias: cache
                 dsn: redis://127.0.0.1/1
                 options:
                     profile: 2.2
                     connection_timeout: 10
                     read_write_timeout: 30
             session:
                 type: predis
                 alias: session
                 dsn: redis://localhost/2
             cluster:
                 type: predis
                 alias: cluster
                 dsn:
                     - redis://127.0.0.1/3?weight=10
                     - redis://127.0.0.1/4?weight=5
                     - redis://127.0.0.1/5?weight=1
    
@耿志环 2012-∞ 冀ICP备17033181号, powered by Gitbook修订: 2018-05-23 16:25:59

results matching ""

    No results matching ""