douxitao8170 2013-09-20 12:13
浏览 38
已采纳

如何使ZF2 db凭据环境特定

I am starting a new project.

I am using ZF2. I have just installed it and have the Skeleton application up and running.

This is my deployment process:

  • I develop on my local machine
  • I then push to my public github repository
  • I then use deployhq.com to deploy those to my production server which is where the user would see the changes made.

I have tried to look around stack, zend site, and google at blogs etc but still dont have any real understanding or solution to my problem.

I want the application to use different database credentials based on its environment. E.g. if on 'dev', my local machine, then use credentials A, but if on live server, then use credentials B.

I have read a lot about global and local autoload config files etc, but baring in mind my github repo is public, any where I commit any config files with my db details would be visible.

I was wondering if there was a way to have, the same theory, global and local files with the DB connections in, i upload the production details manually, not via git for security reason, and tell git to ignore the local config file somehow? I would also need to know how I tell the application to use those config files based on the environment and there location.

  • 写回答

1条回答 默认 最新

  • drbmhd9583 2013-09-20 15:47
    关注

    In Zend 2 There are

    1. Global configuration file &
    2. Module level configuration file

    IF you want to know there use you can refer the link below

    How does configuration works in ZF2

    When I had a same scenario I used the above link to understand and exploit Zend Config module which is really good to handle the situation like this .

    create two files

    production.php local.php

    in both these files

    return this array based on the environment

    return array(
         "dbname" => "yourdbname"
         "dbhostname" => "dbhostname"
         "dbusername" => "yourdbusername",
         "dbpassword" => "yourdbpassword"
     );
    

    in config/autoload/ directory of your zend framewrok application

    later edit your config/application.config.php file as per below instructions

    // get the application env from Apache vhost file ( here you can set in your apache vhost file as production or local )

    $applicationEnv = getenv('APPLICATION_ENV');
    

    $environmentSpecificConfigPath = "config/autoload/{,*.}{".$applicationEnv.",local}.php";

    // Next with in the config array pass the environment specific configuration path

     'config_glob_paths' => array($environmentSpecificConfigPath)
    

    in any controller or action

    you can just use the below code

     $configArray = $this->getGlobalConfig(); 
    

    Now $configarray has all your DB credentials to create a connection

    $adapter = new Zend\Db\Adapter\Adapter(array(
        'driver' => 'Mysqli',
        'database' => $configArray['dbname'],
        'username' => $configArray['dbusername'],
        'password' => $configArray['dbpassword']
     ));
    

    If you use config array to connect the DB in your entire application you dont need to worry about environment changes just make sure you have an Apache APPLICATION_ENV entry in your vhost file

    you can do that by adding below line in your apache vhost file

     SetEnv APPLICATION_ENV "production" // in your production server 
    
     SetEnv APPLICATION_ENV "local"  // in your local 
    

    Also Last but not least you can use the Zend Experts module ZeDB

    https://github.com/ZendExperts/ZeDb

    To manage your CRUD applications

    Hope the above steps may help you in creating the environment

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?