duanpao4522 2015-05-27 08:16
浏览 6

Symfony2最佳实践:路由占位符与查询参数

Is there a recommended way of choosing how to give data to a controller?

Often I have to decide if I want to use route place holders like:

/**
 * @Route("/hello/{name}", name="hello")
 */
public function indexAction($name)
{
    return new Response('<html><body>Hello '.$name.'!</body></html>');
}

Usage: /hallo/Thorsten

Or use query parameters ($_GET):

/**
 * @Route("/hello")
 */
public function indexAction()
{
    $request = Request::createFromGlobals();
    $name = $request->get('name');
    return new Response('<html><body>Hello '.$name.'!</body></html>');
}

Usage: /hallo?name=Thorsten

  • 写回答

1条回答 默认 最新

  • doujiao4710 2015-05-27 09:17
    关注

    I don't think there is a rule for this, it depends.

    Is the value a generic string possibly containing spaces or weird characters or symbols or maybe slashes? I'd use the query string, it's widely used for searches, pagination and such.

    Is the value in a set of predefined "words" or "numbers", like categories, user IDs, blog post slugs and such? I'd use the path for simplicity and to follow SEO rules.

    Take a look here for more information on what I mean: http://googlewebmastercentral.blogspot.co.nz/2015/04/better-presentation-of-urls-in-search.html

    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?