Jan Willems
Jan Willems

Reputation: 61

Route with annotations not working in prod Symfony4

When I'm using annotations for my routing in a SF4 app, only homepage is working. For testing I've just created 1 controller:

<?php

namespace App\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;

class MainController extends Controller
{
    /**
     * @Route("/first", name="first")
     */
    public function first()
    {
        return new Response('Welcome to your first.');
    }

    /**
     * @Route("/second", name="second")
     */
    public function second()
    {
        return new Response('Welcome to your second.');
    }

    /**
     * @Route("/third", name="third")
     */
    public function homepage()
    {
        return new Response('Welcome to your third.');
    }
}

The only thing that is working is http://mysite/

When using /first, /second or /third, I receive a 404. Now I change the last function like this:

/**
 * @Route("/third", name="third")
 */
public function third()
{
    return new Response('Welcome to your third.');
}

The result is that even the homepage doesn't appear anymore, it's a blank page now.

When I run this in development, everything works as normal, I receive 3 pages...

So, my conclusion: only the function called homepage() is working, all the others aren't.

What am I doing wrong? Am I forgetting something?

UPDATE: Some debug info:

First part of error message when I have no homepage() function:

(1/2) ResourceNotFoundException
in srcProdDebugProjectContainerUrlMatcher.php (line 48)
at srcProdDebugProjectContainerUrlMatcher->match('/')
in UrlMatcher.php (line 95)
at UrlMatcher->matchRequest(object(Request))
in Router.php (line 262)
at Router->matchRequest(object(Request))
in RouterListener.php (line 114)
at RouterListener->onKernelRequest(object(GetResponseEvent), 'kernel.request', object(EventDispatcher))
in EventDispatcher.php (line 212)
at EventDispatcher->doDispatch(array(array(object(DebugHandlersListener), 'configure'), array(object(ValidateRequestListener), 'onKernelRequest'), array(object(SessionListener), 'onKernelRequest'), array(object(RouterListener), 'onKernelRequest'), array(object(ResolveControllerNameSubscriber), 'onKernelRequest'), array(object(LocaleListener), 'onKernelRequest')), 'kernel.request', object(GetResponseEvent))
in EventDispatcher.php (line 44)
at EventDispatcher->dispatch('kernel.request', object(GetResponseEvent))
in HttpKernel.php (line 125)
at HttpKernel->handleRaw(object(Request), 1)
in HttpKernel.php (line 66)
at HttpKernel->handle(object(Request), 1, true)
in Kernel.php (line 190)
at Kernel->handle(object(Request))
in index.php (line 37)

and the second part:

(2/2) NotFoundHttpException
No route found for "GET /"
in RouterListener.php (line 144)
at RouterListener->onKernelRequest(object(GetResponseEvent), 'kernel.request', object(EventDispatcher))
in EventDispatcher.php (line 212)
at EventDispatcher->doDispatch(array(array(object(DebugHandlersListener), 'configure'), array(object(ValidateRequestListener), 'onKernelRequest'), array(object(SessionListener), 'onKernelRequest'), array(object(RouterListener), 'onKernelRequest'), array(object(ResolveControllerNameSubscriber), 'onKernelRequest'), array(object(LocaleListener), 'onKernelRequest')), 'kernel.request', object(GetResponseEvent))
in EventDispatcher.php (line 44)
at EventDispatcher->dispatch('kernel.request', object(GetResponseEvent))
in HttpKernel.php (line 125)
at HttpKernel->handleRaw(object(Request), 1)
in HttpKernel.php (line 66)
at HttpKernel->handle(object(Request), 1, true)
in Kernel.php (line 190)
at Kernel->handle(object(Request))
in index.php (line 37)

Upvotes: 0

Views: 3251

Answers (2)

Michał G
Michał G

Reputation: 2302

Check if you have in config/routes/annotations.yaml:

controllers:
    resource: ../../src/Controller/
    type: annotation

Upvotes: 1

Jan Willems
Jan Willems

Reputation: 61

Lets start with the basics... Does your vhost looks like the optimized configuration shown in the doc? Also, run php bin/console debug:router and check if you can find your routes in the result

In the doc we find that we need to add a .htaccess file to the public folder. That did the trick!

Upvotes: 3

Related Questions