Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何生成指向不在symfony2 url中的请求变量的页面的链接_Php_Symfony_Twig - Fatal编程技术网

Php 如何生成指向不在symfony2 url中的请求变量的页面的链接

Php 如何生成指向不在symfony2 url中的请求变量的页面的链接,php,symfony,twig,Php,Symfony,Twig,我正在Symfony2中使用Prismic.io 在Prismic中,您必须根据ID查询文档。例如,我的小树枝模板中有: <h3><a href="{{ path('postDetail', {'id': post.getId, 'slug': post.slug, 'ref': ctx.maybeRef}) }}">{{ title }}</a></h3> 我在控制器中获得十六进制id,可以成功地查询页面。然而,使用ID并不是很人性化。我想生

我正在Symfony2中使用Prismic.io

在Prismic中,您必须根据ID查询文档。例如,我的小树枝模板中有:

 <h3><a href="{{ path('postDetail', {'id': post.getId, 'slug': post.slug, 'ref': ctx.maybeRef}) }}">{{ title }}</a></h3>
我在控制器中获得十六进制id,可以成功地查询页面。然而,使用ID并不是很人性化。我想生成一个url,该url可以在请求中传递id变量,但在url中不可见。这可能吗

为了澄清,这里是整个过程的代码。我有一个按标题列出所有博客文章的页面:

<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>
我想要更像这样的东西,因此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需要以某种方式表示,即使不是来自这个特定页面。我只是想抛出这个问题,看看是否可能。

我认为这是不可能的,因为HTTP方法是GET,您应该通过url传递变量

如果您使用POST方法处理请求,可能会有一个解决方案。您可以设置表单,或者在用户单击链接时使用jQuery创建POST请求。这样,就可以将变量传递给控制器。但是如果所有的链接都需要这个id变量,那么这不是一个很舒服的方式。如果您为链接生成编写一个全局JavaScript函数,并使用该函数而不是
{{path('path_name')}}

另一种方法是,如果在触发控制器操作后立即重定向用户。您使用GET方法传递变量,
id
位于url中。在激发的操作中,您将此变量保存到会话,并将用户重定向到另一个操作,其中url中不需要该变量。然后,您可以通过获取会话变量在新控制器中使用
id
。这也不是一个好的解决方案,因为您必须创建大量冗余操作


我推荐JavaScript版本,如果它只需要在一些页面中,或者你应该考虑不隐藏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>

但是你的url只能处理post请求,这并不太酷,因此它只能通过表单访问,这不太方便用户,但这似乎是你想要的

你的slug是独一无二的吗?如果是,则不需要传递ID。prismic.io当前不会生成唯一的slug,因此我可以按slug查询,但不能保证始终是唯一的。感谢您的建议。我将研究JS选项,但您可能是对的,这可能不值得付出努力。在当今的大多数web应用程序中,唯一的slug基本上就是ID。我不确定prismic为什么使用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');
<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');