我在 symfony 中的第一个控制器 [英] My first controller in symfony
问题描述
//src/AppBundle/Controller/LuckyController.php命名空间 AppBundle\Controller;使用 Symfony\Bundle\FrameworkBundle\Controller\Controller;使用 Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;使用 Symfony\Component\HttpFoundation\Response;类 LuckyController 扩展控制器{/*** @Route("/幸运/号码")*/公共函数 numberAction(){$number = rand(0, 100);返回新的响应('<html><body>幸运数字:'.$number.'</body></html>');}}
我是 Symfony 框架的新手.我尝试了这个简单的代码,但没有任何结果.这是服务器响应:
<块引用>没有找到GET/lucky/number"的路由 404 Not Found - NotFoundHttpException1 个链接异常:ResourceNotFoundException »
我现在不知道为什么默认控制器使用注释,我可以看到我的 Symfony 应用程序的主页.
我认为您需要导入注释路线.Symfony 将扫描您在 app/config/routing.yml
中提到的位置.
# app/config/routing.yml幸运的:资源:@AppBundle/Controller/LuckyController.php"类型:注释
请参阅 Symfony 文档了解更多详情>
也尝试清除缓存,只是为了确定.
// src/AppBundle/Controller/LuckyController.php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
class LuckyController extends Controller
{
/**
* @Route("/lucky/number")
*/
public function numberAction()
{
$number = rand(0, 100);
return new Response(
'<html><body>Lucky number: '.$number.'</body></html>'
);
}
}
I'm new of Symfony framework. I tried this simple code without any result. That's the server response:
No route found for "GET /lucky/number" 404 Not Found - NotFoundHttpException 1 linked Exception: ResourceNotFoundException »
I don't now why the Default controller use the annotation and I can see the homepage of my Symfony application.
I think you need to import your annotation routes. Symfony will scan locations you mention in app/config/routing.yml
.
# app/config/routing.yml
lucky:
resource: "@AppBundle/Controller/LuckyController.php"
type: annotation
See Symfony docs for more details
Also try clearing the cache, just to be sure.
这篇关于我在 symfony 中的第一个控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!