Tutorial of creating your own Symfony2 bundle
http://symfony.cn/docs/quick_tour/the_big_picture.html
symfony new DemoBundle 2.3
(2.3 is my symfony version)
cd DemoBundle
php app/console generate:bundle
this will ask you to answer questions. For Bundle namespace set to DataSpring/DemoBundle (CompanyName / BundleName).
cd src/DataSpring/DemoBundle
Create a new repository in your github first then run the following command lines:
git init
touch README.md
git add .
git commit -m "initial commit"
git remote add origin [email protected]:YourAccount/DemoBundle.git
git push -u origin master
In the src/DataSpring/DemoBundle/
folder, manually add a composer.json
file.
or run
composer init
to get a simpliest file before changing it.
A composer.json file would look like so:
{
"name": "dataspring/demo-bundle",
"description": "Demo Bundle",
"type": "symfony-bundle",
"keywords": ["dataspring"],
"license": "MIT",
"homepage": "http://github.com/guobinqiu/DemoBundle",
"authors": [
{
"name": "Guobin",
"email": "[email protected]"
}
],
"require": {
"php": "~5.3"
},
"autoload": {
"psr-4": {
"DataSpring\\DemoBundle\\": ""
}
},
"extra": {
"branch-alias": {
"dev-master": "0.1.x-dev"
}
}
}
In the src/DataSpring/DemoBundle/
folder, add a LICENSE
file
Your can borrow from the above licence reference as your own license content. Also see LICENSE
Controller Code:
<?php
namespace Dataspring\DemoBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class DefaultController extends Controller
{
public function indexAction()
{
return $this->render('DataSpringDemoBundle:Default:index.html.twig');
}
}
In the src/Resources/config/routing.yml
to add:
demo_index:
path: /index
defaults: { _controller: DataSpringDemoBundle:Default:index }
Notice that when creating your own bundle, Annotation
way is not supported!
git tag -a VERSION -m "MESSAGE"
git push origin VERSION
-
Install GitHub Service Hook to ensure that your package will always be updated instantly when you push to GitHub
{
...,
"require": {
...,
"dataspring/demo-bundle": "^0.1.4"
}
}
php composer.phar update dataspring/demo-bundle
or update all dependencies
php composer.phar update
or you can do this in one command:
php composer.phar require dataspring/demo-bundle
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
...,
new DataSpring\DemoBundle\DataSpringDemoBundle()
);
...
}
}
data_spring_demo:
resource: "@DataSpringDemoBundle/Resources/config/routing.yml"
prefix: /demo
Typing http://YourDomain:port/demo/index
in your web browser to access!