![](/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.