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
I've found a solution! If I'm using composer why not to use it for this too, right? :)
In composer.json:
"scripts": {
"post-install-cmd": "App\\Console\\Installer::postInstall",
"post-update-cmd": "App\\Console\\Installer::postUpdate"
}
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');
}
}
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.