将 Symfony 从 5.4 更新到 6.4 后,routing.yml 停止工作

问题描述 投票:0回答:1

我有一个项目遗留问题,自 symfony 2 以来一直在更新。现在我的版本是 6.4,但是我的路线停止工作了。

在我的routing.yml 中,我试图修复Admin 文件夹中的路由。 按照文档https://symfony.com/doc/6.4/routing/custom_route_loader.html 但不起作用。

我已经尝试过:

app_admin:
  resource: ../../src/AppBundle/Controller/Admin/
  type: attribute

错误是:

In FileLoader.php line 182:
                                                                                                                                                                                                                       
  Warning: Array to string conversion in app/config/../../src/AppBundle/Controller/Admin/ (which is being imported from app/config/routing.yml"). Make sure there is a loader supporting the "attribute" type.                                                                                                                         
                                                                                                                                                                                                                       

In AttributeClassLoader.php line 219:
                                       
  Warning: Array to string conversion  
                                       

我也尝试过:

app_admin:
  # loads routes from the PHP attributes of the controllers found in the given PSR-4 namespace root
  resource:
    path: '../../src/AppBundle/Controller/Admin/'
    namespace: App\Controller\Admin
  type: attribute

但是 $ php bin/console debug:router 不加载任何路线。

如果我更改routing.yml以添加每个类,它可以工作,但我认为这不是最好的方法。添加每个类将会很痛苦。

管理控制器

<?php

namespace AppBundle\Controller\Admin;


use AppBundle\Service\SmsMailer;
use AppBundle\Entity\CletClient;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Notifier\TexterInterface;
use Symfony\Component\Routing\Annotation\Route;

class AdminController extends AbstractController
{

    #[Route(path: '/admin/', name: 'admin')]
    public function showAction(){

        return $this->render("@App/admin/show.html.twig", array(
            '' => '',
        ));
    }

    /**
     * @Route("/admin/sales-chart/", options={"expose"=true},name="sales_chart_data")
     */
    public function getSalesChartDataAction(){


        $json = '{}';
        $result = json_decode ($json);
        return new Response(json_encode($result));

    }

    /**
     * @Route("/file_manager/", name="file_manager")
     */
    public function fileManager(){

        return $this->render("@App/admin/blank.html.twig", array(
        ));
    }

    /**
     * @Route("/2fa_email/", name="two_factor_email")
     */
    public function twoFactorEmail(MailerInterface $mailer, TexterInterface $texter){

        return $this->redirectToRoute("2fa_login");
    }
}

有人知道我错过了什么吗?

php symfony routes
1个回答
0
投票

我遇到了同样的问题。对我来说,解决方案是将

Kernel.php
文件夹中的 Symfony 5.4
src
文件替换为新的 Symfony 6.4 安装之一:

只需运行以下命令:

composer create-project symfony/skeleton:"6.4.*" symfony6

然后您应该在

src
文件夹中找到一个 Kernel.php 文件,如下所示:

<?php

namespace App;

use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;

class Kernel extends BaseKernel
{
    use MicroKernelTrait;
}

另外,我对此也不是 100% 确定,因为我在 Symfony 文档中找不到任何确认,但我认为旧的 Routes 注释不再受支持,应该使用本机 PHP 8 属性表示法,就像您开始做的那样所以使用

admin
路线。

希望有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.