Activate StringLoader Twig Extension in Symfony - php

I'm trying to active the Twig StringLoader Extension in a Symfony 2.3 project but just can't get the yaml-syntax right.
This post refers to the answer by Heyflynn on a post dealing with the exact same problem but providing a solution that just does not work (for me).
Writing the following lines in my app/config/config.yml just gives me the exception below:
# app/config/config.yml
acme.twig.extension.loader:
class: Twig_Extension_StringLoader
tags:
- { name: 'twig.extension' }
Gives me this:
FileLoaderLoadException: Cannot import resource ".../app/config/config.yml" from ".../app/config/config_dev.yml". (There is no extension able to load the configuration for "acme.twig.extension.loader" (in .../app/config/config.yml). Looked for namespace "acme.twig.extension.loader", found "framework", "security", "twig", "monolog", "swiftmailer", "assetic", "doctrine", "sensio_framework_extra", "apy_data_grid", "project", "acme_demo", "web_profiler", "sensio_distribution")
(I was already wondering about the acme appearence in acme.twig.extension.loader and replaced it with the name of the project bundle but that gives me the same exception again. Just cutting it off doesnt work either.)
Please help!

The Twig is not part of acme bundle, Twig is vendor bundle itself and therefore the Error is correct. There is not such namespace as acme.twig.extension.loader
The fixed code would be:
# services.yml
services:
twig.extension.stringloader:
class: Twig_Extension_StringLoader
tags:
- { name: twig.extension }
This can be added to /app/config/config.yml to use to in each bundle or add it into your bundle folder into /Resources/config/services.yml to use in only in certain bundle.
Then in twig templates use them as:
{{ include(template_from_string(page.template)) }}
The above works for me in Symfony v2.5

Related

Twig Gravatar on Symfony 4

I'm a newbie on Symfony and I'm having a problem to integrate the 'ry167/twig-gravatar' package on my project.
First, I did :
$ composer require ry167/twig-gravatar 3.0.0
And after I modified my services.yaml, which looks like this :
services:
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
App\Controller\:
resource: '../src/Controller'
tags: ['controller.service_arguments']
twig.extension.gravatar:
class: \TwigGravatar
arguments:
$default: ~ e.g. 'monsterid'
$size: ~ e.g. 50
$filterPrefix: ~ e.g. 'foo'
$rating: ~ e.g. 'x'
$useHttps: true
tags:
- { name: twig.extension }
And finally, I have this on my view:
<p>{{ 'example#example.com'|grAvatar }}</p>
But I got this error:
Invalid service "twig.extension.gravatar": class "Twig_Extension" not found while loading "TwigGravatar"
Any ideas? I can't understand where my problem comes from...
You probably use Twig 3.* which removed all PSR-0 classes (with the underscore).
The next version of ry167/twig-gravatar fixes the issue.
There is already a release candidate.
Option 1: Wait for next stable release
If you want to wait for the stable release, then temporarily add a conflict block to your composer.json to use the latest Twig version before 3.0:
{
...
"require": {
...
"ry167/twig-gravatar": "^3.0.0",
...
},
"conflict": {
"twig/twig": ">=3.0"
}
}
Run composer update afterwards to let Composer do the work of figuring out the dependencies and downgrading your Twig version.
You may remove the conflict when version 4.0 is released and you changed the dependency to ^4.0.0.
Option 2: Use Release Candidate
If you want to use the new version right away, you have to tell composer that non-stable versions are alright using stability flags.
composer require ry167/twig-gravatar "^4.0.0#RC"

Symfony 4.2 Problem with FOS Comment Bundle, could not load routing.yaml

I am struggling with installation of FOS Comment Bundle. I have Symfony v 4.2.1.
I followed instructions from documentation: https://github.com/FriendsOfSymfony/FOSCommentBundle/blob/master/Resources/doc/1-setting_up_the_bundle.md
I ommitted only this part:
assetic:
bundles: [ "FOSCommentBundle" ]
Because assetic is not working with Symfony ^4. I installed assets by: bin/console assets:install
I get that error:
LoadException: Cannot load resource "#FOSCommentBundle/Resources/config/routing.yml". Make sure the "FOSCommentBundle" bundle is correctly registered and loaded in the application kernel class. If the bundle is registered, make sure the bundle path "#FOSCommentBundle/Resources/config/routing.yml" is not empty.)
Routes.yaml:
fos_comment_api:
type: rest
resource: '#FOSCommentBundle/Resources/config/routing.yml'
prefix: /api
defaults: { _format: html }
I did not find any solution. Anyone knows what should I do or know any alternative bundle to comments' threads?

Could not find any fixture services to load

