Home

Awesome

AssetMix plugin for CakePHP

Latest Stable Version Total Downloads License CakePHP Tests PHPStan Check Coding Style Check

Provides integration with your CakePHP application & Laravel Mix.

This branch works with CakePHP 4.0+, see version map for more details.

❤️ Support The Development

Do you like this project? Support it by donating:

<a href="https://www.buymeacoffee.com/ishanvyas" target="_blank"> <img src="https://www.buymeacoffee.com/assets/img/custom_images/purple_img.png" alt="Buy Me A Coffee" style="height: 41px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" > </a> <a href="https://www.patreon.com/ishanvyas"> <img src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" width="160"> </a>

or Paypal me

or Contact me on Codementor

Follow me

Installation

  1. Install the AssetMix plugin with composer:

    Via composer:

    composer require ishanvyas22/asset-mix
    
  2. Load plugin using below command:

    bin/cake plugin load AssetMix
    
  3. Generate basic Javascript, CSS & Sass scaffolding:

    bin/cake asset_mix generate
    

    Note: Above command will generate scaffolding for vue, but you can generate Bootstrap/jQuery, React or Inertia scaffolding too.

  4. Install frontend dependencies

    npm install
    

    or

    yarn install
    
  5. Compile your assets

    • For development:
    npm run dev
    
    • To watch changes:
    npm run watch
    
    • For production:
    npm run prod
    
  6. Load AssetMix helper from the plugin into your AppView.php file:

    $this->loadHelper('AssetMix.AssetMix');
    

Usage

After compiling your assets(js, css) with laravel mix, it creates a mix-manifest.json file into your webroot directory which contains information to map the files.

echo $this->AssetMix->script('app');

Above code will render:

<script src="/js/app.js" defer="defer"></script>

As you can see it works same as HtmlHelper. There is not need to provide full path or even file extension.

echo $this->AssetMix->css('main');

Output:

<link rel="stylesheet" href="/css/main.css">

If versioning is enabled, output will look something like below:

<link rel="stylesheet" href="/css/main.css?id=9c4259d5465e35535a2a">

Generate command

The generate command is used to generate starter code for your Javascript application to get you started developing your frontend.

Get help:

bin/cake asset_mix -h

Generate default scaffolding (with vue):

bin/cake asset_mix generate

Above command will generate:

assets/ directory is where you will store your js, css files which will compile down into your respective webroot/ directory.

Custom directory name:

bin/cake asset_mix generate --dir=resources

You can also use custom directory name instead of default assets directory, above command will create resources directory where you can put your js, css, etc asset files.

Don't want to use Vue.js? Don't worry this plugin doesn't dictate on which Javascript library you should use. This plugin provides ability to quickly generate scaffolding for Vue as well as Bootstrap, and React.

Generate basic Bootstrap/jQuery scaffolding:

bin/cake asset_mix generate bootstrap

Generate React scaffolding:

bin/cake asset_mix generate react

Generate scaffolding for Inertia.js:

# for vue
bin/cake asset_mix generate inertia-vue

# or for react
bin/cake asset_mix generate inertia-react

Generate React scaffolding inside resources directory:

bin/cake asset_mix generate react --dir=resources

Serving CakePHP out of a Subdirectory

Please see docs/ServingFromSubdirectory

Version map

AssetMix versionBranchCakePHP versionPHP minimum version
1.xmaster>=4.0.0>=7.2
0.xcake3>=3.5.0>=5.6

Changelog

Please see CHANGELOG for more information about recent changes.

Reference

To see this plugin into action you can refer to this project, which will provide more insight.

License

The MIT License (MIT). Please see License File for more information.