Using Composer in CakePHP

This article is over a year old and may contain outdated information.

Composer, a magnificent dependency manager. CakePHP, a brilliant MVC framework. What's stopping you from using both in your application? Nothing at all! Personally, I have been using Composer exclusively in my applications to handle all my dependencies and even my CakePHP plugins. It's very easy to do.

The first thing you need to do is create a composer.json file within your application. It's best to place the file in app/composer.json as it would share the same directory structure with Vendor. Here's a quick example:

{
	"config": {
		"vendor-dir": "Vendor"
	},
	"require": {
		"mjohnson/uploader": "4.*",
		"mjohnson/decoda": "6.*"
	}
}

Since this is an application and not a dependency, all you need to define is the "require" property. We also set the vendor folder to use CakePHP's naming convention, because why not?

To benefit from Composer's autoloader, include the autoload.php file at the very top of Config/core.php.

require_once dirname(__DIR__) . '/Vendor/autoload.php';

Brilliant! Now we have full dependency and autoloading support. Can't get easier than that.

23 Comments

  • Problem 1
    - Installation request for mjohnson/forum 4.* -> satisfiable by mjohnson/forum[4.0.0].
    - mjohnson/forum 4.0.0 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.


    I have exactly the same error, no matter what plugin I try to install with composer.

    Any help ?
    PY Lochard
  • I get an error when i want to use Composer to get dependencies with your Forum plugin :

    "
    > Console\cake composer.c update

    Loading composer repositories with package information
    Updating dependencies (including require-dev)
    Your requirements could not be resolved to an installable set of packages.

    Problem 1
    - Installation request for mjohnson/forum 4.* -> satisfiable by mjohnson/forum[4.0.0].
    - mjohnson/forum 4.0.0 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.

    "

    However this extension is installed and enabled on my server, that's why I need some help.

    Thanks!
  • The problem with composer is that it does not support recursive repository load https://github.com/composer/composer/blob/master/doc/faqs/why-can't-composer-load-repositories-recursively.md
  • I am getting my head around the composer. It is cool tool and I agree it should be used.

    What I do not understand is how you want to recursively load repositories as it is not possible http://goo.gl/EUPjW

    Let me tell you example.

    Creating cakephp application which will use your forum plugin. So getting clean copy of cakephp, then in ROOT, or APP I would create composer.json which would refere your forum plugin
    
    {
    	"name": "lubos/supre-app",
    	"type": "package",
    	"require": {
    		"php": ">=5.3.0",
    		"mjohnson/forum": "*",
    		"composer/installers": "*"
    	}
    }
    

    running composer install I would expect the forum plugin is installed as welll as admin and utility plugin (http://goo.gl/w7znY). the forum is installed - good, but the required plugins admin and utility ARE NOT.

    Any idea how to solve this problem since you decided to use comopser only?

    Thanks in advance
  • @Miles Johnson.

    You said that by manually installing the files can avoid composer But where can i get these files.

    And where to install, will you please tell me clearly.

    I dont want to use composer but i want to use latest 4.0 version.
    pavan kumar
  • Same error :

    Error: Class 'Transit\Transit' not found
    File: C:\xampp\htdocs\upload\Plugin\Uploader\Model\Behavior\AttachmentBehavior.php
    Line: 225

    Please, help
  • @Deepak - These instructions can't get any easier, they just require you to understand Composer. You simply need to go read the how to on Composer.
  • Hi All,

    I believe Miles have done an excellent job of creating these plugins and other script to help fellow coders.

    But I also believe the documentation (especially installation instruction) does confuse a lot of beginners like.

    I think we should help Miles to write a beginner level tutorials of how to use all the pieces together.

    I am still struggling with lots of issue regarding the installation otherwise I would have written something on this.

    What do you think?
    Deepak Kumar ⋅
  • I've been trying to upload images to my db, I've been trying your codes for the past 3 days still having the same error. I am using cakephp 2.3

    Error: Class 'Transit' not found
    File: E:\websites\cake230\bgsite\Plugin\Uploader\Model\Behavior\AttachmentBehavior.php
    Line: 225

    please which folder or file should I paste
    {
    "config": {
    "vendor-dir": "Vendor"
    },
    "require": {
    "mjohnson/uploader": "4.*"
    }
    }

    please this is my skype id: simmysoft
    Henry Opoku ⋅
  • @Redd - Interesting stuff, but that doesn't really solve the problem when people can't access the command line.
  • The simplest way to enable Composer in CakePHP project:
    https://github.com/uzyn/cakephp-composer

    As in "README" states: "There is no need to pre-install Composer." and it's true - this plugin do all the hard work.
  • You can include the autoload file also with:
    
    App::import('Vendor', array('file' => 'autoload'));
    
  • Hi Miles, thanks for the reply. I haven't a clue how to do that to be honest. How close would I be if I dl'd version 3.6 and followed the instructions for the latest version but ignored the composer bit?

    I've been trying to get image uploads working for 3 full days now with no success for one reason or another so I just need a quick solution that works. I'm a bit new and I reckon I've bitten off more than I can chew.
  • @David - View the changelog for differences. The docs only represent the latest version but aren't too drastically different. You would have to comb over the source code for old settings, etc.

    http://milesj.me/code/cakephp/uploader/logs
  • This looks like it would have been handy but the composer thing is a problem for me too. You told Lucho to download a previous version so I did but there aren't any instructions. It just points you to the main page with the most up-to-date instructions

    what's different between installing 3.6 and 4.0.4?

    thanks for the efforts by the way, I hope I can get this working
  • @Eldad - Not all dependencies will be installed to the Vendor folder; thats just the default location. The CakePHP plugins use composer/installers to install into the Plugin folder.

    https://github.com/composer/installers
  • I think that I'm missing something..
    Can you please explain how I suppose to add Plugins to my project?

    For instance, I have a project that's using your Forum plugin.
    If I'll add the Forum plugin to the project composer.json - the Forum will be located in the Vendor folder, while Cake will look for it in the Plugin folder.

    Please advise,
    thanks!
    Eldad Yamin ⋅
  • @Chirayu & pmi - All your questions can be answered by reading up on Composer: http://getcomposer.org/doc/00-intro.md
  • I need your help.
    I do not understand how to install the plugin.
    Can you explain more about this new procedure?
    Thank you very much
  • from where to get the autoload.php?
    and is there any working script which can guide which file contains what? like what and how to write in controller? your real documentations users two different words "plugin" and "vendor". I am confused between.

    Help is appreciated as I am very hopeful about using this script in my project.
    Chirayu
  • @Lucho Molina - Sorry but you will have to use older versions, or manually install all dependencies and autoloading. Newer projects have many dependencies, for example the Uploader now has Transit and AWS as a dependency, so using Composer is the easiest approach.
  • So, in order to use your Uploader plugin, I must do all this? I've been using the plugin for quite a while now in several projects (congrats btw), and today I found out about this change. I think it's a bit unnecessary, I mean, what was wrong with the old way?

    For this particular project I'm working on right now, I don't have access to the console, so I can't install the Composer. What can I do?
    Lucho Molina ⋅