Symfony 路由不起作用?

发布于 2025-01-07 05:02:49 字数 1511 浏览 0 评论 0原文

我正在处理现有项目。我有以下两个网址,

http://example.dev/en/course/myUserName/myCourseName/150/myChapterName/1016/page/1/?hcid=147

http://example.dev/en/course/myUserName/myCourseName/150/myChapterName/1016/page/2/?hcid=147

第二个网址工作正常,但第一个网址给我以下错误

Unable to find a matching route to generate url for params "array (  'action' => 'showChapter',  'module' => 'course',  'courseowner' => 'myUserName',  'coursename' => 'myCourseName',  'courseid' => '150',  'chaptername' => 'myChapterName',)"., 
referer: http://example.dev/en/course/myUserName/myCourseName/150/myChapterName/1016/page/1/?hcid=147

我在routing.yml中有以下条目:

course_chapter:
  url: /:sf_culture/course/:courseowner/:coursename/:courseid/:chaptername/:chapterid/:page/:pageid/
  param: { module: course, action: showChapter }
  requirements:
      courseid: \d+

url_for():< /strong>

<?php $headchapter = ($hcid) ? '?hcid='.$hcid : '' ?>
<a href="<?php echo url_for('course/showChapter?courseowner='.$sf_params->get("courseowner").'&coursename='.$sf_params->get("coursename").'&courseid='.$sf_params->get("courseid").'&chaptername='.$chapter->getName().'&chapterid='.$chapter->getId().'&page=page&pageid=1').$headchapter ?>">

我无法理解它如何适用于第二个 URL 而不是第一个 URL。我检查了操作和视图,没有发现任何错误。

有什么想法或调试技术吗?

I am working on existing project. I have following two URLs

http://example.dev/en/course/myUserName/myCourseName/150/myChapterName/1016/page/1/?hcid=147

http://example.dev/en/course/myUserName/myCourseName/150/myChapterName/1016/page/2/?hcid=147

2nd URL is working fine but 1st URL is giving me following error:

Unable to find a matching route to generate url for params "array (  'action' => 'showChapter',  'module' => 'course',  'courseowner' => 'myUserName',  'coursename' => 'myCourseName',  'courseid' => '150',  'chaptername' => 'myChapterName',)"., 
referer: http://example.dev/en/course/myUserName/myCourseName/150/myChapterName/1016/page/1/?hcid=147

I have following entry in routing.yml:

course_chapter:
  url: /:sf_culture/course/:courseowner/:coursename/:courseid/:chaptername/:chapterid/:page/:pageid/
  param: { module: course, action: showChapter }
  requirements:
      courseid: \d+

url_for():

<?php $headchapter = ($hcid) ? '?hcid='.$hcid : '' ?>
<a href="<?php echo url_for('course/showChapter?courseowner='.$sf_params->get("courseowner").'&coursename='.$sf_params->get("coursename").'&courseid='.$sf_params->get("courseid").'&chaptername='.$chapter->getName().'&chapterid='.$chapter->getId().'&page=page&pageid=1').$headchapter ?>">

I cant understand how it is working for 2nd URL and not for first URL. I have checked the action and view and I did not find any error.

Any Idea or debugging technique ??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

洒一地阳光 2025-01-14 05:02:49

不要那样做你的链接。有一种更清洁的方法。

<a href="<?php echo url_for('course/showChapter?courseowner='.$sf_params->get("courseowner").'&coursename='.$sf_params->get("coursename").'&courseid='.$sf_params->get("courseid").'&chaptername='.$chapter->getName().'&chapterid='.$chapter->getId().'&page=page&pageid=1').$headchapter ?>">

这样做:

link_to("Link text","course_chapter",array("courseowner"=>$sf_params->get("courseowner"),
                                           "coursename"=>$sf_params->get("coursename"),
                                           "courseid"=>$sf_params->get("courseid"),
                                           "chaptername"=>$chapter->getName(),
                                           "chapterid"=>$chapter->getId(),
                                           "pageid"=>1,
                                           "hcid"=>$hcid))

如果您的路由 URL 中不存在参数,则会像 ?hcid=2 一样附加该参数
为什么使用 sf_params 来生成 URL 参数而不是数据库?即为什么是来自学说对象的 $sf_params->get('courseid') 而不是 $course->getId()

另外,不要使用course/showChapter,而是使用course_chapter的路由ID,

我建议您返回symfony手册并重新阅读路由部分。

编辑:

在您的路线定义中,您有:

url: /:sf_culture/course/:courseowner/:coursename/:courseid/:chaptername/:chapterid/:page/:pageid/

为什么页面是 a参数如果它总是相同的值,即它总是页面。

这会更好:

url: /:sf_culture/course/:courseowner/:coursename/:courseid/:chaptername/:chapterid/page/:pageid/

并且不要 chapterid 和 pageid 也需要成为 \d+ 要求的一部分吗?

Don't do your links like that. There is a cleaner way.

<a href="<?php echo url_for('course/showChapter?courseowner='.$sf_params->get("courseowner").'&coursename='.$sf_params->get("coursename").'&courseid='.$sf_params->get("courseid").'&chaptername='.$chapter->getName().'&chapterid='.$chapter->getId().'&page=page&pageid=1').$headchapter ?>">

Do this instead:

link_to("Link text","course_chapter",array("courseowner"=>$sf_params->get("courseowner"),
                                           "coursename"=>$sf_params->get("coursename"),
                                           "courseid"=>$sf_params->get("courseid"),
                                           "chaptername"=>$chapter->getName(),
                                           "chapterid"=>$chapter->getId(),
                                           "pageid"=>1,
                                           "hcid"=>$hcid))

If a param doesn't exist in your routing url, it'll get appended like ?hcid=2
Why are you using sf_params to generate the URL params and not the database? i.e why is it $sf_params->get('courseid') and not $course->getId() from a doctrine object?

Also instead of using course/showChapter use the route id of course_chapter

I'd advise that you go back to the symfony manual and re-read the routing section.

EDIT:

In your route definition, you have:

url: /:sf_culture/course/:courseowner/:coursename/:courseid/:chaptername/:chapterid/:page/:pageid/

why is page a parameter if it's always the same value, i.e. it's always gonna be page.

This would be better:

url: /:sf_culture/course/:courseowner/:coursename/:courseid/:chaptername/:chapterid/page/:pageid/

And don't chapterid and pageid need to be part of the requirements of \d+ too?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文