protected function handle() { // 检查 $role = RoleModel::getRole($this->id); if ($role) { throw new \Exception('角色已经使用了'); } $administration_roles = $this->getContainer()->getParameter('administration_roles'); if ($administration_roles) { $administration_roles = $administration_roles->toArray(); } else { $administration_roles = array(); } if (!isset($administration_roles[$this->id])) { throw new \Exception('角色不存在'); } $administration_role = $administration_roles[$this->id]; $administration_role['id'] = $this->id; $tree = RoleModel::getTree(); $request = $this->getRequest(); if ($request->getMethod() == 'POST') { $parent_role_id = $request->request->get('parent_id'); $session = $this->getSession(); try { $parent_role = RoleModel::getRole($parent_role_id); if (!$parent_role) { throw new \Exception('父角色不存在或者没有被使用'); } $role = new RoleModel($administration_role); $parent_role->createChildNode($role->toArray()); $session->addFlash('success', '操作成功'); } catch (\Exception $e) { $session->addFlash('error', $e->getMessage()); } return new RedirectResponse($this->generateUrl('admin_role')); } return $this->render('role/add.html.twig', array('tree' => $tree, 'role' => $administration_role)); }