Symfony @Route注解教程展示了如何在 Symfony 中使用@Route注解创建路由。
Symfony
Symfony 是一组可重用的 PHP 组件和一个用于 Web 项目的 PHP 框架。 Symfony 于 2005 年发布为免费软件。Fabien Potencier 是 Symfony 的原始作者。 Symfony 受到 Spring 框架的极大启发。
@Route注解
路由是从 URL 路径到控制器的映射。 例如,/about URL 映射到MyController的about()方法。
@Route注解用于创建路径。 其他选项是 XML 和 YAML 配置文件以及 PHP 代码。 该注解用于文档字符串中。
Symfony @Route示例
在下面的示例中,我们使用@Route的各种选项。
$ composer create-project symfony/skeleton routeanno$ cd routeanno
使用composer,我们创建一个新的 Symfony 骨架项目。 我们导航到项目目录。
$ composer require maker$ composer require annotations
我们安装了两个模块:annotations和maker。 @Route在annotations模块中定义。
$ composer require server --dev
我们安装开发 Web 服务器。
$ php bin/console make:controller MyController
创建了MyController。
src/Controller/MyController.php
<?phpnamespace App\Controller;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\Routing\Annotation\Route;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;class MyController extends AbstractController{/*** @Route("/home")*/public function home(){return new Response("home", Response::HTTP_OK,['content-type' => 'text/plain']);}/*** @Route("/about", methods={"GET", "POST"})*/public function about(Request $request){$method = $request->getRealMethod();$msg = "about: " . $method;return new Response($msg, Response::HTTP_OK,['content-type' => 'text/plain']);}/*** @Route("/news/{id}", requirements={"page"="\d+"})*/public function news($id){$msg = 'News ' . $id;return new Response($msg, Response::HTTP_OK,['content-type' => 'text/plain']);}}
MyController具有使用@Route创建的三个路由。
/*** @Route("/home")*/public function home(){return new Response("home", Response::HTTP_OK,['content-type' => 'text/plain']);}
在这里,我们将/home路径映射到home()方法。
/*** @Route("/about", methods={"GET", "POST"})*/public function about(Request $request){$method = $request->getRealMethod();$msg = "about: " . $method;return new Response($msg, Response::HTTP_OK,['content-type' => 'text/plain']);}
使用methods选项,我们可以将请求限制为指定的方法类型。 在我们的例子中,仅针对 GET 和 POST 请求才调用about()方法。
/*** @Route("/news/{id}", requirements={"page"="\d+"})*/public function news($id){$msg = 'News ' . $id;return new Response($msg, Response::HTTP_OK,['content-type' => 'text/plain']);}
使用requirements选项,我们为 URL 路径指定允许的字符。 {id}是整数值的占位符。
也可以将注解放置在控制器类上。 这用作所有路由路径的前缀。
$ php bin/console make:controller TestController
我们创建一个新的控制器。
src/Controller/TestController.php
<?phpnamespace App\Controller;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\Routing\Annotation\Route;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;/*** @Route("/test")*/class TestController extends AbstractController{/*** @Route("/car")*/public function car(){$msg = 'Testing car';return new Response($msg, Response::HTTP_OK,['content-type' => 'text/plain']);}/*** @Route("/book")*/public function book(){$msg = 'Testing book';return new Response($msg, Response::HTTP_OK,['content-type' => 'text/plain']);}}
TestController带有@Route("/test")注解。 因此,URL 路径将为/test/car和/test/book。
$ php bin/console debug:router--------------- ---------- -------- ------ ------------Name Method Scheme Host Path--------------- ---------- -------- ------ ------------app_my_home ANY ANY ANY /homeapp_my_about GET|POST ANY ANY /aboutapp_my_news ANY ANY ANY /news/{id}app_test_car ANY ANY ANY /test/carapp_test_book ANY ANY ANY /test/book--------------- ---------- -------- ------ ------------
我们可以使用bin/console debug:router命令列出创建的路由。
运行示例
我们启动服务器并使用curl工具测试创建的路由。
$ php bin/console server:run
我们启动开发服务器。
$ curl localhost:8000/homehome$ curl -X POST localhost:8000/aboutabout: POST$ curl localhost:8000/news/34News 34$ curl localhost:8000/test/carTesting car$ curl localhost:8000/test/bookTesting book
我们使用curl生成请求。
In this tutorial we have created routes in Symfony using @Route annotation.
您可能也对以下相关教程感兴趣: Symfony 简介, Symfony 创建路由, Symfony 表单教程, PHP 教程。
