Using Composer for CakePHP and dependencies: overall strategy, project structure and `.gitignore`

I would like my CakePHP project to use best practices. Currently we have our PHP dependencies checked into our project's repo, and i don't like that.

I want the project to leverage Composer, but i don't understand the proper strategy.

First, there is the official CakePHP repo:

Then, there's an app-template boilerplate from FriendsOfCake:

Lastly, there's cakephp-composer, an active project. It implies that:

cakephp-composer is the weirdest thing. How am i supposed to fetch CakePHP and cakephp-composer after cloning my project?

I want to achieve the following:

  1. My project repo contains only source code of the project itself. Any code that is versioned somewhere else, e. g. CakePHP framework and CakePHP plugins, should not appear in my repo.
  2. After cloning my project's repo to a new location, i want to fetch all PHP dependencies, including CakePHP and its plugins, with a single Composer command. No manual downloading/cloning. The project should start working right after Composer finishes its job.
  3. I really like the app-template boilerplate from FriendsOfCake. It looks simple and reasonable to me. So i wish my project structure were as close to app-template as possible.

I don't understand and request explanations for the following things:

  1. Where is the folder for Composer to fetch dependencies into configured? Composer docs mention "PSR-4" and i (being a frontend dev coming from the Ruby world) don't have a slightest idea what that is.
  2. What folders should be put under .gitignore?
  3. Do i really need cakephp-composer? I hope that i don't!
  4. How do i configure composer.json to fetch plugins with Composer, especially if i don't use cakephp-composer? It might be as simple as just mentioning package names under require, but how do i tell Composer whether plugins should go under app/Vendor/ and app/Plugin/? Oh, and where should they go?
  5. How do i properly include CakePHP and CakePHP plugins fetched by Composer into my project?

For an example CakePHP plugin to be fetched by Composer, take haml. It's what i need for my frontend work and it has its own Composer dependency.

Upvotes: 3

Views: 3372

Answers (1)

joshua.paling
joshua.paling

Reputation: 13952

OK, there's several questions there... I'll try to answer a few.

First and foremost, you should be clear on wether you want to use CakePHP 2.X, or CakePHP 3.0. I assume since you're talking about an existing project, you mean Cake 2.X - which wasn't built with composer in mind. Cake 3.0 was built with composer in mind, so if upgrading to that is an option, I assume it'll be nicer (I haven't used 3.0 yet myself)

Here's a simplified example composer.json file from one of my projects:

{
    "name": "my-project",
    "require": {
        "cakedc/migrations": "2.2.2",
        "cakephp/debug_kit": "2.2.1",
    },
    "require-dev": {
        "phpunit/phpunit": "3.7.*"
    },
    "config": {
        "vendor-dir": "app/Vendor/"
    },
    "extra": {
       "installer-paths": {
         "app/Plugin/Migrations": ["cakedc/migrations"],
         "app/Plugin/DebugKit": ["cakephp/debug_kit"],
       }
     }

}
  1. see the line "vendor-dir": "app/Vendor/" - that's where I've configured composer to put it's packages, by default, in my app/Vendor/ folder.

  2. What goes in .gitignore... we'll come back to that.

  3. No, you don't need this.

  4. See the line "installer-paths": {? That's where I've configured composer to put specific CakePHP plugins in Cake's plugins dir, as opposed to the default app/Vendor/ which I mentioned in 1.

  5. For CakePHP Plugins - see 4. For CakePHP itself, I haven't personally done it. I did try at one point and found the setup to be not playing nicely with one of my plugins, so I gave up on it. However, you should be able to use the same principals as above.

Now, coming back to 2. what should be in your .gitignore? Anything that has contents completely maintained by composer - in my case, that's app/Vendor/, and app/Plugin/. That assumes that ALL the contents of those folders is maintained by composer. If you had eg. one plugin that you'd added manually, then you couldn't ignore the whole folder - you'd have to ignore only the specific plugins installed with composer.

PS - I personally delete the root vendor/ and plugins/ folders - I don't use them at all.

Upvotes: 4

Related Questions