0

In my Controller, I have so many "use" statements:

Controller.php

namespace App\Controller; 



  use Doctrine\Common\Collections\ArrayCollection;
  use Symfony\Component\HttpFoundation\Response;
  use Symfony\Component\HttpFoundation\Request;
  use Symfony\Component\Routing\Annotation\Route;
  use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  use Symfony\Component\Serializer\Serializer;
  use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
  use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
  use Symfony\Component\Serializer\Encoder\JsonEncoder;
  use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  use Symfony\Component\HttpFoundation\JsonResponse;
  use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  use Doctrine\ORM\Query\ResultSetMapping;
  use App\Repository\PagesRepository;
  use Doctrine\ORM\EntityManagerInterface;
  use Symfony\Component\Form\Extension\Core\Type\TextType;
  use Symfony\Component\Form\Extension\Core\Type\ButtonType;
  use Symfony\Component\Form\Extension\Core\Type\EmailType;
  use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
  use Symfony\Component\Form\Extension\Core\Type\DateType;
  use Symfony\Component\Form\Extension\Core\Type\FormType;
  use Symfony\Component\Form\Extension\Core\Type\NumberType;
  use App\Form\Type\ColorSelectorType;
  use Symfony\Component\Form\Extension\Core\Type\FileType;

To clear up my code I would like to put them on another page, like a service.

One approach was, to put the code on the page Namespace.php and include it:

  include 'App\Service\Namespace.php';

But I get the error message:

Warning: include(App\Service\Namespace.php): failed to open stream: No such file or directory

1
  • why you are not using it ? use App\Service\Namespace.php ?? Commented Oct 8, 2019 at 6:49

1 Answer 1

4

The direct cause of your error is that you're using a relative path to include the file. Always use absolute paths with __DIR__, for example.

But even if you provide the correct path, this still won't work. You can't move use statements to another file. These statements always work in the scope of the current file, and nowhere else.

It's good, however, that you want to clean up your class by reducing the number of use statements. When you have so many use statements in your class, it's an indicator that the class is becoming big and messy, and will grow more and more unmaintainable over time. Fixing this can be achieved by splitting the class into smaller ones. For example, you could extract all the places that use all those Symfony\Component\Form classes to a new Form Type class, and use just this one Form Type instead of building the whole form in the controller.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.