首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Symfony - ID和用户友好路线

Symfony - ID和用户友好路线
EN

Stack Overflow用户
提问于 2018-08-29 14:21:22
回答 2查看 131关注 0票数 0

用例:

  • 我有一个ID和一个路由的数据
  • 我有一个链接列表和一个使用ID的路由器,这样,如果标题或路由发生变化,链接将始终指向相同的内容。
  • 我希望当用户出于可用性和SEO的目的进入页面时,该路由将显示在URL中而不是ID中。

目前,我正在使用重定向,我怀疑这是正确的方式来处理它。

代码语言:javascript
复制
public function show($slug)
{
  $list = array(
    // blog data goes in here
  };
  // when we go to the blog post page, we loop through the blog post data
  foreach ($list as $post) {
    // if the slug from our url is equal to the ID
    if ($slug == $post['id']){
      // redirect to the user-friendly route instead
      $route = '/blog/' . $post['route'];
      return $this->redirect($route);
    }
    // if this is the user-friendly, route, then render the page
    if ($slug == $post['route']) {
        $title = $post['title'];
        $content = $post['content'];
    }
  }
  // hello/show.html.twig is the twig template I'm currently using to render single pages
  return $this->render('hello/show.html.twig', [
    'title' => $title,
    'content' => $content,
  ]);
}   

这在技术上是可行的,但我真的不认为这是真正正确的方法。是否有更好的方法为同一视图使用两条路由,并在链接单击和页面呈现之间的另一条路径上给予优惠待遇?

EN

回答 2

Stack Overflow用户

发布于 2018-08-29 14:40:37

如果使用Symfony,只需在routing.yml文件中键入路由即可。任何控制器操作必须有一个呈现,目前您不使用框架。

在“医生”中读到更多有关这方面的内容。相反,您可以在操作中调用操作。

https://symfony.com/doc/current/components/routing.html#load-routes-from-a-file

票数 0
EN

Stack Overflow用户

发布于 2018-08-29 15:00:49

只需在存储库中创建一个名为“findOneByIdOrSlug”的方法,并构建一个查询,将URI中的值传递给带or的where子句。确保将结果限制为一个,并正确处理异常。

然后,从控制器调用该方法,这样就可以了。

不能提供密码因为我现在在打电话。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52079758

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档