3

I create service for add formType then persist object and in controller I invoke data but I have error shown on below image:

in controller i extend class abstractController content getHandler and I have view newskill.html.twig

enter image description here

Code SkillController.php:

<?php 

    namespace AppBundle\Controller\Condidate;

    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Symfony\Component\HttpFoundation\Request;
    use AppBundle\Entity\Skill;
    use AppBundle\Controller\AbstractController;
    use AppBundle\Form\SkillType;

    /**
     *Class SkillController.
     */
    class SkillController extends AbstractController
    {

         /**
         *function handler.
         */
         protected function getHandler(){
            //var_dump('test');
        }

        /**
         *function addSkill
         * @param Request $request
         * @return \Symfony\Component\Form\Form The form
         */
        public function addSkillAction(Request $request) {
             $skill = $this->getHandler()->post();

             if ($skill instanceof \AppBundle\Entity\Skill) {
                return $this->redirectToRoute('ajouter_info');

        }

           return $this->render('skills/newskill.html.twig', array(
            'form' => $form->createView(),));

        }


        }

Code SkillHandler.php:

<?php 

namespace AppBundle\Handler;

use AppBundle\Handler\HandlerInterface;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\Skill;
use Doctrine\ORM\EntityManager;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Form\formFactory;

/**
 * SkillHandler.
 */
class SkillHandler implements HandlerInterface {

    /**
     *
     * @var EntityManager 
     */
    protected $em;

    /**
     *
     * @var formFactory 
     */
    private $formFactory;


    /**
     *function construct.
     */
    public function __construct(EntityManager $entityManager, formFactory $formFactory)
        {
            $this->em = $entityManager;
            $this->formFactory = $formFactory;
        }

     /**
       *function post
       */
    public function post(array $parameters, array $options = []) {
        $form = $this->formFactory->create(\AppBundle\Form\SkillType::class, $object, $options);
        $form->submit($parameters);
        if ($form->isValid()) {
            $skill = $form->getData();
            $this->persistAndFlush($skill);
            return $skill;
        }

        return $form->getData();
    }

    /**
     *function persisteAndFlush
     */

    protected function persistAndFlush($object) {
       $this->em->persist($object);
       $this->em->flush();
    }


    /**
     *function get
     */
     public function get($id){
        throw new \DomainException('Method SkillHandler::get not implemented');

     }

     /**
      *function all
      */
      public function all($limit = 10, $offset = 0){
        throw new \DomainException('Method SkillHandler::all not implemented');
      }


    /**
     *function put
     */
    public function put($resource, array $parameters, array $options){
        throw new \DomainException('Method SkillHandler::put not implemented');
    }

    /**
     *function patch
     */
    public function patch($resource, array $parameters, array $options){
        throw new \DomainException('Method SkillHandler::patch not implemented');
    }

    /**
     *function delete
     */
     public function delete($resource){
        throw new \DomainException('Method SkillHandler::delete not implemented');
         }
}

code services.yml:

skill_add:
    class: AppBundle\Handler\SkillHandler
    arguments: 
        - "@doctrine.orm.entity_manager"
        - "@form.factory"
    public: true

Any help would be appreciated.

Jaymin
  • 1,643
  • 1
  • 18
  • 39
devit2017
  • 297
  • 2
  • 5
  • 14
  • Which version of Symfony are you using? You need to define the controller as a service and inject the skill handler into it. Exactly how you do that depends on the Symfony version. However, while I gather you are trying to separate the controller and the handler functionality, I don't think your approach is going to work. Might try getting something to work first and then breaking it apart. – Cerad Nov 01 '17 at 12:09
  • Relevant page: https://stackoverflow.com/q/12351737/2943403 – mickmackusa Nov 27 '20 at 23:09

2 Answers2

3

Your $this->getHandler() retruns null.

Solution can be checking if $this->getHandler() doesn't return null in first place.

if (!$this->getHandler()) {
    throw new \Exception(sprintf('Handler cannot be null')
} else {
    $skill = $this->getHandler()->post();
}
Łukasz D. Tulikowski
  • 1,440
  • 1
  • 17
  • 36
  • 2
    That is intended. You are throwing an exception. Your getHandler method is empty. You have to add some content to it to properly be able to call `->post()` on it – Xatenev Nov 01 '17 at 11:49
  • @devit2017 So can't call `->post()` on `$this->getHandler()` if returns `null`. Imagine calling `null->post()` – Łukasz D. Tulikowski Nov 01 '17 at 11:59
1

Try this, firstly you should take your handler into getHandler() method at your Controller.

protected function getHandler(){
    return $this->get('skill_add');
}
Mert Simsek
  • 1,550
  • 9
  • 19