In example below, we're going to create a symfony vendor bundle so that we can install it with composer to use it in our main project.


Setup the vendor bundle


Install it


Inanzzz-MBP:Documents inanzzz$ composer create-project symfony/framework-standard-edition say-hello

Create a bundle


Inanzzz-MacBook-Pro:say-hello inanzzz$ php app/console generate:bundle --namespace=Inanzzz/HelloBundle

Clean it up


After removing certain files and directories from the application, you should end up with structure below.


say-hello
src
Inanzzz
HelloBundle
DependencyInjection
Configuration.php
InanzzzHelloExtension.php
InanzzzHelloBundle.php
.htaccess
.gitignore
composer.json
LICENSE
README.md

Update composer.json


Replace the current content with the one below and run composer update.


{
"name": "inanzzz/sayhello",
"license": "MIT",
"type": "library",
"description": "Say Hello bundle",
"autoload": {
"psr-0": {
"Inanzzz\\HelloBundle": "src/"
}
},
"require": {
"php": ">=5.3.3",
"symfony/symfony": "2.6.*"
},
"minimum-stability": "stable"
}

Update .gitignore


/vendor/
/composer.phar
/composer.lock

Create Hello.php


# say-hello/src/Inanzzz/HelloBundle/Util/Hello.php

namespace Inanzzz\HelloBundle\Util;

class Hello
{
public function name($value)
{
return 'Hello '.$value.'!';
}
}

Define Hello.php service


# say-hello/src/Inanzzz/HelloBundle/Resources/config/services.yml

services:
inanzzz_hello.util.hello: #inanzzz_hello must match DependencyInjection/Configuration.php line $treeBuilder->root('xxxxxxxx');
class: Inanzzz\HelloBundle\Util\Hello

Current structure


We should now have structure below.


say-hello
src
Inanzzz
HelloBundle
DependencyInjection
Configuration.php
InanzzzHelloExtension.php
Resources
config
service.yml
Util
Hello.php
InanzzzHelloBundle.php
.htaccess
.gitignore
composer.json
LICENSE
README.md

Setup remote Git repo and push


First of all, create a remote Git repo named as say-hello then follow the steps below to initiate it so that you can start developing it. Note: You should actually do this before all the steps above.


Inanzzz-MBP:say-hello inanzzz$ git init
Inanzzz-MBP:say-hello inanzzz$ git add --all
Inanzzz-MBP:say-hello inanzzz$ git commit -m 'First commit'
Inanzzz-MBP:say-hello inanzzz$ git remote add origin git@github.com:Inanzzz/say-hello.git
Inanzzz-MBP:say-hello inanzzz$ git push -u origin master

Main application


Update composer.json


Add parts below to current content and run composer update inanzzz/sayhello. As you can see, the name of remote Git repo say-hello doesn't have to match the name of composer.json name sayhello that we created above.


{
...
"repositories": [
{
"type": "git",
"url": "git@github.com:Inanzzz/say-hello.git"
},
...
],
"require": {
...
"inanzzz/sayhello": "dev-master"
...
},
"minimum-stability": "stable"
...
}

If you want you can change repositories block with the one below.


{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/Inanzzz/say-hello.git"
}
}

If you want to get rid of repositories block then you need to submit your new package/bundle to Packagist. Everytime you update your bundle, you must login and update the Packagist by "Force Update" button or setup an "auto update" task.


Update AppKernel.php


After registering the bundle, you can access its service with inanzzz_hello.util.hello service name.


new Inanzzz\HelloBundle\InanzzzHelloBundle(),