简体   繁体   English

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

[英]Symfony: NotFoundHttpException: No route found for “GET /lucky/number”

I'm newbi with symfony, i'm trying to add a new page to the demo application, I'm following this tuto 我是symfony的新手,我正在尝试向演示应用程序添加新页面,我正在关注此教程

I added this controller src/AppBundle/Controller/LuckyController.php 我添加了此控制器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}

When i navigate to http://127.0.0.1:8000/app_dev.php/lucky/number , i got this error 当我导航到http://127.0.0.1:8000/app_dev.php/lucky/number时 ,出现此错误

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

My routing.yml 我的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%"

Thanks. 谢谢。

You should remove part with locale in your route-prefix if you don't want to manage it: 如果不想管理,请在路由前缀中删除带有locale部分:

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

Your routing file must look like this: 您的路由文件必须如下所示:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM