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



我是symfony的新手,我正在尝试向演示应用程序添加一个新页面,我在关注这个芭蕾舞团

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

// src/AppBundle/Controller/LuckyController.php
namespace AppBundleController;
use SymfonyBundleFrameworkBundleControllerController;
use SensioBundleFrameworkExtraBundleConfigurationRoute;
use SymfonyComponentHttpFoundationResponse;
class LuckyController extends Controller
{
/**
 * @Route("/lucky/number")
 */
 public function numberAction()
 {
    $number = rand(0, 100);
    return new Response(
        '<html><body>Lucky number: '.$number.'</body></html>'
    );
  }
}

应用程序/控制台路由器:调试返回:

 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"

我的路线。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%"

谢谢。

如果您不想管理路由前缀中带有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'

最新更新