Heim >php教程 >php手册 >symfony2 路由工作原理及配置,symfony2工作原理

symfony2 路由工作原理及配置,symfony2工作原理

WBOY
WBOYOriginal
2016-06-13 08:46:161030Durchsuche

symfony2 路由工作原理及配置,symfony2工作原理

1、路由是程序的方法和URL的一一映射。

在配置文件里,把经常访问的路由放在前面,可以提高路由匹配的效率。

2、路由匹配的两种方式

  • Annotation

  允许在方法的上面用注释定义方法运行状态的功能

<span>class</span> UserController <span>extends</span><span> Controller{
    </span><span>/*</span><span>*
     * @Route("/user/login")
     * @Template()
     </span><span>*/</span>
     <span>public</span> <span>function</span><span> loginAction(){
         </span><span>//</span><span>代码</span>
<span>     }   
}</span>
  • router.yml

  symfony2常用的配置格式

两种方法不能同时用。

3、URL的定义

静态URL和动态URL

<?<span>php

namespace Scource\WebBundle\Controller;

</span><span>use</span><span> Symfony\Bundle\FrameworkBundle\Controller\Controller;
</span><span>use</span><span> Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
</span><span>use</span><span> Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
</span><strong><span>use</span><span> Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;<span>//</span></span></strong><span>下面路由定义中用到Method<br /><br /></span><span>//</span><span>定义总的router,如果当前控制器中用到的路由都是以/page开始</span><span>
/*</span><span>*
 * @Route("/page")
 </span><span>*/</span>
<span>class</span> DefaultController <span>extends</span><span> Controller
{
    </span><span>/*</span><span>*
     * @Route("/{page_num}",defaults={"page_num":1},requirements={"page_num"="\d+"})
     * @Template()
     * @Method("get")
     </span><span>*/</span>
    <span>//</span><span>以上注释并不是没有用,是利用注释动态影响程序代码,定义总的路由之后,在<span>定义</span>当前路由时,只定义第二级路由就可以
    //defaults={"page_num":1}设置page_num默认值为1
    //requirements={"page_num"="\d+"}要求page_num必须为数字
    //http://localhost:8000/app_dev.php/page/555</span>
    <span>public</span> <span>function</span> indexAction(<span>$page_num</span><span>)
    {
        </span><span>$method</span> = <span>$this</span>->getRequest()->getMethod();<span>//</span><span>获取表单数据的传送方式</span>
        <span>return</span> <span>array</span>('name' => <span>$page_num</span><span>);
    }
    </span><span>/*</span><span>*
     * @Route("/test",name="page_test")
     * @Template()
     </span><span>*/</span>
    <span>//</span><span>name="page_test"设置路由名称
    //通过命令行>php app/console router:match /page/test  可以查询/page/test的路由信息
    //http://localhost:8000/page/test?name=world</span>
    <span>public</span> <span>function</span><span> testAction() {
        </span><span>$name</span> = <span>$this</span>->getRequest()->get('name');<span>//</span><span>获取临时传递参数的值</span>
        <span>return</span> <span>array</span>('name' => <span>$name</span><span>);
    }
}</span>

//////////////

use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter

 

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn