Controller/PostController.php
public function createAction(
        string $production_slug,
        Request $request,
        TokenStorageInterface $token,
        AuthorizationCheckerInterface $auth
    ): Response {
        // Lookup the production by production_slug.
        $production_repo = $this->em->getRepository(Production::class);
        if (null === $production = $production_repo->findOneBy(['slug' => $production_slug])) {
            throw new NotFoundHttpException();
        }

        // Check permissions for this action.
        if (!$auth->isGranted('GROUP_ROLE_USER', $production)) {
            throw new AccessDeniedException();
        }

        // Get some basic information about the user.
        $user = $token->getToken()->getUser();

        // Create a new post.
        $post = new Post();
        $post->setActive(true);
        $post->setPinned(false);
        $post->setAuthor($user->getUsername());
        $post->addGroup($production);

        // This post is a reply.
        if ($request->query->has('reply-to')) {
            // Make sure the parent post is valid.
            $repo = $this->em->getRepository(Post::class);
            if (null === $parent = $repo->findOneBy(['id' => $request->query->get('reply-to')])) {
                throw new NotFoundHttpException();
            }

            // Must be a member of the same group.
            if (!$parent->getGroups()->contains($production)) {
                throw new AccessDeniedException();
            }

            // Parent must not be a child.
            if (null !== $parent->getParent()) {
                throw new AccessDeniedException();
            }
            $post->setParent($parent);

            // This is a reply, use the basic reply form.
            $form = $this->form->create(ReplyType::class, $post);
        } else {
            // This is a new post, use the post form.
            $form = $this->form->create(PostType::class, $post);
        }

        // Handle the form.
        $form->handleRequest($request);

        // Form is submitted and valid.
        if ($form->isSubmitted() && $form->isValid()) {
            // Persist the post.
            $this->em->persist($post);
            $this->em->flush();

            // Set success message and redirect.
            $this->session->getFlashBag()->add(
                'success',
                $this->translator->trans('post.created', [], BkstgNoticeBoardBundle::TRANSLATION_DOMAIN)
            );

            return new RedirectResponse($this->url_generator->generate(
                'bkstg_board_show',
                ['production_slug' => $production->getSlug()]
            ));
        }

        // Render the form.
        return new Response($this->templating->render('@BkstgNoticeBoard/Post/create.html.twig', [
            'form' => $form->createView(),
            'post' => $post,
            'production' => $production,
        ]));
    }
public function createAction(
        string $production_slug,
        Request $request,
        TokenStorageInterface $token,
        AuthorizationCheckerInterface $auth
    ): Response {
        // Lookup the production by production_slug.
        $production_repo = $this->em->getRepository(Production::class);
        if (null === $production = $production_repo->findOneBy(['slug' => $production_slug])) {
            throw new NotFoundHttpException();
        }

        // Check permissions for this action.
        if (!$auth->isGranted('GROUP_ROLE_USER', $production)) {
            throw new AccessDeniedException();
        }

        // Get some basic information about the user.
        $user = $token->getToken()->getUser();

        // Create a new post.
        $post = new Post();
        $post->setActive(true);
        $post->setPinned(false);
        $post->setAuthor($user->getUsername());
        $post->addGroup($production);

        // This post is a reply.
        if ($request->query->has('reply-to')) {
            // Make sure the parent post is valid.
            $repo = $this->em->getRepository(Post::class);
            if (null === $parent = $repo->findOneBy(['id' => $request->query->get('reply-to')])) {
                throw new NotFoundHttpException();
            }

            // Must be a member of the same group.
            if (!$parent->getGroups()->contains($production)) {
                throw new AccessDeniedException();
            }

            // Parent must not be a child.
            if (null !== $parent->getParent()) {
                throw new AccessDeniedException();
            }
            $post->setParent($parent);

            // This is a reply, use the basic reply form.
            $form = $this->form->create(ReplyType::class, $post);
        } else {
            // This is a new post, use the post form.
            $form = $this->form->create(PostType::class, $post);
        }

        // Handle the form.
        $form->handleRequest($request);

        // Form is submitted and valid.
        if ($form->isSubmitted() && $form->isValid()) {
            // Persist the post.
            $this->em->persist($post);
            $this->em->flush();

            // Set success message and redirect.
            $this->session->getFlashBag()->add(
                'success',
                $this->translator->trans('post.created', [], BkstgNoticeBoardBundle::TRANSLATION_DOMAIN)
            );

            return new RedirectResponse($this->url_generator->generate(
                'bkstg_board_show',
                ['production_slug' => $production->getSlug()]
            ));
        }

        // Render the form.
        return new Response($this->templating->render('@BkstgNoticeBoard/Post/create.html.twig', [
            'form' => $form->createView(),
            'post' => $post,
            'production' => $production,
        ]));
    }
