从CakePHP 3中的供应商处加载javascript文件 [英] Loading javascript files from the vendors in CakePHP 3

查看:63
本文介绍了从CakePHP 3中的供应商处加载javascript文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是如何从CakePHP 3.0中的vendor文件夹加载.js文件。我已经通过作曲家加入了Twitter Bootstrap。 .js文件位于/ vendor / twbs / bootstrap-sass / assets / javascripts /文件夹中。我不想将其移至webroot,因为那样我会破坏作曲家提供的自动更新功能。有什么好的建议吗?我不想复制文件并失去作曲家的利益...

My problem is how to load .js files from the vendors folder in CakePHP 3.0. I have included twitter bootstrap via composer. The .js file is located in /vendor/twbs/bootstrap-sass/assets/javascripts/ folder. I don't want to move it to webroot because then I'll break the auto-update functionality provided from composer. Any good suggestions? I don't want to duplicate files and loose the composer benefits...

推荐答案

我找到了解决方案!如果我使用的是作曲家,为什么不也使用它呢? :)

I've found a solution! If I'm using composer why not to use it for this too, right? :)

在composer.json中:

In composer.json:

"scripts": {
    "post-install-cmd": "App\\Console\\Installer::postInstall",
    "post-update-cmd": "App\\Console\\Installer::postUpdate"
}

在src / Console / Installer.php中:

In 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');
    }

}

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

And finally if you are using git add webroot/bootstrap.js to .gitignore. The postUpdate runs after every composer update command, so if you want to run the script after every actual package update just use post-package-update instead of post-update-cmd.

这篇关于从CakePHP 3中的供应商处加载javascript文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