How to import mapping parameter in Symfony 2 configuration file? - symfony

Here is what I would expect to work fine:
parameters.yml
host:
main: example.com
app/config/config.yml
twig:
globals:
hosts_main: %host.main%
But an exception is thrown:
[Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException]
You have requested a non-existent parameter "host.main".
And I cannot find any examples in docs.
Strange.
For example, Incenteev\ParameterHandler\ScriptHandler::buildParameters() replaces "flow" notation with mapping if there is one in .dist file and both structures are equal in terms of YAML but Symfony itself seems to not understand YAML.
Could anyone please suggest some adequate workaround.

The parametes key is missing and it should be flat:
parameters:
host.main: example.com
Or
parameters:
hosts:
main: example.com
other: example.com
twig:
globals:
hosts: "%hosts%"

Related

Symfony: The class XYZ was not found in the chain configured namespaces

Existing questions did not help.
I am still learning Symfony and setting up an existing project. Trying to run doctrine fixtures from my application directory.
./app/console doctrine:fixtures:load --env test
And running this gives me following errors
Error thrown while running command "doctrine:fixtures:load --env test --em default". Message: "The class 'ClientPortal\GenericBundle\Document\Accountant' was not found in the chain configured namespaces _XyzProjectName\CalendarContext\Domain\Model, Integration\GetFeedbackBundle\Model, _XyzProjectName\AccountingProcessContext\Domain\Model, _XyzProjectName\CaseContext\Domain" {"exception":"[object] (Doctrine\Common\Persistence\Mapping\MappingException(code: 0): The class 'ClientPortal\GenericBundle\Document\Accountant' was not found in the chain configured namespaces _XyzProjectName\CalendarContext\Domain\Model, Integration\GetFeedbackBundle\Model, _XyzProjectName\AccountingProcessContext\Domain\Model, _XyzProjectName\CaseContext\Domain at /usr/local/var/www/1800-api/vendor/doctrine/persistence/lib/Doctrine/Common/Persistence/Mapping/MappingException.php:22)
And this exception is being thrown when trying to persist object of class
'ClientPortal\GenericBundle\Document\Accountant'
and here is my doctrine in config.yml
doctrine:
dbal:
driver: pdo_pgsql
host: '%database_host%'
dbname: '%database_name%'
user: '%database_user%'
password: '%database_password%'
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true
mappings:
CalendarBundle:
prefix: '_XyzProjectName\CalendarContext\Domain\Model'
IntegrationGetFeedbackBundle:
prefix: 'Integration\GetFeedbackBundle\Model'
AccountingProcessBundle:
prefix: '_XyzProjectName\AccountingProcessContext\Domain\Model'
CaseBundle:
prefix: '_XyzProjectName\CaseContext\Domain'
GenericBundle:
type: "annotation"
prefix: 'ClientPortal\GenericBundle\Document'
dir: 'src'
is_bundle: false
The snippet which is raising this exception
$account = new ClientPortal\GenericBundle\Document\Accountant();//full namespace added for clarity - also this file lies in the same directory structure.
$account->setSfAccountantId($accountantId);
$account->setUsername($username);
$manager = new ObjectManager;
$manager->persist( $account ); //This throws the above mentioned exception
My thought is, the file ClientPortal\GenericBundle\Document\Accountant.php is being autoloaded as there is no exception thrown at the time of instantion of its object. But, there is something missing with configuration or mapping because of which doctrine does not know how to persist its object.
[Source code][1] Accountant class
GenericBundle(s) registered under AppKernel
new Website\GenericBundle\WebsiteGenericBundle(),
new Admin\GenericBundle\AdminGenericBundle(),
new ClientPortal\GenericBundle\ClientPortalGenericBundle(),
new TaxApp\GenericBundle\TaxAppGenericBundle(),
So, as I suggested in comments - try to add ClientPortal\GenericBundle\Document\ namespace to mappings in your doctrine config file, like that:
#...
orm:
auto_generate_proxy_classes: "%kernel.debug%"
mappings:
# ...
ClientPortalGenericBundle:
type: annotation
prefix: 'ClientPortal\GenericBundle\Document\'
dir: '%kernel.root_dir%/src/ClientPortal/GenericBundle/Document/'
#is_bundle: false
auto_mapping: true
Had the same issue with Symfony 4 while using multiple database connections. This one https://stackoverflow.com/a/55361742/2540699 helped me to solved my problem.
I bumped into this kind of message in my Symfony 6.1 project, using multiple entity managers, with one of the entity managers specified as default in doctrine.yaml:
doctrine:
orm:
default_entity_manager: general
entity_managers:
general:
# config for general
other_entity_manager:
# config for other entity manager
Everything was fine on my dev environment, but on production I got the error
Uncaught PHP Exception Doctrine\Persistence\Mapping\MappingException: "The class XXX was not found in the chain configured namespaces " at /opt/approot/current/vendor/doctrine/persistence/src/Persistence/Mapping/MappingException.php
Problem seemed to be that I have a when#prod: section in my doctrine.config.yml. It seems I had to repeat the default_entity_manager over there:
when#prod:
doctrine:
orm:
default_entity_manager: general
# other prod-specific configuration
This fixed it for me.
Update: This is probably related to this issue: https://github.com/symfony/symfony/issues/27769

How can I test a page behind a security firewall in Symfony?

In Symfony's cookbook, there is a page entitled How to Simulate Authentication with a Token in a Functional Test. In it, it is said that:
The technique described in How to Simulate HTTP Authentication in a Functional Test is cleaner and therefore the preferred way.
Also, on the page that the quotation above links to, the documentation says:
The trick is to include the http_basic key in your firewall, along with the form_login key
This tells me that it is all right to have the form_login key, along with the http_basic key, and somehow http_basic should take precedence.
Here is my config_test.yml configuration file:
imports:
- { resource: config_dev.yml }
framework:
test: ~
session:
storage_id: session.storage.mock_file
profiler:
collect: false
web_profiler:
toolbar: false
intercept_redirects: false
swiftmailer:
disable_delivery: true
liip_functional_test:
cache_sqlite_db: true
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_sqlite
path: %kernel.cache_dir%/test.db
security:
firewalls:
default:
http_basic: ~
However, when I open my application in the test environment, I still get redirected to the login_form URL.
Why isn't setting the http_basic acting like the documentation says it should, namely it getting activated rather than form_login?
As commented here, having the code I pasted in my original question works just fine. The reason it is loading the login form is because I am not logging in via http_basic. In other words, when I have both form_login and http_basic enabled, I can login both by providing the PHP_AUTH_USER/PHP_AUTH_PASSWORD, and by logging in through the form. In effect, I don't need different security_*.yml files; I just need to add http_basic: ~ to the already-defined firewall.
Just split security.yml to security_test.yml and security_prod.yml.
In security_test.yml put default security configuration (as delivered with Symfony) or other one, which doesn't have firewall restrictions.
Create a specific config file for test environment, like config_test.yml with
imports:
- { resource: config.yml }
- { resource: security_test.yml }
note here config.yml itself doesn't have any security imports, because You will receive some Exception about overriding security directive or smth.
Create a separate config_prod.yml with
imports:
- { resource: config.yml }
- { resource: security_prod.yml }
Now You have separate security for test and prod environments.
If Your environment naming is good, then Kernel will pick config_test.yml only when tests are executed. For development environment Your should use config_dev.yml instead of config_test.yml

Symfony2 test parameters

Is there a way to configure test parameters with Symfony2?
I tried importing parameters_test.yml from the config_test.yml file but the variables still come from the parameters.yml file which is imported by config.yml which is imported by config_dev.yml which is imported by config_test.yml.
Basically I want to setup different variables in a test parameter file so I can access the values and also use them in the config files. If I hardcode the variables directly into config_test.yml I don't have access to them.
The variable I'm interested in particular is database_name or doctrine->dbal->dbname.
Thanks
Of course ensure your two first lines your config_test.yml are:
imports:
- { resource: config_dev.yml }
- { resource: parameters_test.yml }
In config_test.yml simple write this configuration:
# Doctrine Configuration
doctrine:
dbal:
driver: %database_driver_test%
host: %database_host_test%
port: %database_port_test%
dbname: %database_name_test%
user: %database_user_test%
password: %database_password_test%

Symfony Routing with variable host

I am using the current version of Symfony and want to define a route with a specific subdomain. This is what I have:
xyz:
resource: "#XYZ/Controller/"
type: annotation
prefix: /
host: "xyz.symfony.dev"
I want to have symfony.dev variable, only xyz as a subdomain is a static value. How can I realize this? I don't get it in the documentation.
I'm not good with regex but this answer your question:
xyz:
resource: "#XYZ/Controller/"
type: annotation
prefix: /
host: "xyz.{domain}.{ltd}"
requirements:
domain: "([a-z0-9-])+"
ltd: "([a-z])+"
EDIT: Regex added

The service "uri_signer" has a dependency on a non-existent parameter "kernel.secret". Did you mean this: "kernel.charset"?

When I clear my cache with app/console cache:clear I get the error:
[Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException]
The service "uri_signer" has a dependency on a non-existent parameter "kernel.secret". Did you mean this: "kernel.charset"?
What does it mean?
That means that kernel.secret hasn't been set, this value is used for generation of CSRF-tokens but could be used for other things as well.
Make sure kernel.secret is known in parameters.yml and import it in config.yml as follows:
imports:
- { resource: parameters.yml }
parameters.yml:
parameters:
kernel.secret: ThisIsVerySecret!
Or how it's done in the standard edition:
config.yml:
framework:
secret: "%secret%"
parameters.yml
parameters:
secret: ThisIsVerySecret!
This means that you're forgotten to fill in the secret parameter in app/config/parameters.yml. It can't be null or empty.

Resources