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
Related
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
I'm Wondering Why There is no "Models / Views / Controllers" Directories Inside SS4 Main Directory? It's Seems Like Everything Should Go Inside "mysite/code" .. How To Accomplish MVC Style?
MVC is more of a software-architectural pattern, not how you organize files. There are only a few assumptions the SilverStripe framework makes on how you should organize your code:
Model
With SilverStripe 4, the default code folder for every module is named src or code. In that folder you're free to organize your files as you see fit. Ideally you build your folder structure in a PSR-4 compliant way, so that your folder-names match with your namespaces.
Controllers
Same as with Model classes, you can put your controllers anywhere you want.
There's a default assumption for Page classes though. These expect that the matching controller has the same namespace as the page. So if your page is named: Company\Module\Pages\MyPage, the framework would look for Company\Module\Pages\MyPageController.
You're free to override that though, by implementing the getControllerName method on your Page and return the FQCN for the controller to use.
Views
Also known as "templates" should be in a templates folder. Each module (your mysite folder is also a module) can have a templates folder.
In addition to this, you can have themes. Themes usually bundle templates, css and other assets. In the themes folder you can have multiple theme folders, each one can have a templates folder.
Basic Question
How can you make symfony look in non-standard directories to find the "best" (custom) Twig template to load for a bundle view?
The Symfony docs say by default it looks in two locations to override a Twig template
When the AcmeBlogBundle:Blog:index.html.twig is rendered, Symfony
actually looks in two different locations for the template:
app/Resources/AcmeBlogBundle/views/Blog/index.html.twig
src/Acme/BlogBundle/Resources/views/Blog/index.html.twig
But this is discussing how you would override a vendor bundle. In my case, I have native bundles in my /src/ that I want to overwrite on a per Design Template or Client specific basis. It needs to look in:
Client: /var/www/vhosts/{ID}/src
Template: /var/www/core/cms/src/Gutensite/TemplateBundle/Templates/Admin/src
Twig Loader has a convenient method to add paths:
$templatePath = '/var/www/core/cms/src/Gutensite/TemplateBundle/Templates/Admin/Resources/views';
$this->container->get('twig.loader')->prependPath($templatePath, 'template');
This allows me to register an alternative path to the template resources, so that I can render the template shell like this:
{% extends '#template/shell/shell.html.twig' %}
But what about when I want to overwrite a bundle template, e.g.
Original: /var/www/core/cms/src/Gutensite/MenuBundle/Resources/views/Menu.html.twig
Custom: /var/www/core/cms/src/Gutensite/TemplateBundle/Templates/Admin/src/Gutensite/MenuBundle/Resources/views/Menu.html.twig
How do I register a generic /src/ file so that Symfony looks in there for all references to Vendor Bundle paths, e.g. trying to render #GutensiteMenu/Menu.html.twig will first look in the custom directory for 1) Client , 2) Template, 3) default bundle directory by that name.
Need Assets
Because my TemplateBundle/Templates/Admin/Resources/ are in a non-standard location, assetic won't dump them to the public directory (or create symlinks)... so I'm not sure how to dynamically make assetic find these files.
I'm also not sure how to load the assets that are in these other locations, e.g. this doesn't work:
{% stylesheets '#GutensiteTemplateBundle/Templates/Admin/Resources/public/css/site.css' %}
<link rel="stylesheet" href="{{ asset_url }}">
{% endstylesheets %}
Presumably because it's not dumped.
Why do I need this?
I am building a hosted CMS that is a core vendor which contain various bundles with controllers and templates, e.g. /Gutensite/CmsBundle, Gutensite/ArticleBundle.
Based on the selected "Design Template" for a site, a design template is referenced in a TemplateBundle, e.g. /TemplateBundle/Templates/Admin (a template called "Admin"). The TemplateBundle need to be able to override the core controllers or views.
I have registered the Gutensite/TemplateBundle/Templates/Admin/src/ folder as an alternative namespace for the Composer app/autoload.php, so that controllers can be overwritten as long as they have the same Gutensite namespace (and this works great):
// in my primary controller:
$loader = $GLOBALS['loader'];
$loader->add('Gutensite', '/var/www/core/cms/src/Gutensite/TemplateBundle/Templates/Admin/src', true);
NOTE: I could register every single template as a bundle, and that would theoretically allow me to override controllers and bundles using symfony's built in methods. But this would add a lot of "bundles" that aren't really traditional bundles. Also I need the above solution to point to alternative template paths, because I also need the ability to point to client's custom files located outside the normal symfony root directory (which I successfully do with the namespace auto loader paths for controllers).
You can use the path option in the twig configuration, with the same namespace for 2 paths.
twig:
# ...
paths:
"%kernel.root_dir%/../src/Gutensite/MenuBundle/Resources/views": templates
"%kernel.root_dir%/../src/Gutensite/TemplateBundle/Templates/Admin/src/Gutensite/MenuBundle/Resources/views": templates
Then, using #templates/Menu.html.twig will first look for Menu.html.twig in MenuBundle, then in TemplateBundle.
That's this way that the TwigExtension is registering paths so the loader is looking in app/Resources/views and then in the bundle's views.
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.
TLDR: How do I make Assetic scan for assets in Twig templates outside bundle?
I've got few registration wizards. Each of these wizards has it's own view directory, the file structure looks like this:
/SiteBundle/Wizard/General/Resources/views
/SiteBundle/Wizard/CountrySpecific/Resources/views
/SiteBundle/Wizard/[...several more...]/Resources/views
In config.yml I defined these paths for twig so I can use #general_wizard/template.html.twig paths:
twig:
paths:
"%kernel.root_dir%/../src/MyWeb/SiteBundle/Wizard/General/Resources/views": general_wizard
"%kernel.root_dir%/../src/MyWeb/SiteBundle/Wizard/CountrySpecific/Resources/views": country_specific_wizard
The problem is that assets used in these templates (inside the Wizard directories) are not dumped using assetic:dump. When I move the view sources to regular SiteBundle/Resources/views, then all the assets are correctly dumped.
Is there a way to make Assetic check the external templates too?
It is not possible with the stock assetic:dump (SF 2.3), as the /Resources/views/ path is hardcoded in the GeneratorBundle, which again provides the list of files to process to assetic. Of course, you could write your own command, but you would basically reinvent the wheel.
I would recommend to keep with the Resources/views convention, and create the subcategories below that:
/SiteBundle/Resources/views/General
/SiteBundle/Resources/views/CountrySpecific
That would have the same effect, and would not require you to write your own commands and mess around with SF2 internals.
I encountered the same problem after moving all templates to templates in the project root, as it will probably come to be in future versions of Symfony.
The simplest solution is to configure all assets in the configuration file and to locate the output files in the web directory, e.g., all bootstrap CSS files into web/css/bootstrap.css. A remaining difficulty is that referencing the assets with the javascripts tag doesn't work anymore because those tags have to be scanned by the Assetic Bundle to work. You have to do this the old-fashioned way, e.g., via {{ asset('css/bootstrap.js') }}.
With Bootstrap I encountered difficulties to make Glyphicons work, since the cssrewrite filter doesn't work as before. I went for Fontawesome at that point, but including your own copy of Glyphicons could also work.