从CakePHP 3中的供应商处加载javascript文件

mut*_*dsu 6 twitter-bootstrap cakephp-3.0

我的问题是如何从CakePHP 3.0中的vendors文件夹加载.js文件.我通过composer包含了twitter bootstrap..js文件位于/ vendor/twbs/bootstrap-sass/assets/javascripts /文件夹中.我不想将它移动到webroot,因为那样我将破坏作曲家提供的自动更新功能.有什么好建议吗?我不想复制文件和松散作曲家的好处......

mut*_*dsu 6

我找到了解决方案!如果我正在使用作曲家为什么不使用它,对吧?:)

在composer.json中:

"scripts": {
    "post-install-cmd": "App\\Console\\Installer::postInstall",
    "post-update-cmd": "App\\Console\\Installer::postUpdate"
}
Run Code Online (Sandbox Code Playgroud)

在src/Console/Installer.php中:

public static function postUpdate(Event $event) {
    $io = $event->getIO();

    $rootDir = dirname(dirname(__DIR__));

    static::copyTwitterBootstrapFiles($rootDir, $io);
}

public static function copyTwitterBootstrapFiles($dir, $io) {

    $bootstrapJsSource = $dir . '/vendor/twbs/bootstrap-sass/assets/javascripts/bootstrap.js';
    $bootstrapJsDestination = $dir . '/webroot/js/bootstrap.js';

    if (file_exists($bootstrapJsSource)) {
        copy($bootstrapJsSource, $bootstrapJsDestination);
        $io->write('Copied `bootstrap.js` file');
    }

}
Run Code Online (Sandbox Code Playgroud)

最后,如果您使用git将webroot/bootstrap.js添加到.gitignore.postUpdate在每个composer update命令之后运行,因此如果要在每次实际的包更新后运行脚本,只需使用post-package-update而不是post-update-cmd.