i know this question has been asked already multiple times:
Symfony 3.4 and Fixtures Bundle issue with bundle version 3.0
Symfony 3.4.0 Could not find any fixture services to load
Symfony Doctrine can't find fixtures to load
Could not find any fixture services to load - Symfony 3.2
None of the above actually helped me out. That said, this is my configuration:
Composer.json
"require": {
"php": ">=7.0",
"doctrine/doctrine-bundle": "^1.6",
"doctrine/orm": "^2.5",
....
},
"require-dev": {
....
"doctrine/doctrine-fixtures-bundle": "^3.0",
}
I'm using a company-developed Bundle which worked fine until the last version of the above (last tested and working configuration had PHP 5.6, Doctrine bundle ^1.6,doctrine orm ^2.5, fixture bundle ^3.0).
This bundle has some Fixture inside VendorName/BundleName/DataFixtures/ORM, all the fixtures have the following declaration:
Class MyFixture1 extends Fixture implements OrderedFixtureInterface,FixtureInterface, ContainerAwareInterface{
...
}
Inside this bundle there's a services.yml file, loaded by this:
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
}
VendorName/BundleName/Resources/config/Services.yml i tried different configurations:
services:
_defaults:
autowire: true
autoconfigure: true
public: false
VendorName\BundleName\:
resource: '../../*'
# you can exclude directories or files but if a service is unused, it's removed anyway
exclude: '../../{Entity,Repository,Tests,EventListener}'
I tried expliciting searching inside DataFixtures:
VendorName/BundleName\DataFixtures\:
resource: '../../src/VendorName/BundleName/DataFixtures'
tags: ['doctrine.fixture.orm']
And even manually configure the service:
VendorName\BundleName\DataFixtures\ORM\MyFixture1:
class: VendorName/BundleNamee\DataFixtures\ORM\MyFixture1
tags: ['doctrine.fixture.orm']
But unfortunately it keeps giving the error. Any idea of what i'm doing wrong? Long time ago it was possible to manually specify to the fixture command which bundle to look in, but now it's not possible anymore
UPDATE 1:
Sorry guys forgot to point the error message: "Could not find any fixture services to load"
The accepted answer helped point me in the right direction to get this working. Though cache could indeed be a problem, having Fixtures in your own bundles does require you to register them.
Using SF 5.1.* I created a services_dev.yaml file and added in:
services:
// other config
Namespace\Registered\In\Composer\For\Fixtures\:
resource: '../vendor/path/registered/in/composer/to/fixtures'
tags: ['doctrine.fixture.orm']
Then indeed, clear cache and it should work.
Added *_dev to the services.yaml file as it's a way for Symfony to only load it in when in your in a dev environment (per APP_ENV Environment variable).
For everyone landing here i solved my problem, what i can tell you is:
Double check you enabled the Bundle in the AppKernel
Clear your symfony cache using the command php bin/console cache:clear --env=dev (or env=prod)
Manually delete cache folder

Symfony 3.4 console command definition not being recognized

I'm upgrading a existing Symfony 3.3 application to Symfony 3.4.
I'm getting a console command auto-registration warning in spite of the fact that I've defined the command in the bundle's services.yml file. The warning:
[2017-12-14 12:22:19] php.INFO: User Deprecated: Auto-registration of
the command "Dplh\DplhIssuesBundle\Command\IssuesReportCommand" is
deprecated since Symfony 3.4 and won't be supported in 4.0. Use PSR-4
based service discovery instead.
The entry in Dplh/DplhIssuesBundle/Resources/config/services.yml:
services:
Dplh\DplhIssuesBundle\Command\IssuesReportCommand:
public: true
tags: ['console.command']
calls:
- [ saveDependencies, [ '#logger' ]]
I am positive that the services.yml file is being processed by Symfony because if I intentionally create a syntax error in it, Symfony reports that the file contains invalid YAML.
Finally found it.
There was a reference to the Dplh\DplhIssuesBundle\Command\IssuesReportCommand class in another bundle's services.yml file (copy/paste error, I'm sure).
Removing that reference removed the error.

Symfony2 & Doctrine2 - Class 'Gedmo\Tree\TreeListener' not found

I´m trying to make working doctrine2 extensions but it still wrotes me this error:
Fatal error: Class 'Gedmo\Tree\TreeListener' not found in /data/web/virtuals/48565/virtual/www/domains/kozusnikjan.com/Symfony/app/cache/prod/appProdDebugProjectContainer.php on line 1377
And I don´t know, how to solve the problem. Please, help. Here are some files:
http://pastebin.com/k9rqvGQn -- config.yml
http://pastebin.com/TvxbvEyS -- doctrine-extensions.yml
http://pastebin.com/prUFmrTb -- AppKernel.php
http://pastebin.com/0zAaHwW9 -- DoctrineExtensionListener.php
Thank you very much
Edit:
New files:
http://pastebin.com/qEAZtFba -- composer.json
http://pastebin.com/aBnvrZj9 -- config.yml
http://pastebin.com/E1aSSddm -- appKernel.php
I edited this files. I didn´t create any file. Thank you for your help!
You're using Gedmo directly. Try through StofDoctrineExtensionsBundle...
If you are using composer add to your composer.json at require section
"stof/doctrine-extensions-bundle": "1.1.*#dev"
and run composer update. You must load bundle on AppKernel.
then put on your config.yml
stof_doctrine_extensions:
default_locale: %locale%
orm:
default:
tree: true
May be you wanna find your problem, but I use Gedmo this way and works fine.
I know it's an old Problem, but I had this one too today. Solved it by declaring the tree listener as a service:
https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/symfony2.md#doctrine-extension-listener-services
It's the same for every Gedmo extension such as translatable, sluggable, timestampable...

Categories