路由及路由规则,请求方式

  1. 作者QQ:67065435 QQ群:821635552

  2. 本站内容全部为作者原创,转载请注明出处!

  3. 路由文件指定

    # [项目路径]/app/config/routing.yml
    
    # 路由规则绑定注释
    xxx:
        resource: "@XxxBundle/Controller/"
        type:     annotation
        prefix:   /[整个XxxBundle所有控制器方法路由前缀]
    
    # 路由规则绑定yml
    xxx:
        resource: "@XxxBundle/Resources/config/routing.yml"
        prefix:   /[整个XxxBundle所有控制器方法路由前缀]
    
    # 路由规则绑定xml
    xxx:
        resource: "@XxxBundle/Resources/config/routing.xml"
        prefix:   /[整个XxxBundle所有控制器方法路由前缀]
    
  4. annotation

    # [项目路径]/src/XxxBundle/Controller/XxxController.php
    # Route用于指定路由名、路由规则,Method用于限定允许请求的方式
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
    
    class XxxController extends Controller
    {
        /**
         * 路由("/路由路径/{参数1}/{参数2}.{参数3}", name="路由别名", defaults={"参数1": "默认值", "参数2": "默认值"}, requirements={"参数2": "正则", "参数3": "正则"})
         * @Route("/{route}/{age}.{format}", name="show", defaults={"route": "api"}, requirements={"age": "\d+", "format": "html|php"})
         * @Method({"POST", "GET"})
         */
        public function showAction($route, $age, $format)
        {
            ...
        }
    }
    
  5. yml

    # [项目路径]/src/XxxBundle/Resources/config/routing.yml
    show:
        path:     /{route}/{age}.{format}
        defaults: { _controller: XxxBundle:Xxx:show, route: api }
        requirements:
            age: \d+
            format:  html|php
    
  6. xml

    # [项目路径]/src/XxxBundle/Resources/config/routing.xml
    <?xml version="1.0" encoding="UTF-8" ?>
    <routes>
        <route id="show" path="/{route}/{age}.{format}">
            <default key="_controller">XxxBundle:Xxx:show</default>
            <default key="route">api</default>
            <requirement key="age">\d+</requirement>
            <requirement key="format">html|php</requirement>
        </route>
    </routes>
    
Copyright © 豆包嘿嘿~ 2012-∞ 冀ICP备17033181号 all right reserved,powered by Gitbook修订: 2020-08-19 14:05:41

results matching ""

    No results matching ""