Creating Bundles in Symfony
Bundle allows to group us several features to a single unit. It works like a module or plugin. Your Symfony site is composed of many bundles to make up your final application. Everything comes in bundles in Symfony from third party components and the one you develop. Even Symfony library code comes in a bundle. Our bundle can be used by multiple application or even distribute it to other developers. Individual entities related to specific functions are packaged in a bundle (thats where it gets its name from). It is somewhat similar to package in Java language but somewhat different. Each bundle has its own configuration, routes, controllers, templates and other as needed. Bundles are generally stored insrc/
folder. It can however be stored anywhere as long as you can address it.Creating Bundle
We can create bundle by making necessary directory structure, files and other structures, but it can be easily generated in command line with a single command. First change your directory to the directory you installed the framework. For an example if you installed at SchoolManagement.
$ cd Schoolmanagement
Run Following command
$ php app/console generate:bundle --namespace=Stm/SchoolManagementBundle --format=yml
The above command generates bundle with
vendor name = Stm
and bundle name = SchoolManagement
with configuration format in yml
format. yml format is easy to use with cleaner body compared to xml or php configurations.
The command asks for some options for creating bundles
Bundle name [StmSchoolManagementBundle]:
Target directory [/Users/user1/htdocs/SchoolManagement/src]:
Do you want to generate the whole directory structure [no]?
Do you confirm generation [yes]?
Confirm automatic update of your Kernel [yes]?
Confirm automatic update of the Routing [yes]?
Just hit enter to select default options or chose option as you wish. If you select yes at third question, Symfony will automatically generate additional code snippets so that you can start faster. But if we like our custom program design, we can write from now on. Inside SchoolManagement/src, new folder with our vendor name is generated, containing, folders like Controller, resources etc. The generator also automatically registers our bundle in registerBundles() method of app/AppKernel.php with
new Stm\SchoolManagementBundle\StmSchoolManagementBundle()
To remove bundle, unregister the bundle by just deleting this line. It also includes
SchoolManagemntBundle
routing file in src/Stm/SchoolManagement/Resources/config/routing.yml
to main routing file in app/config/routing.yml
as
//app/config/routing.yml
stm_school_management:
resource: "@StmSchoolManagementBundle/Resources/config/routing.yml"
prefix: /
//src/Stm/SchoolManagement/Resources/config/routing.yml
stm_school_management_homepage:
path: /hello/{name}
defaults: { _controller: StmSchoolManagementBundle:default:index }
No comments:
Post a Comment