This tutorial is built using Symfony 4, but most of the concepts apply fine to Symfony 5!
// composer.json
{
"require": {
"php": "^7.1.3",
"ext-iconv": "*",
"doctrine/annotations": "^1.8", // v1.8.0
"knplabs/knp-markdown-bundle": "^1.7", // 1.7.0
"knpuniversity/lorem-ipsum-bundle": "*@dev", // dev-master
"nexylan/slack-bundle": "^2.0,<2.2", // v2.0.1
"php-http/guzzle6-adapter": "^1.1", // v1.1.1
"sensio/framework-extra-bundle": "^5.1", // v5.1.6
"symfony/asset": "^4.0", // v4.0.6
"symfony/console": "^4.0", // v4.0.6
"symfony/flex": "^1.0", // v1.21.6
"symfony/framework-bundle": "^4.0", // v4.0.6
"symfony/lts": "^4@dev", // dev-master
"symfony/twig-bundle": "^4.0", // v4.0.6
"symfony/web-server-bundle": "^4.0", // v4.0.6
"symfony/yaml": "^4.0", // v4.0.6
"weaverryan_test/lorem-ipsum-bundle": "^1.0" // v1.0.0
},
"require-dev": {
"easycorp/easy-log-handler": "^1.0.2", // v1.0.4
"sensiolabs/security-checker": "^4.1", // v4.1.8
"symfony/debug-bundle": "^3.3|^4.0", // v4.0.6
"symfony/dotenv": "^4.0", // v4.0.6
"symfony/maker-bundle": "^1.0", // v1.1.1
"symfony/monolog-bundle": "^3.0", // v3.2.0
"symfony/phpunit-bridge": "^3.3|^4.0", // v4.3.3
"symfony/stopwatch": "^3.3|^4.0", // v4.0.6
"symfony/var-dumper": "^3.3|^4.0", // v4.0.6
"symfony/web-profiler-bundle": "^3.3|^4.0" // v4.0.6
}
}
38 Comments
First of all, thanks for this tutorial. Super clear, although when doing it using symfony 5 I always get stuck on this issue:
<blockquote>{{my_route}} has no container set, did you forget to define it as a service subscriber</blockquote>.
Anybody having the same issue?
Edited
Found the issue,
needed to add
<defaults autowire="true" autoconfigure="true"/>on your service.xml file to enable auto wiring.I think that autowiring in the bundle is explicitly discouraged. Another way around this is to add `<call>` to each handler with method `setContainer` (which is admittedly a pain). Tbh, idk how that works exactly (are you creating a *new* container? Nesting a container? Setting something *on* a container?) but both methods don't appear to be necessary to work with bundles which appear to account for Symfony 5 such as:
https://github.com/willdura...
Does anyone have any other input on this?!
Edit
-----
I have not personally tested that bundle out on a Symfony 5 install myself; I just read the commit comments.
Hey ash-m!
Yep, you are correct :). You CAN use autowiring in your bundle, but as a best practice, it IS discouraged. The reason is that wiring things manually helps make your bundle a bit faster (on dev mode, it would be the same in prod) and makes your service config easier to read by others because it's explicit.
> Another way around this is to add `<call>` to each handler with method `setContainer` (which is admittedly a pain).
Yes, this is basically what autowiring is doing normally in this case. So, you're doing it manually. For others, there is a larger conversation about this over here: https://symfonycasts.com/sc...
> Tbh, idk how that works exactly (are you creating a *new* container? Nesting a container? Setting something *on* a container?)
It's a really cool, but complex feature :). You're creating a mini-container that has *some* services in it. You can learn about the idea here: https://symfonycasts.com/sc...
> but both methods don't appear to be necessary to work with bundles which appear to account for Symfony 5 such as: https://github.com/willdura...
If a controller doesn't extend AbstractController - https://github.com/willdura... - then none of this is needed. The AbstractController class is what tries to use this mini-service container :).
Cheers!
Thank you for the detailed reply! I am glad you mentioned the abstract controller thing because I attempted this a again using the ADR pattern and it ended up working and I didn't know why.
THK YOU BROTHER !
Hey Mindgruve
Sorry for being late to help you out but I'm glad that you could find and share the solution. Cheers!
Hey
I am facing issue in this video section i am running the command
bin/console debug:router
but its throw the error
In FileLoader.php line 168:
Unable to find file "@KnpULoremIpsumBundle/Resources/config/routes.xml" in @KnpULoremIpsumBundle/Resources/config/r
outes.xml (which is being imported from "C:\xampp\htdocs\ReusableBundleSymfony\config/routes/knpu_lorem_ipsum.yaml"
). Make sure the "KnpULoremIpsumBundle/Resources/config/routes.xml" bundle is correctly registered and loaded in th
e application kernel class. If the bundle is registered, make sure the bundle path "@KnpULoremIpsumBundle/Resources
/config/routes.xml" is not empty.
In Kernel.php line 275:
Unable to find file "@KnpULoremIpsumBundle/Resources/config/routes.xml".
debug:router [--show-controllers] [--format FORMAT] [--raw] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-e|outes--env ENV] [--no-debug] [--] <command> [<name>]
file path and code .
LoremIpsumBundle/src/Resources/config/routes.xml
`<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
</routes>
`
file path code
LoremIpsumBundle/src/Controller/IpsumApiController.php
`<?php
namespace KnpU\LoremIpsumBundle\Controller;
use KnpU\LoremIpsumBundle\KnpUIpsum;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class IpsumApiController extends AbstractController
{
}
`
file path and code
config/routes/knpu_lorem_ipsum.yaml
`_lorem_ipsum:
Hey Saif Ali Ansari!
Hmm. Before we dig in too far, this may be something simple. The error says:
> Unable to find file "@KnpULoremIpsumBundle/Resources/config/routes.xml
But you said the file is located at:
> LoremIpsumBundle/src/Resources/config/routes.xml
Try moving the Resources/ directory into LoremIpsumBundle directly - NOT inside of src/. If that doesn't help, let me know :).
Cheers!
Hey weaverryan
Thank you to reply
But i can remove from src but steel its gives same error .
i am stuck in this tutorial from last three day .
Hi Saif Ali Ansari!
Sorry for the slow reply! Ok, so let's at what's going on. First, in your
config/routes/knpu_lorem_ipsum.yaml, you have something that looks like this:And... that's great! For our purposes, the only really important part is the path:
@KnpULoremIpsumBundle/Resources/config/routes.xml.When Symfony sees this, it looks for the location of
KnpULoremIpsumBundleand then looks for the pathResources/config/routes.xmlinside of it to find the file. If you're following the tutorial exactly (which it seems like you are), then your bundle lives in theLoremIpsumBundle/. Directory, this means that Symfony is looking for the file in this EXACT path:Do you have a file at this path? I'm asking because, from your original message, you mentioned that your routing file lives at:
And this is NOT the correct path. The correct path is
LoremIpsumBundle/Resources/config/routes.xml. If you DO move the file toLoremIpsumBundle/Resources/config/routes.xmland you still get the error, then there are only 2 other possibilities I can think of:A) The KnpULoremIpsumBundle is not registered in your config/bundles.php... or it has a different name
B) The KnpULoremIpsumBundle class does not live at
LoremIpsumBundle/KnpULoremIpsumBundle.php. And so, the "root directory" of your bundle is somewhere else.Let me know what you find out :).
Cheers!
Hi guys, i see an error here:
"knpu_lorem_ipsum.controller.ipsum_api_controller" has no container set, did you forget to define it as a service subscriber?
In comments people write autowire is fix for an error, but the others not recommend it. So how resolve it then?
Hey triemli!
Sorry about the trouble - let's see if we can work it out :). Check out my answer here: https://symfonycasts.com/sc...
There was a subtle change in the core of Symfony, which makes this now required. Actually, I need to add a note about this to the tutorial - my fault for not doing it already!
Let me know if the solution I linked to helps - and if you want any further explanation.
Cheers!
Thanks a lot! It works!
Hello,
I'm working with Symfony 4, I created a bundle and added the routes inside the bundle /resources/config/routes.yaml, because I have to add the routes again in the global configuration, can't it be done automatically from the bundle?
Hey Jose carlos C.
You don't need to add the routes all over again. You need to tell the app to look into your route config file, in this case, your bundle route config.
One way is adding to the routes folder a reference for what you want:
<br />// config/routes/my_bundle.yaml<br />_my_bundle:<br /> resource: '@MyBundle/Resources/config/routes.xml'<br /> prefix: /<br />Hope that helps.
I just don't want it to depend on the global configuration, once it's defined in the Bundle, it doesn't need to be loaded again, so I'm looking for an option if it exists in Symfony to be able to have routes from the Bundle.
Hey Jose carlos C.
Pretty interesting question =) Of course you can use recipe system, but I think it's not suitable if you have not open source bundle. But there is another way. You can try make Custom route loader, and load routes from your bundle config file. It's described a little here https://symfony.com/doc/cur...
Cheers!
Thanks for the answer. I tried that already and it <b>doesn't work</b>. I have to include the routes.yml file back into app/config/routes.yml, https://stackoverflow.com/a/36487675/2046442 which means I have to include it out of the Bundle.
But there is good news! There is a solution that you only write once and it works for all your bundles, I found it here.
https://github.com/symfony/symfony/issues/22700#issuecomment-301151130
`
bundle_routes:
`
Oh! Thanks fro sharing it!
Without telling the app where are your routes I don't see a way. But you can create your config file that register your bundle route paths on the config folder as part of your bundle installation, like most of the bundles.
If you find a different workaround, please share here. I would be interested to know.
Hey Mindgruve
Thanks for you assistance with this question =) If you interested in another workaround check my answer above =)
Cheers!
hey, thanks for this tutorial;
I still wonder though, if there is a way for Symfony (Flex?...) to auto-add the bundle's routing file (as resource) - so that I would not do it manually? can / should I do that manually in the load() method maybe? could you please just give a hint?
Hey Yaroslav,
Actually, Symfony Flex can auto-add routes.... Are we talking about Symfony 4? For example, here's the routes for EasyAdminBundle that Flex will automatically add when you require this package: https://github.com/symfony/... - i.e. Flex just copy/paste the easy_admin.yaml file with routes into "config/routes/easy_admin.yaml" in your project, and if you have good Symfony 4 application - that file will be automatically read by the application, see "configureRoutes()" in Kernel that responsible to load those files: https://github.com/symfony/...
Did you upgrade to Symfony 4 your application from 3.x? Or did you create Symfony 4 application from scratch? Probably you missed something during your update?
Cheers!
btw, I also tried to do a routes file copy with the means of composer.json of my bundle
"scripts": {
"post-install-cmd": "php -r \"copy('Resources/config/routes.yml', '../config/routes/my-bundle.yml');\"",
"post-package-install": "php -r \"copy('Resources/config/routes.yml', '../config/routes/my-bundle.yml');\"",
"post-package-uninstall": "php -r \"unlink('../config/routes/my-bundle.yml')\""
}
but none of these worked (looks like none of these was called at all, because I tried echoing and got no output)...
Hey Yaroslav,
Well, doing so with Composer is not a correct way in my opinion.
Cheers!
hi Victor,
thank you for the prompt reply. yes, I'm using Smf 4.2, and just follow this tutorial instructions (except for manual addition of bundle's resource to app routes config).
I tried install / uninstall my bundle via composer, but the routes file was never auto-copied.
should I probably create some instructions for Flex (a recipe) so that it would know what file to copy?
Hey Yaroslav,
Ah, yes, now I see the problem. You're talking about *your* bundle, sure Flex does not know nothing about it except that it's a Symfony bundle so it just includes the bundle in bundles.php and that's it. To teach Flex that when you require your bundle Flex should copy some extra files as well - you need to create a recipe for your bundle. Recipe is just a set of instructions, you can see the same EasyAdminBundle as example: https://github.com/symfony/... . But that's the official recipe, your recipe should be in https://github.com/symfony/... repository where other non-official bundles are.
Here's more info about how to create a recipe: https://github.com/symfony/...
I hope that helps!
Cheers!
hi Viktor,
great, thank you for the reply! will surely give it a try.
thank you for your support!
You're welcome! ;)
Cheers!
Hey! I have error:
Type error: Too few
arguments to function
Mwil\DummyArticleBundle\Controller\DummyArticleApiController::__construct(),
0 passed in
C:\github\create-bundle\vendor\symfony\http-kernel\Controller\ControllerResolver.php
on line 111 and exactly 1 expected
in services.xml I have registered new service for controller with argument:
<service id="mwil_dummy_article_bundle.dummy_article_api_controller" class="Mwil\DummyArticleBundle\Controller\DummyArticleApiController">
<argument type="service" id="mwil_dummy_article_bundle.mwil_dummy_article"/>
</service>
But it not passing argument to constructor in DummyArticleApiController class
{{ PROBLEM RESOLVED }} I can't believe I missed typo and I was looking for a problem for an hour ;)
Hey, I've been going crazy trying to find why this bit will not work for me.
I keep gettin the error : [ERROR 1845] Element '{http://symfony.com/schema/d...}container': No matching global declaration available for the validation root.
Now from what I've gathered so far this is due to discrepancy between the xsd defintion and the actual xml file.
However my xml file just contains the defintions from the official symfony docs and one service :
<container xmlns="http://symfony.com/schema/d..." xmlns:xsi="http://www.w3.org/2001/XMLS..." xsi:schemalocation="http://symfony.com/schema/d...
http://symfony.com/schema/d...">
<services>
<service id="ddb_stuart_api.stuart_api" class="DdB\StuartApiBundle\StuartApi" public="true"/>
</services>
</container>
Does anyone have a clue why I might be getting this error ?
Thanks in advance for any help
Turns out i'm an idiot, when including the routes.xml file in my actual application I actually included the services.xml instead, please disregard my previous message
Hey DennisdeBest!
No worries! Let's just say that making mistakes is the best way to learn - so now you're an expert in this area ;). Good job figuring out the issue.
Cheers!
Hi guys, I found a discrepancy between the name of the controller service in the example file in the script, and the name that appears in the video. In the example it is `knpu_lorem_ipsum.controller.ipsum_api_controller`, but in the script it appears as `knpu_lorem_ipsum.ipsum_api_controller`.
Hey Tom,
Thanks for mentioning it! Yes, it looks like we have a little discrepancy between the video and its scripts, in the video we missed that "controller" string between "knpu_lorem_ipsum" and "ipsum_api_controller". Probably not a big deal, but it might be good to know for others.
Cheers!
I need to create some Doctrine Entities in my bundle, to be used by the external App. How can I add and configure these Entities?
Hey Lucas,
Good question! You can create an entity in your bundle and even use Doctrine annotation for mapping. Then in your app that uses the bundle you can create an empty class in src/Entity/ dir that will extend the one from the bundle - Doctrine will see all the parents fields. Or even better to create a Trait in your bundle instead of a class, so you don't need to inherit any class in your app, just *use* the trait - this way is even more flexible, thanks to PHP traits.
Cheers!
"Houston: no signs of life"
Start the conversation!