带有注释的路由在产品 Symfony4 中不起作用
Route with annotations not working in prod Symfony4
当我在 SF4 应用程序中为我的路由使用注释时,只有主页在工作。
为了测试,我刚刚创建了 1 个控制器:
<?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.');
}
}
唯一有效的是http://mysite/
当使用 /first、/second 或 /third 时,我收到 404。现在我像这样更改最后一个函数:
/**
* @Route("/third", name="third")
*/
public function third()
{
return new Response('Welcome to your third.');
}
结果连主页都没有了,现在是空白页
当我 运行 在开发中时,一切正常,我收到 3 页...
所以,我的结论是:只有名为 homepage() 的函数在工作,其他的都没有。
我做错了什么?我是不是忘记了什么?
更新:
一些调试信息:
当我没有 homepage() 函数时错误消息的第一部分:
(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)
和第二部分:
(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)
检查您是否在 config/routes/annotations.yaml
:
controllers:
resource: ../../src/Controller/
type: annotation
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
在文档中我们发现我们需要将一个.htaccess 文件添加到public 文件夹中。成功了!
当我在 SF4 应用程序中为我的路由使用注释时,只有主页在工作。 为了测试,我刚刚创建了 1 个控制器:
<?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.');
}
}
唯一有效的是http://mysite/
当使用 /first、/second 或 /third 时,我收到 404。现在我像这样更改最后一个函数:
/**
* @Route("/third", name="third")
*/
public function third()
{
return new Response('Welcome to your third.');
}
结果连主页都没有了,现在是空白页
当我 运行 在开发中时,一切正常,我收到 3 页...
所以,我的结论是:只有名为 homepage() 的函数在工作,其他的都没有。
我做错了什么?我是不是忘记了什么?
更新: 一些调试信息:
当我没有 homepage() 函数时错误消息的第一部分:
(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)
和第二部分:
(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)
检查您是否在 config/routes/annotations.yaml
:
controllers:
resource: ../../src/Controller/
type: annotation
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
在文档中我们发现我们需要将一个.htaccess 文件添加到public 文件夹中。成功了!