DemoDataController - "DemoDataController does not exist in"

Hello.
I was following the video: https://academy.shopware.com/courses/take/shopware-6-developer-training-english/lessons/9225170-data-storefront
When I try to access the website the following error appears:

"

{„errors“:[{„code“:„0“,„status“:„500“,„title“:„Internal Server Error“,„detail“:„Class \DemoDataController does not exist in /Users/alphabetus/Documents/repos/shopware01/custom/plugins/SwagShopFinder/src/Core/Api/DemoDataController.php (which is being imported from \u0022/Users/alphabetus/Documents/repos/shopware01/custom/plugins/SwagShopFinder/src/Resources/config/routes.xml\u0022). Make sure annotations are installed and enabled.“}]}

I’m stuck for a while trying to understand what is wrong.
The video never shows the full code that the guy is using only references.
Probably some imports are missing

DemoDataController.php goes as follows:
 

countryRepository = $countryRepository;
    $this->shopFinderRepository = $shopFinderRepository;
  }

    /**
     * @Route("/api/v{version}/_action/swag-shop-finder/generate", name="api.custom.swag_shop_finder.generate", methods={"POST"})
     * @param Context $context
     * @return Response
     * @throws CountryNotFoundException
     * @throws InconsistentCriteriaIdsException
     */
  public function generate(Context $context): Response
  {
    $faker = Factory::create();
    $country = $this->getActiveCountry($context);

    $data = [];
    for($i = 0; $i < 50; $i++){
      $data[] = [
        'id' => Uuid::randomHex(),
        'active' => true,
        'name' => $faker->name,
        'street' => $faker->streetAddress,
        'postcode' => $faker->postcode,
        'city' => $faker->city,
        'countryId' => $country->getId()
      ];
    }

    $this->shopFinderRepository->create($data, $context);
    return new Response('', Response::HTTP_NO_CONTENT);
  }

  /**
  * @param Context $context
  * @return CountryEntity
  * @throws CountryNotFoundException
  * @throws InconsistentCriteriaIdsException;
  **/
  private function getActiveCountry(Context $context): CountryEntity
  {
    $criteria = new Criteria();
    $criteria->addFilter(new EqualsFilter('active', '1'));
    $criteria->setLimit(1);

    $country = $this->countryRepository->search($criteria, $context)->getEntities()->first();
    if ($country === null) {
      throw new CountryNotFoundException('');
    }

    return $country;
  }
}

 

Hello,

you use the wrong imports.

Here is my working DemoDataController

 

countryRepository = $countryRepository;
    $this->shopFinderRepository = $shopFinderRepository;
  }

  /**
   * @Route("/api/v{version}/_action/swag-shop-finder/generate", name="api.custom.swag_shop_finder.generate", methods={"POST"})
   * return Response
   */
  public function generate(Context $context): Response {
    $faker = Factory::create();
    $country = $this->getActiveCountry($context);

    $data = [];
    for ($i = 0; $i < 50; $i++) {
      $data[] = [
        'id' => Uuid::randomHex(),
        'active' => true,
        'name' => $faker->name,
        'street' => $faker->streetAddress,
        'postCode' => $faker->postcode,
        'city' => $faker->city,
        'countryId' => $country->getId(),
      ];
    }
    $this->shopFinderRepository->create($data, $context);

    return new Response('', Response::HTTP_NO_CONTENT);
  }

   /**
   * @param Context $context
   * @return CountryEntity
   * @throws CountryNotFoundException
   * @throws InconsistentCriteriaIdsException
   */
  private function getActiveCountry(Context $context): CountryEntity {
    $criteria = new Criteria();
    $criteria->addFilter(new EqualsFilter('active', '1'));
    $criteria->setLimit(1);

    $country = $this->countryRepository->search($criteria, $context)->getEntities()->first();
    if($country === null) {
      throw new CountryNotFoundException('');
    }
    return $country;
  }

}

 

1 „Gefällt mir“

Hi for me helped adding the namespace to my controller. Obviously on training course there is no namespace, and it doesn’t work. Also i had to add created_ad and updated_at to my migration code.

my DemoDataController code is
<?php declare(strict_types=1);

use Faker\Factory;

use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\System\Country\CountryEntity;
use Shopware\Core\System\Country\Exception\CountryNotFoundException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;

/**
 * @RouteScope(scopes={"api"})
 */

class DemoDataController extends AbstractController
{
    /**
     * @var EntityRepositoryInterface
     */
    private $countryRepository;

    /**
     * @var EntityRepositoryInterface
     */
    private $shopFinderRepository;


    public function __construct(
        EntityRepositoryInterface $countryRepository, EntityRepositoryInterface $shopFinderRepository){
        $this->countryRepository = $countryRepository;
        $this->shopFinderRepository = $shopFinderRepository;
    }

    /**
     * @Route("/api/v{version}/_action/swag-shop-finder/generate", name="api.custom.swag_shop_finder.generate", methods={"POST"})
     * @param Context $context
     * @return Response
     */
    public function generate(Context $context): Response
    {
        $faker 			= Factory::create();
        $country 		= $this->getActiveCountry($context);
        $data = [];
        for($i=0; $i<=25; $i++){
            $data[] = [
                'id' 				=> Uuid::randomHex(),
                'name' 				=> $faker->name,
                'countryId' 		=> $country->getId(),
                'active'	        => true,
                'postal_code'       => $faker->postcode,
            ];
        }

        $this->shopFinderRepository->create($data, $context);
        return new Response("", Response::HTTP_NO_CONTENT);
    }

    /**
     * @param Context $context
     * @return CountryEntity
     * @throws CountryNotFoundException
     * @throws InconsistentCriteriaIdsException
     */
    private function getActiveCountry(Context $context): CountryEntity
    {
        $criteria = New Criteria();
        $criteria->addFilter(new EqualsFilter('active', '1'));
        $criteria->setLimit(1);

        $result = $this->countryRepository->search($criteria, $context)->getEntities->first();

        if($result===null){
            throw new CountryNotFoundException();
        }
        return $result;

    }

}

I got error 
{
  "errors": [
    {
      "code": "0",
      "status": "500",
      "title": "Internal Server Error",
      "detail": "The controller for URI \"/api/v2/_action/swag-shop-finder/generate\" is not callable: Controller \"DemoDataController\" does neither exist as service nor as class."
    }
  ]
}

can you help??@rahmspinat

I had the same problem and i solved it by checking my ide (using phpstorm).
In my case there were some imports missing. In the very first i forgot the namespace definition in my DemoDataController.php and this did lead to the exact same error (DemoDataController class is missing).
Later on i had to add some use statements in addition and phpstorm helped me with this quite comfortable.

1 „Gefällt mir“