我创建了一个在每个页面上运行的控制器,它除了一个方面之外都能工作。。
在我的base.html.twig
文件中,我放入:{% render "EcsCrmBundle:Module:checkClock" %}
因此,我有一个名为ModuleController
的控制器,其动作为checkClockAction
,其中包含以下代码:
<?php
namespace EcsCrmBundleController;
use SymfonyBundleFrameworkBundleControllerController;
use EcsCrmBundleEntityTimeClock;
class ModuleController extends Controller
{
public function checkClockAction() {
$em = $this->getDoctrine()->getEntityManager();
$user = $this->get('security.context')->getToken()->getUser();
$today = time();
$start = date('Y-m-d 00:00:00');
$entities = $em->getRepository('EcsCrmBundle:TimeClock');
$query = $entities->createQueryBuilder('tc')
->select('tc.in1, tc.out1, tc.in2, tc.out2, tc.in3, tc.out3')
->where('tc.noteBy = :user')
->andWhere('tc.daydate >= :start')
->setParameter('user', $user->getid())
->setParameter('start', $start)
->setMaxResults('1')
->getQuery();
$entities = $query->getOneOrNullResult();
if (empty($entities)) {
$ents = "clocked_out";
$this->get('session')->set('clockedin', 'clocked_out');
} else {
for ($i=1; $i <= 3; $i++) {
if ($entities["in$i"] != NULL) {
$ents = "clocked_in";
$this->get('session')->set('clockedin', 'clocked_in');
if ($i == 1) {
$this->get('session')->set('nextclock', "out$i");
} else {
$x = $i+1;
$this->get('session')->set('nextclock', "out$x");
}
if ($entities["out$i"] != NULL) {
$ents = "clocked_out";
$this->get('session')->set('clockedin', 'clocked_out');
$x = $i+1;
$this->get('session')->set('nextclock', "in$x");
}
if ($entities["out3"] != NULL) {
$ents = "day_done";
$this->get('session')->set('clockedin', 'day_done');
}
}
}
}
return $this->render('EcsCrmBundle:Module:topclock.html.twig', array(
'cstat' => $ents,
));
}
}
这很好,除了当我点击其中一个登录/注销按钮时。。。
<a href="{{ path('timeclock_clockin') }}" class="clockin clocker">Clock In</a>
这是通过jquery ajax提交的,带有:
$(".clocker").click(function() {
// lets send the ajax request to clockin..
$.ajax({
url: $(this).attr('href'),
type: "POST",
data: "url="+document.URL,
success: function(response) {
location.reload(true);
return false;
}
});
return false;
})
问题是,即使页面刷新,会话也不会更新,直到我再次手动刷新页面。。。你知道我做错了什么吗?
您使用的location.reload(true);
应该忽略浏览器缓存。。。
可能发生的情况是,当您访问页面时,您的一个操作会触发以更改会话的状态。
例如:访问页面(将操作设置为X)。AJAX请求:将操作设置为Y。刷新:将操作设置为X。再次刷新:将操作设置为Y.
为了获得一些调试帮助,你可以让AJAX请求返回会话的当前状态,这样你就可以在它更改时看到它吗?