我在symfony中的第一个控制器 [英] My first controller in symfony

查看:55
本文介绍了我在symfony中的第一个控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 //src/AppBundle/Controller/LuckyController.php命名空间AppBundle \ Controller;使用Symfony \ Bundle \ FrameworkBundle \ Controller \ Controller;使用Sensio \ Bundle \ FrameworkExtraBundle \ Configuration \ Route;使用Symfony \ Component \ HttpFoundation \ Response;类LuckyController扩展Controller{/*** @Route("/lucky/number")*/公共函数numberAction(){$ number = rand(0,100);返回新的响应('< html>< body>幸运数字:'.$ number.'</body></html>');}} 

我是Symfony框架的新手.我尝试了这个简单的代码,没有任何结果.那是服务器的响应:

找不到"GET/lucky/number"的路由404找不到-NotFoundHttpException1个链接的异常:ResourceNotFoundException»

我现在不解释为什么Default控制器使用注释,并且可以看到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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