Question

I'm just getting started with Laravel / Composer. I want jquery to load with the composer autoloader.

My composer.json file looks like this:

{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"require": {
    "laravel/framework": "4.1.*",
    "frameworks/jquery": "1.*"
},
"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php"
    ],
    "files":[
        "vendor/frameworks/jquery/jquery.min.js"
    ]
},
"scripts": {
    "post-install-cmd": [
        "php artisan clear-compiled",
        "php artisan optimize"
    ],
    "post-update-cmd": [
        "php artisan clear-compiled",
        "php artisan optimize"
    ],
    "post-create-project-cmd": [
        "php artisan key:generate"
    ]
},
"config": {
    "preferred-install": "dist"
},
"minimum-stability": "stable"

}

notice i've added jquery to the require list and have added the jquery.min.js to the autoload / files. It loads, but as plain text, not as a js file.

This has to be something stupid I'm doing, but I can't figure it out. How do I get the file to load properly?

Was it helpful?

Solution

You cannot autoload jQuery with composer, because that would mean you want to execute Javascript on the server with the PHP script engine. This will never work.

What you want to do is include jQuery in the page you are creating with PHP. Probably include it as a javascript tag that loads it. Which means you have to configure a way to make that jquery.min.js file available on the server for HTTP access.

The frameworks/jquery package does not have any suggestions on how to do this. It simply grabs the files and adds it to the vendor folder. If you want it made available to the browser you'd either copy it into the document root, setup a script that reads the file and sends it to the browser, set a symlink to it, or add an alias to that vendor directory. All of these solutions are not that nice, but if asked I'd prefer the copy step.

There is a components/jquery package that uses another package to allow easy configuration of where to drop the files. You should investigate that, read the README. It looks much nicer to maintain and will essentially do the copy step.

OTHER TIPS

I might be wrong but composer is used to autoload php files. It allows you to call something with out including it. This is all on the server side. I don't think you can do what you are trying to do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top