You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Symfony 5.3开发模式正常生产模式返回503错误排查求助

Symfony 5.3 生产模式503错误问题排查结果

核心错误原因

你代码中存在逻辑判断颠倒问题,开发模式下debug规则宽松不会触发严重错误,生产模式严格校验直接抛出异常导致503:

  1. Doctrine 的 getResult() 方法永远不会返回null,没有匹配查询结果时返回的是空数组,is_null([])的返回值为false,你写的if(is_null($azemoon_ha))判断逻辑完全不符合实际返回结果
  2. 现有逻辑下,无论是否查询到对应数据,都会直接进入else分支执行重定向,极易出现重定向死循环;就算逻辑走通if分支,你定义的$keys变量仅在foreach内部生效,渲染模板时会出现变量未定义报错,生产模式下该类错误直接触发503

修复步骤

  • 修正$azemoon_ha的结果判断逻辑,同时提前定义模板用到的变量避免未定义报错
    原错误代码块:
    $azemoon_arr = [];
    $azemoon_arr['meta']['lesson1'] = null;
    $i = 1;
    if(is_null($azemoon_ha)){
        // dd($azemoon_ha);
        foreach ($azemoon_ha as $value) {
            $az_metas = $value->getAzemoonMetas();
            // dd($az_metas);
            $c_num = 1;
            $keys = [];
            foreach ($az_metas as $am) {
                $azemoon_arr['meta']['lesson'.$c_num][] = $am->getVal();
                $keys['lesson'.$c_num] = $am->getName();
                $c_num++;
            }
            $azemoon_arr['name'][]     = 'آزمون '.$i;
            $azemoon_arr['tarazKol'][] = $value->getTarazKol();
            $azemoon_arr['tarazOmomi'][] = $value->getTarazOmomi();
            $azemoon_arr['tarazEkhtesasi'][] = $value->getTarazEkhtesasi();
            $i++;
        }
    }else{
        $this->addFlash('error', 'برای دانش آموز '.$student->getName().' ازمونی تعریف نشده است');
        return $this->redirectToRoute('student.info', ['username'=> $username]);
    }
    
    修正后代码块:
    $azemoon_arr = [];
    $azemoon_arr['meta']['lesson1'] = null;
    $keys = []; // 提前定义模板用到的变量,避免未定义报错
    $i = 1;
    // 替换判断逻辑:非空时遍历,空时走提示分支
    if(!empty($azemoon_ha)){
        foreach ($azemoon_ha as $value) {
            $az_metas = $value->getAzemoonMetas();
            $c_num = 1;
            foreach ($az_metas as $am) {
                $azemoon_arr['meta']['lesson'.$c_num][] = $am->getVal();
                $keys['lesson'.$c_num] = $am->getName();
                $c_num++;
            }
            $azemoon_arr['name'][]     = 'آزمون '.$i;
            $azemoon_arr['tarazKol'][] = $value->getTarazKol();
            $azemoon_arr['tarazOmomi'][] = $value->getTarazOmomi();
            $azemoon_arr['tarazEkhtesasi'][] = $value->getTarazEkhtesasi();
            $i++;
        }
    }else{
        $this->addFlash('error', 'برای دانش آموز '.$student->getName().' ازمونی تعریف نشده است');
        return $this->redirectToRoute('student.info', ['username'=> $username]);
    }
    
  • 代码修改完成后执行生产环境缓存清理命令,确保代码变更生效:
    php bin/console cache:clear --env=prod
    php bin/console cache:warmup --env=prod
    
  • 可选补充:给$student增加判空逻辑,避免查询不到对应学生时调用方法抛出错误
    $student = $this->studentRepository
    ->createQueryBuilder('s')
    ->andWhere('s.isArchive = :is_archive')
    ->setParameter('is_archive', false)
    ->andWhere('s.username = :username')
    ->setParameter('username', $username)
    ->setMaxResults(1)
    ->getQuery()
    ->getOneOrNullResult();
    // 新增判空逻辑
    if (null === $student) {
        $this->addFlash('error', '未找到对应的学生信息');
        return $this->redirectToRoute('app_login');
    }
    

内容的提问来源于stack exchange,提问作者abdola shamsaddini

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 06:36:04