2

I have jQuery installed with composer.

Now jQuery is in vendor/components/jquery directory.

I've tried to include that in /app/config/config.yml

assetic:
    debug:          "%kernel.debug%"
    use_controller: false
    filters:
        cssrewrite: ~
    assets:
        jquery: %kernel.root_dir%/../vendor/components/jquery/jquery.min.js

If I run the command:

php app/console assetic:dump

I get

[RuntimeException]                                                                                                              
  The source file "/home/user/project/app/current/app/../web/vendor/components/jquery/jquery.min.js" does not exist.

Cause assetic is still looking in the /web/ directory which is wrong. How can I change it so it will look in the /vendor/ directory? Also I don't want to put the jquery files in the public bundle folder cause that would brake all the sense of getting the right versions of vendor libraries via composer.

caramba
  • 21,963
  • 19
  • 86
  • 127
  • 2
    It's probably worth using the google cdn for jquery as most users will have already cached it: https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js – Roy Jul 29 '16 at 09:04
  • 1
    @user1 thank you. But no, imagine what happens if google is offline, my webApp would then also be broken just because of that ... – caramba Jul 29 '16 at 09:08
  • it's pretty unlikely – Ziumin Jul 29 '16 at 09:11
  • The major CDNs are extremely reliable. In fact, they've had zero downtime in the last 3 months: https://www.cdnperf.com/#jsdelivr,cdnjs,google,yandex,microsoft,jquery,bootstrapcdn/https/90. – Rory McCrossan Jul 29 '16 at 09:13
  • Ok ok, guys, that was just a joke. I don't want to include it over google. Imagine if the App should run only inside a company and can not connect to the outside world. – caramba Jul 29 '16 at 09:15
  • I guess you can configure where Composer should install the components? Check this http://stackoverflow.com/questions/21998144/composer-in-symfony2-creates-the-same-assets-twice-jquery-jqueryui – dlondero Jul 29 '16 at 09:24
  • Thank you @dlondero, I've tried that but nothing changed. Removed cache, installed composer, assets and dumped assets multiple times, but no jquery in `/web/` dir – caramba Jul 29 '16 at 09:36
  • I suggest you to read [this answer about the different ways to include jQuery](http://stackoverflow.com/a/41324944/1941316). – Kwadz Jan 14 '17 at 11:27

2 Answers2

8

I'm not a big fan of installing jquery in the vendor directory in Symfony, as it's not a PHP library. Composer and vendor directory should only be used for PHP dependencies. You should separates the frontend part from the backend part. All the js, css and other assets should go somewhere else.

Therefore you have 2 options:

  1. If you want to have separate resources per bundle, then put your resources you use for the frontend in the public directory of your bundle: for example put your jquery.min.js in the Resources/public/js folder of your bundle.

Then run the command php app/console assets:install. this will copy your js files to the web/bundles/yourbundle/js/ directory.

Finally use <script type="text/javascript" src="{{ asset('yourbundle/js/jquery.min.js') }}"></script> in your twig file to add jquery.

  1. If you have common resources for all your bundles, put your resources directly in the web folder, for example: web/js, web/css, web/img... And then use <script type="text/javascript" src="{{ asset('js/jquery.min.js') }}"></script> in your twig template.
Youri_G
  • 219
  • 2
  • 9
  • 1
    +1 and thank you @Youring_G! I didn't know about "Composer and vendor directory should only be used for PHP dependencies." To separate front- and backend makes sense. I still like the idea that it could be possible to update those files via `composer` – caramba Jul 29 '16 at 09:41
1

While following the links from comments and retrying other answers it was pretty easy. Everything missing was

composer require robloach/component-installer

more information about robloach/component-installer

Then add this line manually to composer.json

"config": {
    #...
    "component-dir": "web/assets",
    #...
},

Assumed that you already have

composer require components/jquery

You might have to rerun the command

composer install

Now it will create a directory /web/assets/jquery with all the files which will be easy to include.

caramba
  • 21,963
  • 19
  • 86
  • 127
  • **Component Installer has been deprecated** – Jonathan Aug 23 '21 at 10:25
  • @Jonathan indeed true that robloach/component-installer is deprecated, however there seems to be no good alternative. I've tried a few and have decided the above, despite being deprecated, is the best approach. – AQuirky Jul 27 '22 at 23:17
  • Never, simply **never** rely on deprecated, outdated, untested or unstable software. – Jonathan Jul 28 '22 at 11:50