找不到Symfony运行提示kernel.secret参数



错误消息:服务"uri_signer"依赖于一个不存在的参数"kernel.secret"。你的意思是:"kernel.charset"吗?

index.php详细信息为:

<?php
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentDebugDebug;
use AppKernel;
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详细信息为:

<?php
namespace App;
use SymfonyComponentConfigLoaderLoaderInterface;
use SymfonyComponentDependencyInjectionContainerBuilder;
use SymfonyComponentHttpKernelKernel 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 = [
SymfonyBundleFrameworkBundleFrameworkBundle::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的详细信息是:

{
"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/"
}
}
}

下一次运行,显示:

单击以显示图像

我用symfony组件一个接一个地安装然后运行,发现提示找不到kernel.securt。我已经很久没有找到谷歌的解决方案了,在线的答案是解决symfony现有的框架。我希望告诉我如何解决这个问题。谢谢

出现此错误的新原因是:

framework.secret用于MicroKernelTrait中设置。

查看MicroKernelTrait中对APP_SECRET的commit[FrameworkBundle]Remove引用,其中删除了此行:

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

解决方案:

自行设置配置:

# config/packages/framework.yaml
framework:
secret: '%env(APP_SECRET)%'

在Symfony 6上,所有答案都不适用,我只需要一个简单的内核来测试我的捆绑包,我需要在该内核中使用SymfonyFrameworkBundle,它抱怨kernel.secret没有设置,因为我没有env或config(毕竟是一个测试微内核(

所以我在这里尝试了答案,但它抱怨参数包被冻结,这就是我的情况:

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

感谢Cerad。

我发现configureContainer不是父Kernel已经定义的方法,所以没有调用这个方法。

所以我改变了配置容器来构建,它是可以的。

代码显示如下:

app/kernel.php代码为:

<?php
namespace App;
use SymfonyComponentConfigLoaderLoaderInterface;
use SymfonyComponentDependencyInjectionContainerBuilder;
use SymfonyComponentHttpKernelKernel as BaseKernel;
class Kernel extends BaseKernel{
public function registerContainerConfiguration(LoaderInterface $loader)
{
}
public function registerBundles()
{
$contents = [
SymfonyBundleFrameworkBundleFrameworkBundle::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");
}
}

我也遇到了同样的问题。它是由我的symfony项目的路径引起的:它很长,包含空格和方括号。

我把它换了一个更短、更安全的,它很管用。

最新更新