ป้ายกำกับ: Yii2

Fix Yii2 jQuery Version to 2.1.3 for CompatibilityFix Yii2 jQuery Version to 2.1.3 for Compatibility

In the development of web applications using Yii2, developers often encounter the challenge of maintaining a specific version of jQuery. This is particularly important when working with plugins that are compatible only with a particular version of jQuery, such as version 2.1.3. However, every time Yii2 is updated using the composer update command, it changes the jQuery version to 2.1.4, necessitating manual intervention to revert to the desired version.

Why Use jQuery Version 2.1.3?

Version 2.1.3 of jQuery is chosen due to its compatibility with specific plugins that are not supported in newer versions. Ensuring that the jQuery version remains consistent prevents potential issues related to plugin functionality and application stability.

How to Fix jQuery Version in Yii2

  1. Modify composer.json: Add the following line to the ‘require’ section of your composer.json file to specify the jQuery version.

    PHP

    "require": {<br>    "components/jquery": "2.1.3"<br>}
    
  2. Run Composer Update: Execute the command composer update in your terminal. This will download jQuery version 2.1.3 and store it in the vendorcomponentsjquery directory.
  3. Configure Yii2 to Use the Specified jQuery Version: Modify the main.php configuration file to direct Yii2 to use the specified jQuery version.

    PHP

    "components" => [<br>    "assetManager" => [<br>        "bundles" => [<br>            "yiiwebJqueryAsset" => [<br>                "baseUrl" => "@web",<br>                "js" => [<br>                    "../../vendor/components/jquery/jquery.min.js",<br>                ],<br>                "sourcePath" => null<br>            ],<br>        ],<br>    ],<br>]
    

After these changes, the script link in your application will change from

XML

XML

<script src="/YiiAdvanced/frontend/web/assets/41df07c8/jquery.js"></script>

to

XML

XML

<script src="/YiiAdvanced/frontend/web/../../vendor/components/jquery/jquery.min.js"></script>

With these modifications, updating Yii2 will no longer change the jQuery version, ensuring that your application remains stable and compatible with the required plugins.

Conclusion

By following the steps outlined above, developers can easily fix the jQuery version in Yii2, ensuring compatibility with specific plugins and maintaining application stability. This approach is crucial for projects that rely on older versions of jQuery for functionality and performance reasons.