如何阿贾克斯capabilites添加到Symfony的 [英] How to add Ajax capabilites to Symfony
问题描述
我有一组在页面的会议,我想用AJAX来去除。即点击一个链接,而无需浏览一个新的页面,只是删除了会议,并显示成功的消息。
现在,根据给定的答案(仍然对我来说没有工作的),我有以下几点:
控制器
使用的Symfony \分量\ HttpFoundation \ JsonResponse;
// ..
公共职能ajaxRemoveSessionAction()
{
$会话= $这个 - >调用getRequest() - >的getSession();
$会话级>删除(名称);
返回新JsonResponse(阵列('成功'=>真));
}
路由:
ajax_remove_session:
模式:/删除会话
默认值:{_controller:FooTestBundle:页:ajaxRemoveSession}
枝:
< A HREF =#ID =remove_session>删除会话< / A>
<脚本类型=文/ JavaScript的>
$(文件)。就绪(函数(){
$('#remove_session)。点击(函数(){
。事件preventDefault();
$阿贾克斯({
网址:{{URL('ajax_remove_session')}},
缓存:假的,
成功:函数(结果){
$(成功)追加(结果)。
}
});
});
});
< / SCRIPT>
UPDATING问题:
使用所有的codeS中发现的路由,控制器和模板
控制器:PageController.php
/src/Simon/TestBundle/Controller/PageController.php
< PHP
命名空间西蒙\ TestBundle \控制器;
使用Symfony的\包\ FrameworkBundle \控制器\控制器;
使用Symfony的\分量\ HttpFoundation \会议\会议;
使用Symfony的\分量\ HttpFoundation \ JsonResponse;
一流的PageController扩展控制器
{
公共职能helloAction的($名)
{
$会话=新的Session();
$会话级>开始();
$会话级>获得(名称,DRAK');
$会话级>获得(名称);
$会话级> getFlashBag() - >添加(通知,资料更新');
$消息= NULL;
的foreach($会话级> getFlashBag() - >获得(通知,阵列())为$消息){
$消息= $消息;
}
返回$这个 - >渲染('SimonTestBundle:页:index.html.twig,阵列(名称=> $名称''$消息)。);
}
公共职能ajaxRemoveSessionAction()
{
//销毁所需会议
$会话= $这个 - >调用getRequest() - >的getSession();
$会话级>删除(名称);
返回新JsonResponse(阵列('成功'=>真));
}
}
模板:枝杈模板
/src/Simon/TestBundle/Resources/views/Page/index.html.twig
{%'延伸SimonTestBundle :: layout.html.twig%}
{%块体%}
&所述; A HREF =#标识=remove_session>删除会话&所述; / a取代;
<脚本类型=文/ JavaScript的>
$('#remove_session)。点击(函数(五){
即preventDefault();
$阿贾克斯({
网址:{{URL('ajax_remove_session')}},
缓存:假的,
成功:函数(HTML){
//做一些成功
}
}),失败(函数(事件){
执行console.log(事件);
});
});
});
< / SCRIPT>
{%端嵌段%}
路由:
/src/Simon/TestBundle/Resources/config/routing.yml
simon_test_homepage:
模式:/你好/ {名}
默认值:{_controller:SimonTestBundle:页:您好}
ajax_remove_session:
模式:/删除会话
默认值:{_controller:SimonTestBundle:页:ajaxRemoveSession}
尝试把引号之间的URL的选项的值:
URL:{{URL('ajax_remove_session')}}',
I have a set of sessions in a page, which I want to remove using AJAX. i.e click on a link, and without having to navigate for a new page, just remove the session, and show a message on success.
Now, as per the given answer (which still did not work for me), I have the following:
Controller
use Symfony\Component\HttpFoundation\JsonResponse;
//..
public function ajaxRemoveSessionAction()
{
$session = $this->getRequest()->getSession();
$session->remove('name');
return new JsonResponse(array('success' => true));
}
routing:
ajax_remove_session:
pattern: /remove-session
defaults: { _controller: FooTestBundle:Page:ajaxRemoveSession }
twig:
<a href="#" id="remove_session">Remove session</a>
<script type="text/javascript">
$(document).ready(function() {
$('#remove_session').click(function(){
event.preventDefault();
$.ajax({
url: {{ url('ajax_remove_session') }},
cache: false,
success: function(result){
$(".success").append(result);
}
});
});
});
</script>
UPDATING QUESTION:
With all the codes found in the the routing, controller and template
Controller: PageController.php
/src/Simon/TestBundle/Controller/PageController.php
<?php
namespace Simon\TestBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\JsonResponse;
class PageController extends Controller
{
public function helloAction($name)
{
$session = new Session();
$session->start();
$session->get('name', 'Drak');
$session->get('name');
$session->getFlashBag()->add('notice', 'Profile Updated');
$messages = null;
foreach($session->getFlashBag()->get('notice', array()) as $message){
$messages = $message;
}
return $this->render('SimonTestBundle:Page:index.html.twig', array('name' => $name.' '.$messages));
}
public function ajaxRemoveSessionAction()
{
// Destroy the desired session
$session = $this->getRequest()->getSession();
$session->remove('name');
return new JsonResponse(array('success' => true));
}
}
Template: Twig template
/src/Simon/TestBundle/Resources/views/Page/index.html.twig
{% extends 'SimonTestBundle::layout.html.twig' %}
{% block body %}
<a href="#" id="remove_session">Remove session</a>
<script type="text/javascript">
$('#remove_session').click(function(e){
e.preventDefault();
$.ajax({
url: {{ url('ajax_remove_session') }},
cache: false,
success: function(html){
// do something on success
}
}).fail(function(event){
console.log(event);
});
});
});
</script>
{% endblock %}
Routing:
/src/Simon/TestBundle/Resources/config/routing.yml
simon_test_homepage:
pattern: /hello/{name}
defaults: { _controller: SimonTestBundle:Page:hello }
ajax_remove_session:
pattern: /remove-session
defaults: { _controller: SimonTestBundle:Page:ajaxRemoveSession }
Try to put the value of the URL's option between quotes :
url: '{{ url('ajax_remove_session') }}',
这篇关于如何阿贾克斯capabilites添加到Symfony的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!