'Symfony run hint kernel.secret parameter not found

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

index.php detail is:

<?php
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Debug\Debug;
use App\Kernel;

require './vendor/autoload.php';

// 开启调试
Debug::enable();

// 实例化请求
$request = Request::createFromGlobals();

// 实例化内核
$kernel = new Kernel('dev', true);

$response = $kernel->handle($request);

$response->send();

$kernel->terminate($request, $response);

app/Kernel.php detail is:

<?php
namespace App;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;

class Kernel extends BaseKernel{

    public function registerContainerConfiguration(LoaderInterface $loader)
    {
//        $confDir = $this->getProjectDir() . "/config";

//        $loader->load($confDir . "/app.php");
//        $this->set
    }

    public function registerBundles()
    {
        $contents = [
            \Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
        ];

        foreach ($contents as $class => $envs) {
            if (isset($envs["all"]) || isset($envs[$this->environment])) {
                yield new $class();
            }
        }
    }

    protected function configureContainer(ContainerBuilder $container) {
        // code invalid
        $container->setParameter("kernel.secret", "abck");
    }
}

composer.json detail is:

{
    "require": {
        "php": "^7.2.0",
        "symfony/http-foundation": "^4.1",
        "symfony/http-kernel": "^4.1",
        "symfony/config": "^4.1",
        "symfony/dependency-injection": "^4.1",
        "symfony/framework-bundle": "^4.1"
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/"
        }
    }
}

next run, show:

click to show image

I used the symonfy component one by one to install and then run, and found that the prompt could not find kernel.secret. I haven't found a solution for Google for a long time, and the online answer is to solve the existing framework of symfony. I hope to tell me how to solve this problem. Thank you



Solution 1:[1]

There is a new reason to get this error:

framework.secret used to be set in MicroKernelTrait.

Have a look at the commit [FrameworkBundle] Remove reference to APP_SECRET in MicroKernelTrait in which this line has been removed:

$container->loadFromExtension('framework', [
   'secret' => '%env(APP_SECRET)%'   //      <== removed
   ...

Solution:

Set the config by yourself:

# config/packages/framework.yaml

framework:
    secret: '%env(APP_SECRET)%'

Solution 2:[2]

Thank you for Cerad.

I found that configureContainer is not a method already defined by the parent Kernel, so this method is not called.

So I changed the configureContainer to build and it was ok.

code show as below?

app/kernel.php code is :

<?php
namespace App;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;

class Kernel extends BaseKernel{

    public function registerContainerConfiguration(LoaderInterface $loader)
    {
    }

    public function registerBundles()
    {
        $contents = [
            \Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
        ];

        foreach ($contents as $class => $envs) {
            if (isset($envs["all"]) || isset($envs[$this->environment])) {
                yield new $class();
            }
        }
    }

    // Modify location
    public function build(ContainerBuilder $container)
    {
        parent::build($container); // TODO: Change the autogenerated stub
        $container->setParameter("kernel.secret", "abck");
    }
}

Solution 3:[3]

None of the answers worked for me on Symfony 6, I just needed a simple Kernel to test my bundle, and I needed to use the SymfonyFrameworkBundle in that Kernel, which was complaining about kernel.secret not being set because I had no env or config (it's a test micro kernel after all)

So I tried the answers here but it was complaining about the parameter bag being frozen, this is what worked in my case:

class Kernel extends BaseKernel {
//...
    protected function getKernelParameters(): array
    {
        return parent::getKernelParameters() + ['kernel.secret' => '$3cret'];
    }
}

Solution 4:[4]

I had this same problem. It was caused by the path of my symfony project: it was long and contained spaces and square brackets.

I changed it to a shorter, safer one and it works.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 jizhidemowangjuanzi
Solution 3
Solution 4 Alfonso Jiménez