Rafa
Rafa

Reputation: 66

The controller for URI is not callable. Controller does neither exist as service nor as class

I'm getting the error in the title when trying to call a controller.

Here's my configuration:

config/services.yaml

parameters:
  locale: 'en'
  localhost_base_url: '%env(string:localhost_base_url)%'

services:
  # default configuration for services in *this* file
  _defaults:
    autowire: true      # Automatically injects dependencies in your services.
    autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
    public: false

  # makes classes in src/MlngAppBundle/ available to be used as services
  # this creates a service per class whose id is the fully-qualified class name
  MlngApp\MlngAppBundle\:
    resource: '../src/MlngApp/MlngAppBundle/*'
    exclude:
      - '../src/MlngApp/MlngAppBundle/MlngAppBundle.php'
      - '../src/MlngApp/MlngAppBundle/{Common,Event,Location,People,User}/Domain/{Entity,Dto,Exception,Invariant}/*'
      - '../src/MlngApp/MlngAppBundle/{Common,Event,Location,People,User}/Tests/*'
      - '../src/MlngApp/MlngAppBundle/{Common,Event,Location,People,User}/Infrastructure/CommandLine/*'

config/packages/config.yaml

imports:
- { resource: domains/* }
- { resource: "@MlngAppBundle/Common/Infrastructure/Resources/config/services.xml" }
- { resource: "@MlngAppBundle/Location/Infrastructure/Resources/config/services.xml" }

And the "Common" services.xml file:

<?xml version="1.0" encoding="UTF-8" ?>

<container xmlns="http://symfony.com/schema/dic/services"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

    <services>

        <!-- repository classes -->
        <prototype
                namespace="MlngApp\MlngAppBundle\"
                resource="../../../../../../../src/MlngApp/MlngAppBundle/{Common,Event,Location,People,User}/Infrastructure/Repository/"
                autoconfigure="true"
                autowire="true"
                public="true"
        >
            <tag name="doctrine.repository_service"/>
        </prototype>

        <!-- repository interface classes -->
        <prototype
                namespace="MlngApp\MlngAppBundle\"
                resource="../../../../../../../src/MlngApp/MlngAppBundle/{Common,Event,Location,People,User}/Domain/Repository/"
                autoconfigure="true"
                autowire="true"
                public="true"
        >
            <tag name="doctrine.repository_service"/>
        </prototype>

        <!-- controllers -->
        <prototype
                namespace="MlngApp\MlngAppBundle\"
                resource="../../../../../../../src/MlngApp/MlngAppBundle/{Common,Event,Location,People,User}/Api/Controller"
        >
            <tag name="controller.service_arguments" />
            <call method="setContainer">
                <argument type="service" id="service_container"/>
            </call>
        </prototype>

    </services>
</container>

Concerning routes, I have:

config/routes.yaml

location:
  resource: "@MlngAppBundle/Location/Api/Resources/config/routing.xml"
  prefix: /api
  type: xml

and

@MlngAppBundle/Location/Api/Resources/config/routing.xml

<?xml version="1.0" encoding="UTF-8"?>
<routes xmlns="http://symfony.com/schema/routing"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"
>
    <import resource="routing/location.xml" prefix="/location"/>

    <route id="testingstuff" path="/donothing" methods="GET">
        <default key="_controller">MlngApp/MlngAppBundle/Location/Api/Controller/AnotherController::anotherEndpoint</default>
    </route>
</routes>

and the controller:

<?php

namespace MlngApp\MlngAppBundle\Location\Api\Controller;

use FOS\RestBundle\Controller\AbstractFOSRestController;
use Symfony\Component\HttpFoundation\Request;

class AnotherController extends AbstractFOSRestController
{
    public function __construct()
    {

    }

    public function anotherEndpoint(Request $request) {
        echo "do nothing";
    }
}

When I go to http://127.0.0.1:8000/api/donothing I get

The controller for URI "/api/donothing" is not callable: Controller "MlngApp/MlngAppBundle/Location/Api/Controller/AnotherController" does neither exist as service nor as class.

I understand that it means the AnotherController class is undefined but it gets defined in services.yaml and services.xml

Using Symfony 5.4

I don't know where to look for any clue.

Any suggestions?

Upvotes: 2

Views: 5686

Answers (3)

Oleh Diachenko
Oleh Diachenko

Reputation: 662

In my case this error was happen after upgrade symfony/sonata version. Reason is in controller name, now it should be passed fully.

Old format: - App:Admin/ManualCronCommands

New format: - App\Controller\Admin\ManualCronCommandsController

Here is full arguments example:

    arguments:
        - ~
        - App\Entity\JobLogs
        - App\Controller\Admin\ManualCronCommandsController

Upvotes: 0

For me i've added "use 'Name_Space'" of my controller in the entity and it works!!

Upvotes: 2

Rafa
Rafa

Reputation: 66

Thanks, people.

@msg, you were right. I'm using the incorrect backslashes. I am a bit ashamed since this is kind of rookie mistakes. Shame on me.

In @MlngAppBundle/Location/Api/Resources/config/routing.xml it should be

        <default key="_controller">MlngApp\MlngAppBundleLocation\Api\Controller\AnotherController::anotherEndpoint</default>

Working now.

As a side note, I'm trying this configuration as a way to work with DDD with an arquitectural design proposed in https://www.fabian-keller.de/blog/domain-driven-design-with-symfony-a-folder-structure/

Again, thanks @Cerad and @msg for your time and advices. :)

Upvotes: 1

Related Questions