我使用以下步骤安装了Symfony4。
step1:composer create-project“Symfony / skeleton:^ 4.0”symfony4 step2:git status step3:git add。step4:git commit step5:composer require annotations step6:创建一个名为ArticleController的控制器
<?php
namespace App\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Response;
class ArticleController
{
/**
* @Route("/")
*/
public function indexAction()
{
return new Response('OMG! My first page already! Wooooo!');
}
/**
* @Route("/{id}", requirements={"id" = "\d+"}, defaults={"id" = 1})
*/
public function showAction($id)
{
echo 123;die;
}
/**
* @Route("/news/{$slug}")
* @Method({"GET", "POST"})
*/
public function news($slug)
{
return new Response(sprintf('Today new is "%s"', $slug));
}
}
Step7:访问http://127.0.0.1:8000你可以查看'OMG!我的第一页已经! WOOOOO!”。
但http://127.0.0.1:8000/123和http://127.0.0.1:8000/news/test不起作用。谁能告诉我为什么?请帮我修理一下。
找到解决方案。
composer需要symfony / apache-pack
它会自动生成.htaccess。
正确的命名空间是:
use Symfony\Component\Routing\Annotation\Route;
谢谢