Symfony 5.4通过注释路由不工作



我知道这个问题在这里已经讨论过无数次了,但这是我在这个问题上挣扎的第三天了。

我已经在我的本地PC (Windows)上开发了symfony应用程序。现在我正试图将它部署在我的linux网络托管(linux)上。

我的路由是通过注解在控制器的类中初始化的。

<?php
namespace AppController;
use AppEntityCategory;
use AppRepositoryCategoryRepository;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentRoutingAnnotationRoute;
class IndexController extends AbstractController
{
/**
* @Route("/", name="app_index")
*/
public function index(): Response
{
return $this->render('index/index.twig', [
'controller_name' => 'IndexController'
]);
}
}

问题是由于某些原因这些路由没有被包含,这导致404错误

/config/services.yaml

services:
_defaults:
autowire: true
autoconfigure: true
App:
resource: '../src/'
exclude:
- '../src/DependencyInjection/'
- '../src/Entity/'
- '../src/Kernel.php'
...

/config/线路/annotations.yaml

controllers:
resource: ../../src/Controller/
type: annotation
prefix:
ru: '' # don't prefix URLs, the default locale
en: '/en'
kernel:
resource: ../../src/Kernel.php
type: annotation

/config/routes.yaml是空的

symfony console debug:router只显示admin分析器和在控制器中启动路由但不包括mine

如果你能帮我解决这个问题,我将非常感激。

如果我在routes.yaml中描述索引路由我不喜欢……那么注释是用来干什么的呢?)
index:
path: /
controller: AppControllerIndexController::index

symfony go wild"AppControllerIndexController" has no container set, did you forget to define it as a service subscriber?

我在部署期间所做的一切是:

  1. 将我的应用克隆到生产服务器上
  2. 已安装的编译器依赖项
  3. 已迁移数据库结构
  4. 用新的数据库连接更新了环境

尝试使用PHP 8属性

#[Route('/', name: 'app_index')]

功能:

#[Route('/', name: 'app_index')] 
public function index(): Response {
return $this->render('index/index.twig');
}

文档:https://www.php.net/manual/fr/language.attributes.overview.php

我希望在service.yaml中启用自动连接

services:
# default configuration for services in *this* file
_defaults:
autowire: true      # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.

请在routes.yaml中注释代码并尝试使用php bin/console cache:clear命令。

您必须要求:

composer require symfony/apache-pack

之后,只是改变FollowSymLinks到SymLinksIfOwnerMatch…和"vu &;…

我创建了一个新的Symfony 5.4项目:

symfony new --webapp --version=lts foo

安装PHP 8.2。

但是注解(PHP属性格式)不起作用。也就是说,我的新路由在发布php bin/console debug:route时不会出现。

但是下面的命令修复了它:

composer require annotations

最新更新