Bundles and Controllers - Puzzlout/DevGuidelines GitHub Wiki
Bundles
For bundles command is php bin/console generate:bundle
, as you noticed by now we will run bin/console commands often, they are really great.
When running the command you will be prompted to state if bundle is shared or not, shared bundles are bundles which are to be used with multiple apps, like one in vendor directory and they should be prefixed with vendor name, I guess in our case Puzzlout/, name should be Puzzlout/NameBundle or just NameBundle if bundle isn't shared, i.e ForumBundle. After that we should just state that we want to create bundle in default /src directory and that we want to use annotations as config format.
Skeleton will be created, for example /src/ForumBundle with ForumBundle.php inside or in shared bundle case /src/Puzzlout/ForumBundle with PuzzloutForumBundle.php. If you open it up you'll see namespace set relative to the src directory class name is in VendorNameBundle format and it extends base Bundle class. Now open up /app/config/routing.yml which is our main router config file. You'll see that we got new key under routes named vendor_name: (puzzlout_forum:, or forum:), below it should be resource which will contain actual routes, if we chose annotation it should be resource:"@ForumBundle/Controller", and after that prefix to the route we want, by default is '/'. That means that all routes stated in annotations in our controllers will be included by default.
Last thing is our app/AppKernel.php file, it contains registerBundles()
method. In $bundles
array, there should be our newly created bundle.
So this said, both controllers and bundles can be created manually. For controllers we only need to create controller file in Controller directory, setup namespace, include base classes we need and extend Controller.
For bundles we must create directory skeleton and VendorNameBundle.php file which also contains proper namespace and extends Bundle class. After that register it in app/AppKernel.php and include routes in app/config/routing.yml
Controllers
OK for Controllers is pretty straightforward, use this command php bin/console generate:controller
It will guide you through interactive controller generation. You will first enter the name of the controller in this format BundleName:ControllerName, i.e. PuzzloutFooBundle:Product, or AppBundle:Product for default AppBundle. Other stuff it will ask you for is routing config format, we will go with annotations, template format I guess we will use twig and actions you want to add, you can skip actions since we will add them later on.
That's it, new controller will be created in Controller directory with proper namespace and with Controller base class included and with Route annotation class if we chose annotations as our route config format.
Now you can see how controller looks like in Controller directory in our Puzzlout/FooBundle/Controller directory.
It extends base Controller class and it's named like NameController. Actions are named like NameAction.
If you want to specify routes there like we should we use @Route("/path")
annotation before each action, you can see one in DefaultController in our AppBundle.
References
- http://symfony.com/doc/current/book/controller.html - Symfony Controller
- http://symfony.com/doc/current/best_practices/controllers.html - Controllers best practices
- http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html - Controller annotation routing
- http://symfony.com/doc/current/cookbook/bundles/best_practices.html - Bundles structure and best practices
- http://symfony.com/doc/current/bundles/SensioGeneratorBundle/commands/generate_bundle.html - Bundle generation
- http://symfony.com/doc/current/bundles/SensioGeneratorBundle/commands/generate_controller.html - Controller generation