How to use a vendor file (installed through Composer) in Cake-PHP-View?

I have a problem here, and I got the feeling of a simple and clean solution that I have not yet found ... My Cake-PHP application looks like this:

  • Projectfolder
    • application
    • vendors
    • composer.json

Cake-PHP and the external file that I want to use are installed inside the providers folder. In providers, I have a twitter bootstrap package for twitter that has a css- and js file that I want to include in my view, but it is not available, since this file does not remain inside the webroot-Folder of my -PHP cake. Now my question is: how do I make both of these files available in my Cake-PHP project WITHOUT copying them to the webroot folder? Using symbolic links is a bit like a dirty hack for me ... There should be a clean solution, because otherwise using Cake-PHP with Composer would not make any sense. My composer.json looks like this:

{ "name": "MyProject", "version": "0.0.0", "require": { "php": ">=5.5.11", "cakephp/cakephp": "2.6.3", "composer/installers": "*", "twbs/bootstrap": "3.3.4", "components/jquery": "2.1.3" }, "extra" : { "installer-paths":{ "plugins/{$name}":["type:cakephp-plugin"], "app/webroot/bootstrap":["twbs/bootstrap"] } }, "config": { "vendor-dir": "vendors" } 

Btw: the composer ignores this installation path for bootstrapping, since the package is not of type ...

+6
source share
2 answers

Using tws/bootstrap will require an additional step to install these assets. This is often done using symlinking or copying.

For example, when adding Bootstrap to Symfony2, you would need

  "require" : { "mopa/bootstrap-bundle": "dev-master", "twbs/bootstrap": "dev-master", }, "scripts": { "post-install-cmd": [ /* sensio commands **/ "Mopa\\Bundle\\BootstrapBundle\\Composer\\ScriptHandler::postInstallSymlinkTwitterBootstrap" ], "post-update-cmd": [ /* sensio commands **/ "Mopa\\Bundle\\BootstrapBundle\\Composer\\ScriptHandler::postInstallSymlinkTwitterBootstrap" ] }, 

One package is the object itself, other packages provide integration and a postinstall handler for the asset.

For Cake, you will need to find the package that performs the installation task for tws/boostrap - perhaps such a package exists in the Cake community.

But I would suggest using something simpler that works out of the box:

 { "require": { "slywalker/boost_cake": "*" } } 

And then use enable CakePlugin::load('BoostCake'); and add the helpers you need.

As @ndm noted: you can also decide to work with Composer β€œbridges” for other asset managers (indirect). One of them is https://github.com/francoispluchino/composer-asset-plugin

Or you can work with these asset managers, for example, with a gazebo, npm.

+1
source

In your composer.json use an array of scripts .

With CakePHP 3.0, at least it is pre-built as follows:

 "scripts": { "post-install-cmd": "App\\Console\\Installer::postInstall", "post-autoload-dump": "Cake\\Composer\\Installer\\PluginInstaller::postAutoloadDump" }, 

post-install-cmd script calls the postInstall function from /src/Console/Installer.php .

If you look at this function, you will see that after installation it performs several actions, for example, setting the rights to a file.

Here you can do everything you need, for example, create a function to copy files from the /vendor/ folder to a web directory folder using standard PHP functions such as copy or rename (for moving).

0
source

Source: https://habr.com/ru/post/984607/


All Articles