public function createAction ( string $ production_slug , Request $ request , TokenStorageInterface $ token , AuthorizationCheckerInterface $ auth ) : Response { // Lookup the production by production_slug. $ production_repo = $ this -> em -> getRepository ( Production :: class ) ; if ( null === $ production = $ production_repo -> findOneBy ( [ 'slug' => $ production_slug ] ) ) { throw new NotFoundHttpException ( ) ; } // Check permissions for this action. if ( ! $ auth -> isGranted ( 'GROUP_ROLE_USER' , $ production ) ) { throw new AccessDeniedException ( ) ; } // Get some basic information about the user. $ user = $ token -> getToken ( ) -> getUser ( ) ; // Create a new post. $ post = new Post ( ) ; $ post -> setActive ( true ) ; $ post -> setPinned ( false ) ; $ post -> setAuthor ( $ user -> getUsername ( ) ) ; $ post -> addGroup ( $ production ) ; // This post is a reply. if ( $ request -> query -> has ( 'reply-to' ) ) { // Make sure the parent post is valid. $ repo = $ this -> em -> getRepository ( Post :: class ) ; if ( null === $ parent = $ repo -> findOneBy ( [ 'id' => $ request -> query -> get ( 'reply-to' ) ] ) ) { throw new NotFoundHttpException ( ) ; } // Must be a member of the same group. if ( ! $ parent -> getGroups ( ) -> contains ( $ production ) ) { throw new AccessDeniedException ( ) ; } // Parent must not be a child. if ( null !== $ parent -> getParent ( ) ) { throw new AccessDeniedException ( ) ; } $ post -> setParent ( $ parent ) ; // This is a reply, use the basic reply form. $ form = $ this -> form -> create ( ReplyType :: class , $ post ) ; } else { // This is a new post, use the post form. $ form = $ this -> form -> create ( PostType :: class , $ post ) ; } // Handle the form. $ form -> handleRequest ( $ request ) ; // Form is submitted and valid. if ( $ form -> isSubmitted ( ) && $ form -> isValid ( ) ) { // Persist the post. $ this -> em -> persist ( $ post ) ; $ this -> em -> flush ( ) ; // Set success message and redirect. $ this -> session -> getFlashBag ( ) -> add ( 'success' , $ this -> translator -> trans ( 'post.created' , [ ] , BkstgNoticeBoardBundle :: TRANSLATION_DOMAIN ) ) ; return new RedirectResponse ( $ this -> url_generator -> generate ( 'bkstg_board_show' , [ 'production_slug' => $ production -> getSlug ( ) ] ) ) ; } // Render the form. return new Response ( $ this -> templating -> render ( '@BkstgNoticeBoard/Post/create.html.twig' , [ 'form' => $ form -> createView ( ) , 'post' => $ post , 'production' => $ production , ] ) ) ; }
Create a new post.

@param string                        $production_slug The production slug.
@param Request                       $request         The incoming request.
@param TokenStorageInterface         $token           The token storage service.
@param AuthorizationCheckerInterface $auth            The authorization checker service.

@throws NotFoundHttpException When the production is not found.
@throws AccessDeniedException When the user is not a member of the production.

@return Response
Create a new post .