I am new to Symfony2 and I have been following the symblog tutorial
But I get this error when I load the homepage
ParameterNotFoundException: You have requested a non-existent parameter "secret".
Where should I check for that whether in config.yml if config.yml in which statement it should be edited.
That parameter will be set in your app/config/parameters.ini (or .yml on newer versions). Make sure that file exists and looks something like this:
[parameters]
database_driver = pdo_mysql
database_host = localhost
database_port =
database_name = symfony
database_user = root
database_password =
mailer_transport = smtp
mailer_host = localhost
mailer_user =
mailer_password =
locale = en
secret = ThisTokenIsNotSoSecretChangeIt
I had the same problem and it turned out that I added a second imports section in my config.yml.
So I removed it and just added my resource in the top imports section at the top of the file and now it works!
I was adding the sonata admin service.
Hope this helps.
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: #MyBundle/Resources/config/admin.yml }
Instead of the incorrect
imports:
- { resource: parameters.yml }
- { resource: security.yml }
imports:
- { resource: #MMyBundle/Resources/config/admin.yml }
I also studied this article. And I also got this error. As found. I did not correctly copied the example in the file app / config / config.yml
Related
I have a registered service where I inject the content of a json as argument (see followed configuration, App\Service\Authenticator service)
parameters:
env(LDAP_CONFIG_FILE): '../auth.json'
services:
_defaults:
autowire: true
autoconfigure: true
App\:
resource: '../src/'
exclude:
- '../src/DependencyInjection/'
- '../src/Entity/'
- '../src/Kernel.php'
App\Service\Authenticator:
arguments:
$config: '%env(json:file:resolve:LDAP_CONFIG_FILE)%'
$environment: '%env(APP_ENV)%'
When I try to inject this service in a controller, this works fine as expected. But when I inject the service into a command I get this error :
> php bin/console app:execute
[WARNING] Some commands could not be registered:
In EnvVarProcessor.php line 136:
File "../auth.json" not found (resolved from "resolve:LDAP_CONFIG_FILE").
Command "app:execute" is not defined.
And if I replace env(LDAP_CONFIG_FILE) by auth.json it work in a command but not in a controller anymore.
How can I configure to make it work in both commands and controllers ?
Edit: I found a temporary fix with the default processor but I really want a clean solution.
I want to configure the Doctrine bundle to have a DBAL connection. For some reason the configuration needs a bit of logic to retrieve. I tried to use a container extension and then a compiler pass to execute the logic while the container is compiled and store the configuration as container parameters.
During my attempts, I registered the extension and compiler pass like this in the Kernel class:
protected function build(ContainerBuilder $container)
{
// Those lines weren't there at the same time
$container->registerExtension(new MyCustomExtension());
$container->addCompilerPass(new MyCustomCompilerPass());
}
It seemed to work well as I could see my parameters in the console:
# ./bin/console debug:container --parameters
Symfony Container Parameters
============================
------------------------------------------------------------- ------------------------------------------------------------------------
Parameter Value
------------------------------------------------------------- ------------------------------------------------------------------------
...
some.prefix.host some-mariadb-host
some.prefix.dbname some-database-name
...
The problem is that when I try to use those parameters in my config/packages/doctrine.yaml I get an error on my next console command:
doctrine:
dbal:
driver: pdo_mysql
host: '%some.prefix.host%'
dbname: '%some.prefix.dbname%'
# ...
# ./bin/console debug:container --parameters
In ParameterBag.php line 98:
You have requested a non-existent parameter "some.prefix.host".
I am using Symfony 5.3 and Doctrine bundle 2.4.
Why do my parameters seem inaccessible for 3rd party bundle configuration ?
How can I make this work ?
Is there a better way to achieve this ?
I think the Doctrine bundle configuration gets processed before my compiler pass can declare the parameters. It probably can't be solved using the DependencyInjection component.
Solved it by importing a PHP configuration file in the services.yaml:
imports:
- { resource: my_custom_file.php }
With the following content:
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return function(ContainerConfigurator $configurator) {
// My specific logic
// Saving the configuration as parameters
$configurator->parameters()->set('some.prefix.host', $host);
$configurator->parameters()->set('some.prefix.dbname', $dbname);
// ...
};
I start new project and install fresh copy of Symfony 5 (microservice skeleton), and add first controller HealthCheckController to the default folder src/Controller, at this moment all is fine, I can get access to it from browser.
In next step I change a project name in composer.json and all related namespaces in code to
"autoload": {
"psr-4": {
"Project\\SubProject\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Project\\SubProject\\Tests\\": "tests/"
}
},
and in service.yaml
Project\SubProject\:
resource: '../src/'
exclude:
- '../src/DependencyInjection/'
- '../src/Entity/'
- '../src/Kernel.php'
- '../src/Tests/'
# controllers are imported separately to make sure services can be injected
# as action arguments even if you don't extend any base controller class
Project\SubProject\Controller: # assuming you have namespace like that
resource: '../src/Controller/'
tags: [ 'controller.service_arguments' ]
everything still works.
Next step is to change directory structure, add layers and modules. So I move Kernel.php to the Common/Infrastructure/Symfony/ (ofcourse I change path to config files in the kernel) and controller to folder Common/Interfaces/Controller and change configs in the service.yaml
Project\SubProject\:
resource: '../src/'
exclude:
- '../src/Common/Infrastructure/Symfony/DependencyInjection/'
- '../src/Common/Infrastructure/Symfony/Kernel.php'
- '../src/Module1/Infrastructure/Entity/'
- '../src/Module2/Infrastructure/Entity/'
- '../src/Module3/Infrastructure/Entity/'
- '../src/Module1/Test/'
- '../src/Module2/Test/'
- '../src/Module3/Test/'
# controllers are imported separately to make sure services can be injected
# as action arguments even if you don't extend any base controller class
Project\SubProject\Common\Interfaces\Controller\: # assuming you have namespace like that
resource: '../src/Common/Interfaces/Controller/'
tags: [ 'controller.service_arguments' ]
and in routes/annotation/yaml
controllers:
resource: ../../src/Common/Interfaces/Controller
type: annotation
kernel:
resource: ../../src/Common/Infrastructure/Symfony/Kernel.php
type: annotation
and now I'm getting error Project\SubProject\Common\Interfaces\Controller\HealthCheckController" has no container set, did you forget to define it as a service subscriber?
What I'm doing wrong, I forgot to change something???
I know you can tell me you need to add container to controller like this
Project\SubProject\Common\Interfaces\Controller\HealthCheckController:
calls:
- method: setContainer
arguments: ['#service_container']
but it's stupid to configure each controller manually when autowire feature turned on, and when it worked with a default directory structure.
Also I clear cache by CLI command and manually deleting folder.
It was somewhat difficult to follow exactly what your configuration files ended as. I don't have a specific answer for you but I was a bit intrigued at the notion of moving Kernel.php. You did not mention moving the config directory so I chose to leave it where it was and:
namespace Project\SubProject\Common\Infrastructure\Symfony;
class Kernel extends BaseKernel
{
protected function configureContainer(ContainerConfigurator $container): void
{
$base = $this->getProjectDir();
$container->import($base . '/config/{packages}/*.yaml');
$container->import($base . '/config/{packages}/'.$this->environment.'/*.yaml');
$container->import($base . '/config/services.yaml');
$container->import($base . '/config/{services}_'.$this->environment.'.yaml');
}
# same for configureRoutes
# project/config/services.yaml
services:
_defaults:
autowire: true
autoconfigure: true
Project\SubProject\:
resource: '../src/'
exclude:
- '../src/Common/Infrastructure/Symfony/Kernel.php'
Project\SubProject\Common\Interfaces\Controller\:
resource: '../src/Common/Interfaces/Controller/'
tags: ['controller.service_arguments']
# project/config/routes/annotations.yaml
controllers:
resource: ../../src/Common/Interfaces/Controller/
type: annotation
Tweaked index.php and console to use the new Kernel path and it all worked as expected.
I should point out that as long as you are extending from AbstractController then you don't actually need the Controller section in services.yaml at all. It's quite puzzling why you seem to be getting a controller service but setContainer is not being called.
bin/console debug:container HealthCheckController
Class Project\SubProject\Common\Interfaces\Controller\HealthCheckController
Tags controller.service_arguments
container.service_subscriber
Calls setContainer
The Calls setContainer is obviously the important line.
I'm guessing you do have a typo somewhere and I suspect you did not actually start your namespace with Project\SubProject. But again it does work as expected.
Just for my own reference I checked in my test project.
I want to use a controller as a service in symfony2. I defined the service in the test\testBundle\Resources\services.yml file:
parameters:
# user.example.class: test\testBundle\Example
services:
test.controller:
class: test\testBundle\Controller\TestController
and call the service in my controller:
$this->get('test.controller');
but symfony throws the following exception:
You have requested a non-existent service "test.controller".
You may have forgotten to import the services.yml file from a config file that's already being read, typically app/config/config.yml:
imports:
- { resource: '#MyBundle/Resources/config.yml' }
It is a convention to put config files in the Resources/config directory inside your bundle, cf. Importing Configuration with imports in the Symfony2 docs:
imports:
- { resource: '#MyBundle/Resources/config/config.yml' }
I have installed Symfony2, after fixing the file permissions, I can access the dev environment from my browser by pointing it to:
http://localhost/app_dev.php
However, when I try to access the production environment by pointing the browser to http://localhost, I get the following exception (from app/logs/prod.log):
[2012-08-13 11:30:03] request.ERROR:
Symfony\Component\HttpKernel\Exception\NotFoundHttpException: No route
found for "GET /" (uncaught exc eption) at
/path/to/frameworks/Symfony2/app/cache/prod/classes.php line 4584 []
[]
I then checked the available routes for the prod environment from the command line. here is the result of that investigation.
root#yourbox:~/path/to/frameworks/Symfony2$ php app/console
router:debug -e=prod [router] Current routes Name Method Pattern
Incredibly, it shows that there are no routes defined for this environment (I didn't believe the error message - which essentially said the same thing).
So, my conclusion is this: out of the box installation of Symfony2, and the production environment has no default routes - is this true, or have I made a mistake somewhere?
More importantly, how do I fix this?. In SF1.x, it was straight forward to switch from dev to prod and vice versa. How do I view the AcmeDemo app for example, in a prod environment. ?
[[UPDATE]]
After feedback from thecatontheflat, I added a simple test route to my routing.yml file. The contents of app/config/routing.yml are now:
_welcome2:
pattern: /test
defaults: { _controller: AcmeDemoBundle:Welcome:index }
When I try http://localhost/test in the browser, I get the same 404 error. When I debug the routes available at the console, I get the following output:
root#yourbox:~/path/to/frameworks/Symfony2$ php app/console router:debug -e=prod
[router] Current routes
Name Method Pattern
_welcome2 ANY /test
I had the exact same problem!!
Symfony2 has two generic routing files called:
app/config/routing.yml and app/config/routing_dev.yml
However, it also has a bundle specific routing file in each bundle. The standard procedure is to define all of your bundle routes in your bundle routing.yml file then reference this from the main routing.yml file by adding:
YourbundlenameMainBundle:
resource: "#YourbundlenameMainBundle/Resources/config/routing.yml"
prefix: /
This is what I had and I was still getting the error. But then I read the error more closely... no route found for GET / .... then I checked my routing_dev.yml file and of course it had a route for / from the Acme demo bundle _welcome route. This is why it works for the dev version but not the prod version!
Solution:
Add a route for / to either your routing.yml global file or your routing.yml bundle file by adding the following:
_welcome:
pattern: /
defaults: { _controller: YourbundlenameMainBundle:Default:index }
you can change index to some other route if your welcome page is not index
After you make sure you have your / route defined in any of your routing.yml
_welcome:
pattern: /
defaults: { _controller: YourbundlenameMainBundle:Default:index }
clear your prod environment cache!
app/console cache:clear --env=prod
I had the exact same problem. And it was caused because of a missing bundel in the AppKernel.php.
A little late but you might try to change this line in the web/app.php:
$kernel = new AppKernel('prod', false);
to:
$kernel = new AppKernel('prod', true);
That will enable debugging and you should be able to find your problem. When fixed you will ofcorse need to change it back to false ;).
Hope this helps.