Symfony:NotFoundHttpException:找不到“GET / lucky / number”的路由

时间:2015-11-30 12:13:40

标签: php symfony routing

我是symfony的newbi,我正在尝试向演示应用程序添加新页面, 我正在关注此tuto

我添加了这个控制器src / AppBundle / Controller / LuckyController.php

// 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>'
    );
  }
}

app / console router:debug return:

 Name                     Method   Scheme Host Path

 _wdt                     ANY      ANY    ANY  /_wdt/{token}

 _profiler_home           ANY      ANY    ANY  /_profiler/

_profiler_search         ANY      ANY    ANY  /_profiler/search

_profiler_search_bar     ANY      ANY    ANY  /_profiler/search_bar

_profiler_purge          ANY      ANY    ANY  /_profiler/purge

_profiler_info           ANY      ANY    ANY  /_profiler/info/{about}

_profiler_phpinfo        ANY      ANY    ANY  /_profiler/phpinfo

_profiler_search_results ANY      ANY    ANY   /_profiler/{token}/search/results

_profiler                ANY      ANY    ANY  /_profiler/{token}

_profiler_router         ANY      ANY    ANY  /_profiler/{token}/router

_profiler_exception      ANY      ANY    ANY  /_profiler/{token}/exception

_profiler_exception_css  ANY      ANY    ANY  /_profiler/{token}/exception.css

admin_index              GET      ANY    ANY  /{_locale}/admin/post/

admin_post_index         GET      ANY    ANY  /{_locale}/admin/post/

admin_post_new           GET|POST ANY    ANY  /{_locale}/admin/post/new

admin_post_show          GET      ANY    ANY  /{_locale}/admin/post/{id}

admin_post_edit          GET|POST ANY    ANY  /{_locale}/admin/post/{id}/edit

admin_post_delete        DELETE   ANY    ANY  /{_locale}/admin/post/{id}

blog_index               ANY      ANY    ANY  /{_locale}/blog/

blog_index_paginated     ANY      ANY    ANY  /{_locale}/blog/page/{page}

blog_post                ANY      ANY    ANY  /{_locale}/blog/posts/{slug}

comment_new              POST     ANY    ANY    /{_locale}/blog/comment/{postSlug
}/new
app_lucky_number         ANY      ANY    ANY  /{_locale}/lucky/number

security_login_form      ANY      ANY    ANY  /{_locale}/login

security_login_check     ANY      ANY    ANY  /{_locale}/login_check

security_logout          ANY      ANY    ANY  /{_locale}/logout

homepage                 ANY      ANY    ANY  /{_locale}

当我导航到http://127.0.0.1:8000/app_dev.php/lucky/number时,我收到了此错误

NotFoundHttpException: No route found for "GET /lucky/number"

我的routing.yml

app:
resource: @AppBundle/Controller/
type:     annotation
prefix:   /{_locale}
requirements:
    _locale: %app_locales%
defaults:
    _locale: %locale%

# These lines define a route using YAML configuration. The controller used by
# the route (FrameworkBundle:Template:template) is a convenient shortcut when
# the template can be rendered without executing any logic in your own   controller.
# See   http://symfony.com/doc/current/cookbook/templating/render_without_controller.html
homepage:
path: /{_locale}
requirements:
    _locale: %app_locales%
defaults:
    _controller: FrameworkBundle:Template:template
    template:    'default/homepage.html.twig'
    _locale:     "%locale%"

感谢。

1 个答案:

答案 0 :(得分:1)

如果您不想管理,请在路径前缀中删除locale的部分:

prefix:   /{_locale}
requirements:
    _locale: %app_locales%
defaults:
    _locale: %locale%

您的路由文件必须如下所示:

app:
    resource: @AppBundle/Controller/
    type:     annotation

homepage:
    path: /
    defaults:
        _controller: FrameworkBundle:Template:template
        template:    'default/homepage.html.twig'