Loading javascript files from the vendors in CakePHP 3

前端 未结 3 1244
不知归路
不知归路 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:39

    I don't like to copy files so based on previous answers i have created function which create links to bootstrap files. Note, that for Bootstrap4, which distributed without fonts now.

    /**
     * Create Bootstrap4 assets links
     *
     * @param string $dir The application's root directory.
     * @param \Composer\IO\IOInterface $io IO interface to write to console.
     * @return void
     */
    
    public static function createBootstrapLinks($dir, $io) {
        $ds = DIRECTORY_SEPARATOR;
        if (!file_exists($dir . $ds . 'vendor' . $ds . 'twbs' . $ds . 'bootstrap'))
        {
            $io->write('Bootstrap is not installed. Include Bootstrap into project: composer require twbs/bootstrap');
            return;
        }
        $bootstrapAssetsDir = realpath($dir . $ds . 'vendor' . $ds . 'twbs' . $ds . 'bootstrap' . $ds . 'dist');
        $bootstrapAssets = [
            'css' => $bootstrapAssetsDir . $ds . 'css' . $ds,
            'js' => $bootstrapAssetsDir . $ds . 'js' . $ds
        ];
        $webrootDir = realpath($dir . $ds . 'webroot');
    
        $webrootPathes = [
            'css' => $webrootDir . $ds . 'css' . $ds . 'bootstrap',
            'js' => $webrootDir . $ds . 'js' . $ds . 'bootstrap'
        ];
    
        foreach ($bootstrapAssets as $type => $asset)
        {
            if (!file_exists($asset))
            {
                $io->write('Asset `' . $asset . '` does not exists. Unable to create link.');
                continue;
            }
            $name = isset($webrootPathes[$type]) ? $webrootPathes[$type] : ($webrootDir . $type . $ds . 'bootstrap');
            if (file_exists($name))
            {
                switch(filetype($name))
                {
                    case 'link':
                        $link_target = readlink($name);
                        if ($link_target == $asset)
                        {
                            $io->write('Correct link to `' . $asset . '` already exists. Fine.');
                        }
                        else
                        {
                            $io->write('Link `' . $name . '` already exists and points to `'. $link_target .'`. Unable to create link.');
                        }
                        break;
                    case 'dir':
                        $io->write('Dir `' . $name . '` already exists. Unable to create link.');
                        break;
                    default:
                        $io->write(filetype($name).' `' . $name . '` already exists. Unable to create link.');
                        break;
                }
            }
            else
            {
                $io->write('Link `' . $name . '` to `' . $asset . '` created. Fine.');
                symlink($asset, $name);
            }
        }
    }
    

提交回复
热议问题