问题
I have no idea why my path to template isn't working. I have heard that everything is ok and it should be working, but it isn't.
..\project\src\Application\Sonata\AdminBundle\Resources\views\CRUD\list_image.html.twig
->add('photo', null, array('template' => 'ApplicationSonataAdminBundle:CRUD:list_image.html.twig'))
But when I try to fire the project, I get this error from log:
[2014-10-15 10:28:59] app.WARNING: An error occured trying to load the template "ApplicationSonataAdminBundle:CRUD:list_image.html.twig" for the field "photo", the default template "SonataAdminBundle:CRUD:base_list_field.html.twig" was used instead: "Unable to find template "ApplicationSonataAdminBundle:CRUD:list_image.html.twig".". [] []
For me this path is ok. I have no idea why this isn't working.
There is no typo, because path was copied and I was looking for typo for many, many times.
Please help!
回答1:
Big thanks for user Nic. He took me on the good way of finding the solution of this problem.
First, define new file: ApplicationSonataAdminBundle.php in ..\project\src\Application\Sonata\AdminBundle\
then in file:
namespace Application\Sonata\AdminBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class ApplicationSonataAdminBundle extends Bundle
{
public function getParent()
{
return 'SonataAdminBundle';
}
}
and then as a Nic said, we need to register our bundle in app/AppKernel.php:
new Application\Sonata\AdminBundle\ApplicationSonataAdminBundle(),
Thank you guys for posts and help!
回答2:
You can't make Twig loader to find your template because of your very complex directory structure.
I recommend you to omit some extra folders like Application or Sonata (or both). Also it is not a good manner to name your bundles with names of popular bundles.
To override 3rd party bundles you doesn't need to have Sonata folder in your Vendor's namespace. You just need to set getParent()
on your bundle to point on bundle that you are overriding.
Take a look at Symfony's best practices
来源:https://stackoverflow.com/questions/26400579/symfony-2-path-to-template