1

I am a true beginner with composer. I need to maintain an old PHP application with a lot of <script type="text/javascript" src="../../jquery.min.js"></script>. I would like to get rid of JQuery in the code base and rely on a package manager, so I discovered composer.

I wrote a basic composer.json then I naively wrote a test page while expecting to have JQuery loaded in my page.

<?php require_once 'vendor/autoload.php' ?>
<html>
<head>
    <title>Where is my JQuery?</title>
    <?php magic_to_get_jquery() ?> <!-- Is there any magic? -->
</head>
<body>
    <div id="test">Test</div>
</body>
</html>

I don't really understand what I should do to get JQuery in this test page.

Here my composer.json

{
    "name": "test/test",
    "require": {
        "components/jquery": "^3.3",
    },
    "authors": [
        {
            "name": "John Doe",
            "email": "[email protected]"
        }
    ]
}
4
  • It is often better to get JQuery from a CDN rather than store it on you local or live server Commented Jul 5, 2018 at 15:52
  • 1
    I know, but this is an intranet portal. Commented Jul 5, 2018 at 15:53
  • This might help you then Commented Jul 5, 2018 at 15:58
  • @RiggsFolly, not really :( Commented Jul 5, 2018 at 16:03

2 Answers 2

3

You may use scripts to copy jQuery assets to public directory after installing package by composer. Add something like this to your composer.json:

{
    "scripts": {
        "post-install-cmd": [
            "php -r \"copy('vendor/components/jquery/blob/master/jquery.min.js', 'public/assets/jquery.min.js');\""
        ]
    }
}

But you may be interested by using some tools for processing and publishing assets. Googling tools like "gulp" or "webpack" may give you some insight into this topic - you may be surprised how complicated may be using jQuery when you want to use all modern fronted stuff :).

Also PHP frameworks usually have some tools for publishing assets - if you're using one of them you may take a look at documentation, you may find more sophisticated and ready to use solution.

Sign up to request clarification or add additional context in comments.

Comments

1

composer update only download file in this repo: https://github.com/components/jquery

So, I think there's no call function to load the js file, and you need to link it manually if you use composer. Or you can make the function yourself.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.