简体   繁体   English

如何使用symfony2中的url不在请求变量中生成页面链接

[英]How to generate a link to a page with a request variable not in the url in symfony2

I am using Prismic.io in Symfony2 我在Symfony2中使用Prismic.io

In Prismic, you have to query a document by the ID. 在Prismic中,您必须通过ID查询文档。 So for instance, I have this in my twig template: 例如,在我的树枝模板中有这个:

 <h3><a href="{{ path('postDetail', {'id': post.getId, 'slug': post.slug, 'ref': ctx.maybeRef}) }}">{{ title }}</a></h3>

which generates this url: 生成以下网址:

/U3zRuQEAADoAGg7D/slug-of-document

I get the hexadecimal id in the controller and can query the page successfully. 我在控制器中获取了十六进制ID,并且可以成功查询页面。 However, using the ID is not very human friendly. 但是,使用ID并不是很友好。 I would like to generate a url that can pass the id variable in the request, but not be visible in the url. 我想生成一个可以在请求中传递id变量的网址,但在该网址中不可见。 Is this possible? 这可能吗?

For clarification, here is the code through the process. 为了澄清起见,这是该过程的代码。 I have a page that lists all blog articles by title: 我有一个页面按标题列出所有博客文章:

<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>

When the title is clicked, it goes to the pageDetail Action: 单击标题后,将转到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');
    }

I would like something more like this, so the id is in the request and can be used to query, but not visible: 我想要更多类似的东西,因此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');

} }

I have tried this, but I get an undefined variable exception, making me think I am not sending the ID properly in the request. 我已经尝试过了,但是我得到了一个未定义的变量异常,使我觉得我在请求中没有正确发送ID。

I dont think this is even possible really, because the ID would need to be represented somehow, even when not coming from this particular page. 我认为这实际上是不可能的,因为即使不是从该特定页面获得ID,也需要以某种方式表示ID。 Just wanted to throw out the question and see if it's possible. 只是想抛出一个问题,看看是否有可能。

Why not use simple js to submit a hidden form ? 为什么不使用简单的js提交隐藏的表单?

sth like this jquery-code 像这样的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>

and in controller just do : 并在控制器中执行:

   $request = $this->getRequest();
   $id=$request->get('id');

but its not too cool that your url can only handle post-requests, so its only reachable through a form, thats not too user-friendly but it seems this is what you want 但是您的网址只能处理后期请求并不是很酷,因此它只能通过一种形式访问,那不是太用户友好,但这似乎就是您想要的

I don't think it's possible, as the HTTP method is GET, you should pass the variables over the url. 我认为这是不可能的,因为HTTP方法是GET,您应该将变量传递给url。

Maybe there is a solution, if you do the request with POST method. 如果您使用POST方法执行请求,也许有解决方案。 You can set up a form, or use jQuery to create POST request if the user clicks on the link. 您可以设置表单,或者如果用户单击链接,则使用jQuery创建POST请求。 With this, you can pass variables to the controller. 这样,您可以将变量传递给控制器​​。 But if all the links need this id variable, it's not a too comfortable way. 但是,如果所有链接都需要此id变量,则这不是一种很舒适的方法。 Maybe if you write a global JavaScript function for link generation, and use that function instead of {{ path('path_name') }} . 也许您编写了用于链接生成的全局JavaScript函数,然后使用该函数代替{{ path('path_name') }}

Another method can be if you redirect the user immediately after the controller action is fired. 如果您在触发控制器操作后立即重定向用户,则可能是另一种方法。 You pass the variables with GET method, and the id is in the url. 您使用GET方法传递变量,并且id在URL中。 in the fired action you save this variable to a session and redirect the user to another action, where the variable is not needed in the url. 在触发的操作中,您将此变量保存到会话中,并将用户重定向到另一个操作,在该操作中url中不需要该变量。 Then you can use the id in your new controller by get the session variable. 然后,您可以通过获取会话变量在新控制器中使用id It's not a nice solution as well, because you have to create a lot of redundant action. 这也不是一个好的解决方案,因为您必须创建许多多余的动作。

I would recommend the JavaScript version if it's only needed in some pages, or you should consider to not to hide the id. 如果某些页面只需要JavaScript版本,我会推荐它,或者您应该考虑不要隐藏id。 Maybe it's not worth the effort to do that. 也许这样做是不值得的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM