I am getting the following error when I try to clear the cache in Symfony:
"Cannot import resource C:...\config.yml from "C:...\config_dev.yml"
(There is no extension able to load the configuration for
"knp_paginator")..."
And it goes on to say:
"Looked for namespace "knp_paginator", found "framework", "security",
"twig", etc etc"
It happens when I put the config settings suggested at https://github.com/KnpLabs/KnpPaginatorBundle into my config.yml file:
# Pagination
knp_paginator:
page_range: 5 # default page range used in pagination control
default_options:
page_name: page # page query parameter name
sort_field_name: sort # sort field query parameter name
sort_direction_name: direction # sort direction query parameter name
distinct: true # ensure distinct results, useful when ORM queries are using GROUP BY statements
template:
pagination: KnpPaginatorBundle:Pagination:sliding.html.twig # sliding pagination controls template
sortable: KnpPaginatorBundle:Pagination:sortable_link.html.twig # sort link template
What am I doing wrong?
Discovered what it was:
I needed to add the bundle to the application kernel. In app/AppKernel.php, the function registerBundles() I needed to add:
new Knp\Bundle\PaginatorBundle\KnpPaginatorBundle(),
And that fixed it.
for symfony 5 : add
Knp\Bundle\PaginatorBundle\KnpPaginatorBundle::class=> ['all' => true],
in config/bundles.php
Related
Preface: in ez4 i remember there was a tpl function to read ini settings, we used to use this to pass specific locations or id's with which we could then render certain content.
In ezplatform I am now doing the same thing but by using the PreContentViewListener (in the PreContentViewListener read a yml file and pass into the view as params), but this doesn't feel like the correct way as the PreContentViewListener doesn't always get triggered, in custom controllers for example.
Question
Is there a native way to read yaml files from within twig templates? After searching the docs and available packagists i cannot find anything :/
If your needs are simple (i.e. reading container parameters), you can also use eZ Publish config resolver component which is available in any Twig template with ezpublish.configResolver.
You can specify a siteaccess aware parameter in format <namespace>.<scope>.<param_name>, like this:
parameters:
app.default.param.name: 'Default param value'
app.eng.param.name: 'English param value'
app.cro.param.name: 'Croatian param value'
where default, eng and cro are different eZ Publish scopes.
You can then use the config resolver to fetch the parameter in current scope with:
{{ ezpublish.configResolver.parameter('param.name', 'app') }}
If you have Legacy Bridge installed, this even falls back to legacy INI settings if no Symfony container parameter exists:
{{ ezpublish.configResolver.parameter('SiteSettings.SiteName', 'site') }}
Disclaimer: Some say that using config resolver is bad practice, but for simpler usecases it is okay, IMO.
Have a look to our CjwPublishToolsBundle.
https://github.com/cjw-network/CjwPublishToolsBundle
https://github.com/cjw-network/CjwPublishToolsBundle/blob/master/Services/TwigConfigFunctionsService.php
Here we have 2 wrapper twig functions
{{cjw_config_resolver_get_parameter ( 'yamlvariablename', 'namespace default ezsettings') }}
=> ezpublish siteaccessmatching
{{cjw_config_get_parameter( 'mailer_transport' )}}
=> core symfony yaml reader without siteaccess
You could do a lot of things in eZ 4 and not always really good for your application design. ezini was able to read the configuration from the template but now in eZ Platform and by extension Symfony you need to respect more common patterns. IMO the view should not be that smart.
Then injecting variables to the view from a listener (PreContentViewListener or your own) is not a bad idea.
You can also use the Twig Globals that could allow you to do 2 global things:
inject variables (1)
inject a service (2)
Look here: https://symfony.com/doc/current/templating/global_variables.html
(2): please don't inject the service container globally it is bad
(1): I don't remember if the Twig Globals are Site Access aware, if not injecting your own service (2) to manage access to the config might be better.
And finally, I think that the use case is not a common one:
we used to use this to pass specific locations or id's with which we could then render certain content.
Most of the time it is a bad idea to pass ids coming from the configuration to render something, it is much better to organize the content structure to let you pull the location you want using the PHP API. (no id in configuration no hassle with dev, stage, preprod and prod architecture)
After updating to Symfony 2.6, pages using kn._paginator no longer load:
An exception has been thrown during the rendering of a template
("Warning: file_get_contents(/Users/steve/dev/sites/virtual/newgt/
vendor/knplabs/knp-paginator-bundle/Knp/Bundle/PaginatorBundle/Resources/views
/Pagination/sliding.html.twig):
failed to open stream: No such file or directory") in
NewgtMediaBundle:Default:showSeriesSeason.html.twig at line 32
Config.yml ::
# KNP Paginator
knp_paginator:
page_range: 5 # default page range used in pagination control
default_options:
page_name: page # page query parameter name
sort_field_name: sort # sort field query parameter name
sort_direction_name: direction # sort direction query parameter name
distinct: true # ensure distinct results, useful when ORM queries are using GROUP BY statements
template:
pagination: KnpPaginatorBundle:Pagination:sliding.html.twig # sliding pagination controls template
sortable: KnpPaginatorBundle:Pagination:sortable_link.html.twig # sort link template
The requested template:
/Users/steve/dev/sites/virtual/newgt/vendor/knplabs/knp-paginator-bundle/Knp/Bundle/PaginatorBundle/Resources/views/Pagination/sliding.html.twig
actually exists at:
/Users/steve/dev/sites/virtual/newgt/vendor/knplabs/knp-paginator-bundle/Resources/views/Pagination/sliding.html.twig
How do I tell my app where knp_paginator should look for the template?
Update config.yml
knp_paginator:
....
template:
# Following two lines were present before Symfony 2.6 upgrade.
# Commented out to load from default location.
# pagination: KnpPaginatorBundle:Pagination:sliding.html.twig # sliding pagination
# sortable: KnpPaginatorBundle:Pagination:sortable_link.html.twig # sort link template
Then
sudo rm -rf app/cache/prod
app/console cache:clear –env=prod
(same for –-env=dev)
both
http://mysite/app_dev.php/media/gallery/24223?page=5&count=82&number=1752&series=FIAGT&season=2009
and
http://mysite/media/gallery/24223?page=5&count=82&number=1752&series=FIAGT&season=2009
are working again.
I finished configuring SonataMediaBundle successfully then the page
/formation/web/app_dev.php/admin/dashboard
is displayed.
But when I click "list" (menu Media), I get this error:
An exception has been thrown during the rendering of a template
("Parameter "id" for route "admin_sonata_media_media_edit" must match
"[^/]++" ("" given) to generate a corresponding URL.") in
SonataMediaBundle:MediaAdmin:list_custom.html.twig at line 17.
Any ideas?
I think your problem is based on this known issue:
Sonata type model - hide/delete the "create" button · Issue #530 · sonata-project/SonataAdminBundle
It is recommended to update to Doctrine 2.4
I created a parameter in my parameters file:
parameters:
category:
var: test
I can access this in PHP by fetching it like this:
$var = $this->container->getParameter('category');
$var = $var['var'];
But how can I access this parameter in my config.yml? For example, I want to pass this parameter to all my twig files as a global twig variable:
twig:
globals:
my_var: %category.var% # throws ParameterNotFoundException
(Sidequestion:
I assumed I could access it via getParamter('category.var'), but got an error there. Do you know a nicer way than my two-liner? $this->container->getParameter('category')['var'] works, but is a syntax error according to my IDE.)
$this->container->getParameter('category')['var']
..is actually a pretty good way to go. Which version of PHP is your IDE using for its syntax checking? Somebody please correct me, but I think this behavior was made valid in 5.3 or 5.4.
I want to output "Site information" form in another place. I tried to use drupal_get_form and it does not work as expected
the code in menu callback
drupal_get_form('system_site_information_settings');
Error message
Notice: Undefined index: system_site_information_settings in
drupal_retrieve_form()
My guess the form ID is wrong?
The form ID you're using is definitely the right one...system_site_information_settings() lives in the system.admin.inc file in the core system module. By default it's not included in the page process, you need to include it manually:
module_load_include('inc', 'system', 'system.admin');
$form = drupal_get_form('system_site_information_settings');