![](/img/trans.png)
[英]How to generate URL for other pages using JavaScript in Symfony2?
[英]How to generate a link to a page with a request variable not in the url in symfony2
我在Symfony2中使用Prismic.io
在Prismic中,您必须通过ID查询文档。 例如,在我的树枝模板中有这个:
<h3><a href="{{ path('postDetail', {'id': post.getId, 'slug': post.slug, 'ref': ctx.maybeRef}) }}">{{ title }}</a></h3>
生成以下网址:
/U3zRuQEAADoAGg7D/slug-of-document
我在控制器中获取了十六进制ID,并且可以成功查询页面。 但是,使用ID并不是很友好。 我想生成一个可以在请求中传递id变量的网址,但在该网址中不可见。 这可能吗?
为了澄清起见,这是该过程的代码。 我有一个页面按标题列出所有博客文章:
<article class="blogEntry">
{% if date is defined %}
<small>{{ date|date("m/d/Y") }}</small>
{% endif %}
{% if title is defined %}
<h3><a href="{{ path('postDetail', {'id': post.getId, 'slug': post.slug, 'ref': ctx.maybeRef}) }}">{{ title }}</a></h3>
{% endif %}
{% if author is defined %}
<small class="blogAuthor">{{ author }}</small>
{% endif %}
{% if excerpt is defined %}
{{ excerpt|raw }}… <a href="{{ path('postDetail', {'id': post.getId, 'slug': post.slug, 'ref': ctx.maybeRef}) }}">Continue Reading »</a>
{% endif %}
</article>
单击标题后,将转到pageDetail操作:
/**
* @Route("/blog/{id}/{slug}", name="postDetail")
* @Template()
*/
public function postDetailAction($id, $slug)
{
$ctx = $this->get('prismic.context');
$post = $ctx->getApi()->forms()->everything->ref($ctx->getRef())->query('[[:d = at(document.id, "'.$id.'")]]')->submit();
$post = $post->getResults();
if ($post) {
return array(
'ctx' => $ctx,
'bgImages' => $backgroundImages,
'siteInfos' => $siteInfos,
'post' => $post
);
}
throw $this->createNotFoundException('Document not found');
}
我想要更多类似的东西,因此id在请求中,可用于查询,但不可见:
/**
* @Route("/blog/{slug}", name="postDetail")
* @Template()
*/
public function postDetailAction($slug)
{
$request = $this->getRequest();
$request->query->get('id');
$ctx = $this->get('prismic.context');
$post = $ctx->getApi()->forms()->everything->ref($ctx->getRef())->query('[[:d = at(document.id, "'.$id.'")]]')->submit();
$post = $post->getResults();
if ($post) {
return array(
'ctx' => $ctx,
'post' => $post
);
}
throw $this->createNotFoundException('Document not found');
}
我已经尝试过了,但是我得到了一个未定义的变量异常,使我觉得我在请求中没有正确发送ID。
我认为这实际上是不可能的,因为即使不是从该特定页面获得ID,也需要以某种方式表示ID。 只是想抛出一个问题,看看是否有可能。
为什么不使用简单的js提交隐藏的表单?
像这样的jQuery代码
<span class="link">{{ title }}</span>
<form class="fooForm" method="POST" action="{{ path('postDetail', {'slug': post.slug, 'ref': ctx.maybeRef}) }}" style="display:none">
<input name="id" type="hidden" value="{{post.getId}}">
</form>
<script>
$(document).on("click",".link",function(){
$('.fooForm').submit();
})
</script>
并在控制器中执行:
$request = $this->getRequest();
$id=$request->get('id');
但是您的网址只能处理后期请求并不是很酷,因此它只能通过一种形式访问,那不是太用户友好,但这似乎就是您想要的
我认为这是不可能的,因为HTTP方法是GET,您应该将变量传递给url。
如果您使用POST方法执行请求,也许有解决方案。 您可以设置表单,或者如果用户单击链接,则使用jQuery创建POST请求。 这样,您可以将变量传递给控制器。 但是,如果所有链接都需要此id变量,则这不是一种很舒适的方法。 也许您编写了用于链接生成的全局JavaScript函数,然后使用该函数代替{{ path('path_name') }}
。
如果您在触发控制器操作后立即重定向用户,则可能是另一种方法。 您使用GET方法传递变量,并且id
在URL中。 在触发的操作中,您将此变量保存到会话中,并将用户重定向到另一个操作,在该操作中url中不需要该变量。 然后,您可以通过获取会话变量在新控制器中使用id
。 这也不是一个好的解决方案,因为您必须创建许多多余的动作。
如果某些页面只需要JavaScript版本,我会推荐它,或者您应该考虑不要隐藏id。 也许这样做是不值得的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.