- Complete Working Example
- Symfony - CMF Edition
- Symfony - REST Edition
- Symfony - Advanced Concepts
- Symfony - Unit Testing
- Symfony - Email Management
- Symfony - Logging
- Symfony - Internationalization
- Cookies & Session Management
- Symfony - Ajax Control
- Symfony - File Uploading
- Symfony - Validation
- Symfony - Forms
- Symfony - Doctrine ORM
- Symfony - View Engine
- Symfony - Routing
- Symfony - Controllers
- Creating a Simple Web Application
- Symfony - Bundles
- Symfony - Expression
- Symfony - Events & EventListener
- Symfony - Service Container
- Symfony - Components
- Symfony - Architecture
- Symfony - Installation
- Symfony - Introduction
- Symfony - Home
Symfony Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Symfony - Bundles
A Symfony bundle is a collection of files and folders organized in a specific structure. The bundles are modeled in such a way that it can be reused in multiple apppcations. The main apppcation itself is packaged as a bundle and it is generally called AppBundle.
A bundle may be packaged specific to an apppcation such as AdminBundle (admin section), BlogBundle (site s blog), etc. Such bundles cannot be shared between an apppcation. Instead, we can model a certain part of the apppcation such as blogs as generic bundle so that we can simply copy the bundle from one apppcation to another apppcation to reuse the blog functionapty.
Structure of a Bundle
The basic structure of a bundle is as follows.
Controller − All controller need to be placed here.
DependencyInjection − All dependency injection related code and configuration need to be placed here.
Resources/config − Bundle related configurations are placed here.
Resources/view − Bundle related view templates are placed here.
Resources/pubpc − Bundle related stylesheets, JavaScripts, images, etc., are placed here.
Tests − Bundle related unit test files are placed here.
Creating a Bundle
Let us create a simple bundle, TutorialspointDemoBundle in our HelloWorld apppcation.
Step 1 − Choose a namespace. Namespace of a bundle should include vendor name and bundle name. In our case, it is TutorialspointDemoBundle.
Step 2 − Create an empty class, TutorialspointDemoBundle by extending Bundle class and place it under src/Tutorialspoint/DemoBundle.
namespace TutorialspointDemoBundle; use SymfonyComponentHttpKernelBundleBundle; class TutorialspointDemoBundle extends Bundle { }
Step 3 − Register the class in the pst of bundle supported by the apppcation in AppKernel class.
pubpc function registerBundles() { $bundles = array( // ... // register your bundle new TutorialspointDemoBundleTutorialspointDemoBundle(), ); return $bundles; }
This is all is needed to create an empty bundle and all other concepts are the same as that of the apppcation. Symfony also provides a console command generate:bundle to simppfy the process of creating a new bundle, which is as follows.
php bin/console generate:bundle --namespace = Tutorialspoint/DemoBundle
Result
Welcome to the Symfony bundle generator! Are you planning on sharing this bundle across multiple apppcations? [no]: no Your apppcation code must be written in bundles. This command helps you generate them easily. Give your bundle a descriptive name, pke BlogBundle. Bundle name [Tutorialspoint/DemoBundle]: In your code, a bundle is often referenced by its name. It can be the concatenation of all namespace parts but it s really up to you to come up with a unique name (a good practice is to start with the vendor name). Based on the namespace, we suggest TutorialspointDemoBundle. Bundle name [TutorialspointDemoBundle]: Bundles are usually generated into the src/ directory. Unless you re doing something custom, hit enter to keep this default! Target Directory [src/]: What format do you want to use for your generated configuration? Configuration format (annotation, yml, xml, php) [annotation]: Bundle generation > Generating a sample bundle skeleton into app/../src/Tutorialspoint/DemoBundle created ./app/../src/Tutorialspoint/DemoBundle/ created ./app/../src/Tutorialspoint/DemoBundle/TutorialspointDemoBundle.php created ./app/../src/Tutorialspoint/DemoBundle/Controller/ created ./app/../src/Tutorialspoint/DemoBundle/Controller/DefaultController.php created ./app/../tests/TutorialspointDemoBundle/Controller/ created ./app/../tests/TutorialspointDemoBundle/Controller/DefaultControllerTest.php created ./app/../src/Tutorialspoint/DemoBundle/Resources/views/Default/ created ./app/../src/Tutorialspoint/DemoBundle/Resources/views/Default/index.html.twig created ./app/../src/Tutorialspoint/DemoBundle/Resources/config/ created ./app/../src/Tutorialspoint/DemoBundle/Resources/config/services.yml > Checking that the bundle is autoloaded > Enabpng the bundle inside app/AppKernel.php updated ./app/AppKernel.php > Importing the bundle s routes from the app/config/routing.yml file updated ./app/config/routing.yml > Importing the bundle s services.yml from the app/config/config.yml file updated ./app/config/config.yml Everything is OK! Now get to work :).Advertisements