官术网_书友最值得收藏!

Custom bundles versus AppBundle

When we use AppBundle as a code base, the app/ directory of our project can be seen as part of AppBundle. Sure, it has other files and folders that take care of other bundles available in the /vendor directory, for example, but we can benefit a lot from the app/ folder.

For example, if you look at the MyBundle/Resources folder, you will find two subfolders named Resources/config/ and Resources/views/, which hold service definitions (and other required settings in the future) and template files for that bundle.

However, with AppBundle, we already have a folder named app/, so conveniently, we can use the available app/config for our configuration needs and app/Resources/views for our templates. Using this approach, referencing these files are much easier.

Compare the render() method in indexAction() of each controller. In the AppBundle controller, we simply referenced the template file without mentioning the name of the bundle. When there is no bundle name, Symfony by default looks in the app/Resources/views directory to find the required template:

return $this->render('default/index.html.twig');

To check the contents of the app/Resources/views directory use the following command:

$ tree app/Resources/views/
app/Resources/views/
├── base.html.twig
└── default
 └── index.html.twig

In MyBundle, we have to mention the bundle name in the reference:

return $this->render('MyBundle:Default:index.html.twig');

Note

Did you notice the tests/ folder that Symfony created in the root of the project? We can use it to write tests for AppBundle.

We got two elements of MVC so far: Controller and View. Let's have a look at the big picture that we have now:

Custom bundles versus AppBundle
主站蜘蛛池模板: 家居| 阿城市| 漾濞| 湟中县| 武山县| 海晏县| 青阳县| 柯坪县| 漠河县| 瓦房店市| 大庆市| 易门县| 洞口县| 太湖县| 沈阳市| 九龙坡区| 盐城市| 新源县| 平阳县| 韶山市| 大关县| 漳州市| 崇义县| 志丹县| 佛学| 河北区| 峨边| 疏勒县| 东方市| 广元市| 宝坻区| 康乐县| 靖远县| 铜陵市| 托克逊县| 华蓥市| 中山市| 昌平区| 龙南县| 远安县| 会理县|