yii2 高级应用程序模板如何新添加一个应用

飛(fìe) 4月前 289

一、前提

  • 假设我们已经下好了yii2 advanced版,advanced版中默认已经有几个应用了backend、frontend 和 common,现在我们再加一个mobileapp应用

二、复制一个应用

  • 复制一份 frontend,改名为mobileapp
  • 这个时候你./init初始化的时候并不会识别mobileapp这个应用,还需要去修改environments目录下的配置文件。

三、添加新应用到环境里

  • 修改environments目录下的index.php,修改配置如下:
  • 添加40、41两行代码
  • 添加51一行代码
  • 添加62、63两行代码
  • 添加71一行代码
    <?php
    /**
    * The manifest of files that are local to specific environment.
    * This file returns a list of environments that the application
    * may be installed under. The returned data must be in the following
    * format:
    *
    * ```php
    * return [
    *     'environment name' => [
    *         'path' => 'directory storing the local files',
    *         'skipFiles'  => [
    *             // list of files that should only copied once and skipped if they already exist
    *         ],
    *         'setWritable' => [
    *             // list of directories that should be set writable
    *         ],
    *         'setExecutable' => [
    *             // list of files that should be set executable
    *         ],
    *         'setCookieValidationKey' => [
    *             // list of config files that need to be inserted with automatically generated cookie validation keys
    *         ],
    *         'createSymlink' => [
    *             // list of symlinks to be created. Keys are symlinks, and values are the targets.
    *         ],
    *     ],
    * ];
    * ```
    */
    return [
    'Development' => [
        'path' => 'dev',
        'setWritable' => [
            'backend/runtime',
            'backend/web/assets',
            'console/runtime',
            'frontend/runtime',
            'frontend/web/assets',
            'mobileapp/runtime', <<<<-这里
            'mobileapp/web/assets', <<<<-这里
        ],
        'setExecutable' => [
            'yii',
            'yii_test',
        ],
        'setCookieValidationKey' => [
            'backend/config/main-local.php',
            'common/config/codeception-local.php',
            'frontend/config/main-local.php',
            'mobileapp/config/main-local.php', <<<<-这里
        ],
    ],
    'Production' => [
        'path' => 'prod',
        'setWritable' => [
            'backend/runtime',
            'backend/web/assets',
            'console/runtime',
            'frontend/runtime',
            'frontend/web/assets',
            'mobileapp/runtime', <<<<-这里
            'mobileapp/web/assets', <<<<-这里
        ],
        'setExecutable' => [
            'yii',
        ],
        'setCookieValidationKey' => [
            'backend/config/main-local.php',
            'frontend/config/main-local.php',
            'mobileapp/config/main-local.php', <<<<-这里
        ],
    ],
    ];
  • 到environments目录下的dev复制一份frontend改名为mobileapp
  • 到environments目录下的prod复制一份frontend改名为mobileapp

四、初始化应用

  • 在终端下项目根目录执行./init,结果如下,就有mobileapp了

    Yii Application Initialization Tool v1.0
    Which environment do you want the application to be initialized in?
    
    [0] Development
    [1] Production
    
    Your choice [0-1, or "q" to quit] 0
    
    Initialize the application under 'Development' environment? [yes|no] yes
    
    Start initialization ...
    
    unchanged mobileapp/config/test-local.php
    unchanged mobileapp/config/params-local.php
      exist mobileapp/config/main-local.php
            ...overwrite? [Yes|No|All|Quit] All
    overwrite mobileapp/config/main-local.php <<<<-这里
    unchanged mobileapp/config/codeception-local.php <<<<-这里
    unchanged mobileapp/web/index.php <<<<-这里
    unchanged mobileapp/web/robots.txt <<<<-这里
    unchanged mobileapp/web/index-test.php <<<<-这里
    unchanged .DS_Store
    unchanged frontend/config/test-local.php
    unchanged frontend/config/params-local.php
    overwrite frontend/config/main-local.php
    unchanged frontend/config/codeception-local.php
    unchanged frontend/web/index.php
    unchanged frontend/web/robots.txt
    unchanged frontend/web/index-test.php
    unchanged yii
    unchanged backend/config/test-local.php
    unchanged backend/config/params-local.php
    overwrite backend/config/main-local.php
    unchanged backend/config/codeception-local.php
    unchanged backend/web/index.php
    unchanged backend/web/robots.txt
    unchanged backend/web/index-test.php
    unchanged common/config/test-local.php
    unchanged common/config/params-local.php
    unchanged common/config/main-local.php
    overwrite common/config/codeception-local.php
    unchanged yii_test.bat
    unchanged yii_test
    unchanged console/config/test-local.php
    unchanged console/config/params-local.php
    unchanged console/config/main-local.php
    generate cookie validation key in backend/config/main-local.php
    generate cookie validation key in common/config/codeception-local.php
    generate cookie validation key in frontend/config/main-local.php
    generate cookie validation key in mobileapp/config/main-local.php <<<<-
      chmod 0777 backend/runtime
      chmod 0777 backend/web/assets
      chmod 0777 console/runtime
      chmod 0777 frontend/runtime
      chmod 0777 frontend/web/assets
      chmod 0777 mobileapp/runtime <<<<-这里
      chmod 0777 mobileapp/web/assets <<<<-这里
      chmod 0755 yii
      chmod 0755 yii_test
    
    ... initialization completed.

五、最后

  • 去mobileapp下面修改main.php中有关frontend为mobileapp
  • 控制器等相关文件命名空间改为mobileapp
  • 到common/confing/bootstrap.php文件修改为Yii::setAlias('@api', dirname(dirname(DIR)) . '/mobileapp');

以上,一个新的mobileapp应用就添加完成了,要再添加多个也一样的步骤

最新回复 (0)
    • 都市菜鸟网
      2
        立即登录 立即注册 
返回
发新帖