Unable to find template - moved Resources from AppBundle to app folder - symfony

I moved the Resources/ twig templates from AppBundle to the app folder and now I got:
Unable to find template "ItemBundle:Category:index.html.twig".
I already changed the Routes in the Annotations for the Controllers, but where can I changed the default behaviour for index.html.twig, because the alway want to look at the Bundle, but there is no Bundle anymore, it's just the app folder.
/**
* Lists all Category entities.
*
* #Route("/", name="category")
* #Method("GET")
* #Template(":Category:index.html.twig")
*/
I don't want to change all #Template - Just explain, that all stuff is now under app folder and than the normal CRUD way. For the normal CRUD way, I don't need to write down each #Template where to find the twig template.

I think you're misunderstanding how those template references resolve. Here's the relevant documentation
When you use something like ItemBundle:Category:index.html.twig that means Symfony will try to find that template in two places, in this order
app/Resources/ItemBundle/views/Category/index.html.twig
src/ItemBundle/Resources/views/Category/index.html.twig
So this is going to depend on where you moved them to. So let's say for example that you moved this one twig file to app/Resources/views/Category/index.html.twig. Make note of how this is different than #1 above - the template is no longer in a bundle directory - just a sub-directory of the root views directory.
Therefore, the proper references is ::Category/index.html.twig

Related

Is there a way to move Symfony 6's controllers to several other directories?

I'm starting to develop a project that will be quite big (hear lots of files) and I want to have an organization that's different from Symfony's default one: instead of having one dir for all my controllers, another for all my forms, etc, I want to have one dir per functionality, ie a directory for my homepage which will contain the controller, the templates, another dir for the blog page with the controller, forms and templates, and so on.
I tried what was explained in this (old) answer, and it didn't work : my routes weren't recognized, and a php bin/console debug:router showed they weren't listed anymore.
I feel I will have something to adapt in my routes.yaml file, which looks like this for now, and which explains why it doesn't work because it explicitely searches in the src\Controller directory:
controllers:
resource:
path: ../src/Controller/
namespace: App\Controller
type: attribute
I looked in the documentation I found, but I didn't find anything useful (I think I will have to remove the path and change it to something else, but I don't know what yet.
The above solutions are only for differentiating controller and template files. But For Entity, Events, and Forms it will not work.
It seems like you want to separate files for each module or functionality. For example, settings.yaml, controller, entity, forms, events, etc...
For that Symfony provides a Bundle feature. You can create a new Bundle in Symfony and use it as a separate feature. For more details please read this link

Use bundle error templates in Symfony 3 app

I have a MyBundle bundle which I use in many Symfony applications.
This bundle provides common things which are shared across these applications, such as Controllers, Entities, templates, etc.
I would like the bundle to provide error templates as well.
I tried to put the templates in MyBundle/Resources/TwigBundle/views/Exception folder but the application does not use them.
When I copy TwigBundle folder from MyBundle/Resources into app/Resources then the templates are used as expected. However I do not want to copy the templates into every application's app/Resources folder.
Is it possible to override error templates using MyBundle/Resources instead of app/Resources?
TwigBundle always by default checks directory app/Resources/TwigBundle/views/Exception/ for templates.
If you want to use custom directory you have to override ExceptionController.
You can do this in config
// app/config/config.yml
twig:
exception_controller: MyBundle:Exception:showException
Default Twig ExceptionController can be found here.
Documentation

Referencing an entity from another bundle in Doctrine in Symfony 3

Firstly, I have already searched this and tried a few solutions but none work - I may well have missed something so this is my issue.
I have three different Bundles in my Symfony 3 project, AppBundle (shared one), ClientBundle (for clients only) and AdminBundle (for admin users only). The only Entity present in the AppBundle is User.php which is extended from the FOSUserBundle in the project, as I needed extra fields. I need to be able to map a field in the User entity to a field in the Site entity which is located in the AdminBundle, but whenever I try to update the schema, I get the following error:
[Doctrine\ORM\Mapping\MappingException] The target-entity
AppBundle\Entity\Site cannot be found in
'AppBundle\Entity\User#siteId'.
Currently my mapping looks like this, in User.php:
/**
* #ORM\ManyToOne(targetEntity="\AdminBundle\Entity\Site")
* #ORM\JoinColumn(name="site_id", referencedColumnName="id")
*/
private $siteId;
As you can see, I have used the full path to the Site entity here. I have even included a use statement for the Entity, just in case:
use AdminBundle\Entity\Site;
But still the same error occurs upon schema update.
Can anyone shed any light on this? I need to be able to get this mapping to work and I cannot move the user entity to the AdminBundle as it is required by both admin and client users, and with my config set up, clients are restricted to the ClientBundle and the AppBundle.
change to targetEntity="AdminBundle\Entity\Site" (remove the leading slash)

how to override FOSUserBundle's schema file in other bundle

I'm working in symfony 2.5 with propel for my project.
I need to add a behavior to fos_user table so I need a way to override the original schema file in my src/ directory. Defining my project as a child of FOSUserBundle does work but this is not what I want.
Is there another way?
you can copy the original schema file to your app directory
and add the behavior you need. The way is exactly like overriding
any twig templates from vendors.

Generating a CRUD Controller in a different bundle

I want to generate some CRUD controllers with the following command:
php app/console generate:doctrine:crud
my problem is that I made 2 bundles (1 front end and 1 for the admin section) my entities are in the defaultbundle, but I'd like to generate the CRUD controllers in my admin bundle, is there a way to do this?
Generating CRUD controllers in a bundle different of the entity's one is, as far as I know, impossible.
Indeed, CRUD controllers generator is just here for test. But if you really want to use it, you can actually copy the controller, views and form in the other bundle. You'll just need to change the templates' paths and the namespaces at the beginning of the controller and the Form type.

Resources