简体   繁体   中英

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

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)%'

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");
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM