我创建了一个控制器来在每个页面上运行,它除了一个方面外都在工作..
在我的base.html.twig
文件中,我放了:{% render "EcsCrmBundle:Module:checkClock" %}
所以,我有一个控制器调用ModuleController
,checkClockAction
其中包含以下代码:
<?php
namespace Ecs\CrmBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Ecs\CrmBundle\Entity\TimeClock;
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;
})
问题是,即使页面刷新,会话也不会更新,直到我再次手动刷新页面......关于我做错了什么的任何想法?