简体   繁体   中英

How to use JQuery with php via composer?

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": "doe@example.com"
        }
    ]
}

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM