Home > Back-end >  Symfony 5.4 routes by annotations not working
Symfony 5.4 routes by annotations not working

Time:01-24

I know this question has been discussed million times here, but it is the third day I keep on struggling on this issue.

I've got symfony application been developed on my local PC (Windows). Now I'm trying to deploy it on my linux web-hosting (Linux).

My routes are initted in controllers' classes via annotations.

<?php

namespace App\Controller;

use App\Entity\Category;
use App\Repository\CategoryRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class IndexController extends AbstractController
{
    /**
     * @Route("/", name="app_index")
     */
    public function index(): Response
    {
        return $this->render('index/index.twig', [
            'controller_name' => 'IndexController'
        ]);
    }
}

The issue is these routes are not included by some reason, which leads to 404 error

/config/services.yaml

services:
  _defaults:
    autowire: true
    autoconfigure: true

  App\:
    resource: '../src/'
    exclude:
      - '../src/DependencyInjection/'
      - '../src/Entity/'
      - '../src/Kernel.php'
...

/config/routes/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 is empty

symfony console debug:router shows only admin and profiler routes but not mine initted in controllers

I would be very grateful if you could help me solving this issue.

If I describe the index route in routes.yaml (which I don't like.. What are the annotations then for?)

index:
    path: /
    controller: App\Controller\IndexController::index

symfony goes wild "App\Controller\IndexController" has no container set, did you forget to define it as a service subscriber?

All I've done during deployment was:

  1. clone git repo with my application on the production server
  2. installed composer dependencies
  3. migrated database structure
  4. updated env with new db connection

CodePudding user response:

I hope auto wire is enabled in 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.

And please comment code in routes.yaml And try with php bin/console cache:clear command.

CodePudding user response:

try with PHP 8 attributes

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

function:

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

Documentation:

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

  • Related