2017-09-15 110 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

喬,太棒了!就是這樣!非常感謝你! –

相關問題