我试图通过权利(我正在研究示例)为db添加一些值。 我创建了一个位于DefaultController中的createAction的本地路由。但是,当我尝试去页面时,它说没有路由器。
这是我的示例代码。我怎么能解决这个问题。谢谢你的帮助。
/*Local Routes:
acme_store_homepage:
pattern: /hello/{name}
defaults: { _controller: AcmeStoreBundle:Default:index }
acme_store_product:
pattern: /add
defaults: { _controller: AcmeStoreBundle:Default:create }
*/
/* app Routes:
acme_store:
resource: "@AcmeStoreBundle/Resources/config/routing.yml"
prefix: /
*/
/*
Default Controller
*/
<?php
namespace Acme\StoreBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Acme\StoreBundle\Entity\Product;
use Symfony\Component\HttpFoundation\Response;
class DefaultController extends Controller
{
public function indexAction($name)
{
return $this->render('AcmeStoreBundle:Default:index.html.twig', array('name' => $name));
}
public function createAction()
{
$product = new Product();
$product->setName("SomeProduct");
$product->setPrice("1000");
$product->setDescription("Bla bla bla.");
$em = $this->getDoctrine()->getManager();
$em->persist($product);
$em->flush();
return new Response('Product id: '.$product->getId());
}
}
//Running http://localhost/Symfony/web/app_dev.php/demo/add is throwing an exception: No route found for "GET /demo/add"
//Running http://localhost/Symfony/web/app_dev.php/demo/hello/World is Fine...
答案 0 :(得分:1)
这里
acme_store:
resource: "@AcmeStoreBundle/Resources/config/routing.yml"
prefix: /
您尚未在
中定义前缀acme_store_product:
pattern: /add
defaults: { _controller: AcmeStoreBundle:Default:create }
您仅定义:
pattern: /add
尝试:localhost/Symfony/web/app_dev.php/add
如果你想通过/ demo / add和/ demo / hello / world add
来访问它prefix:/demo