Loading javascript files from the vendors in CakePHP 3

前端 未结 3 1243
不知归路
不知归路 2021-01-18 10:04

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

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-18 10:46

    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.

提交回复
热议问题