2017-09-15 101 views
0

我正在关注symfony上的openclassrooms教程。我现在正在阅读“Symfony的控制者”一章。Symfony Component Routing Exception InvalidParameterException

我尝试打开http://localhost/Symfony/web/app_dev.php/platform,我得到这个错误

InvalidParameterException

这里是AdvertController.php代码:

<?php 
//src/Neo/PlatformBundle/Controller/AdvertController.php 
namespace Neo\PlatformBundle\Controller; 
use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\HttpFoundation\Response; 
//use Symfony\Component\Routing\Generator\UrlGeneratorInterface; 
class AdvertController extends Controller 
{ 
    public function indexAction() 
    { 
     $url= $this->get('router')->generate(
     'neo_platform_view', //first argument : path name 
     array('id' => 5) 
    ); 
     return new Response("The url of the announcement is:".$url); 
    } 
public function viewAction($id) 
{ 
    return new Response("Desplay of the announcment with id:".$id); 
} 
public function viewSlugAction($slug, $year, $_format) 

     { 
      return new Response(

       "We could desplay the announcment conrresponding the the slug '".$slug."', created in ".$year." and with the format ".$_format."." 
     ); 
     } 
} 
?> 

我不明白的错误意味着什么,不知道如何解决它。 谢谢你的帮助!

+0

你能从你的routing.yml中显示'neo_platform_view'的路由定义吗? – Joe

+0

neo_platform_view: 路径:/广告/ {ID} 默认: _controller:NeoPlatformBundle:广告:鉴于 要求: ID:\ ID + –

+0

你或许应该开始另一个之前完成一个问题。我可能会补充说,如果你在教程中遇到这么多麻烦,那么你可能会尝试其他的东西。像文档一样?当你添加诸如路由定义之类的东西时更新你的问题。评论格式不好。 – Cerad

回答

0
neo_platform_view: 
    path: /advert/{id} 
    defaults: 
     _controller: NeoPlatformBundle:Advert:view 
    requirements: 
     id: \d+ 

变化\ ID +到\ d + 这应该修复它。如果你想只允许数字\ d +是正确的条件。

https://symfony.com/doc/current/routing/requirements.html欲了解更多关于要求的信息。

+0

乔,太棒了!就是这样!非常感谢你! –

相关问题