Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ControllerResolver.php

function ControllerResolver::getControllerError

2 calls to ControllerResolver::getControllerError()
ControllerResolver::createController in vendor/symfony/http-kernel/Controller/ControllerResolver.php
Returns a callable for the given controller.
ControllerResolver::getController in vendor/symfony/http-kernel/Controller/ControllerResolver.php

File

vendor/symfony/http-kernel/Controller/ControllerResolver.php, line 155

Class

ControllerResolver
This implementation uses the '_controller' request attribute to determine the controller to execute.

Namespace

Symfony\Component\HttpKernel\Controller

Code

private function getControllerError(mixed $callable) : string {
    if (\is_string($callable)) {
        if (str_contains($callable, '::')) {
            $callable = explode('::', $callable, 2);
        }
        else {
            return \sprintf('Function "%s" does not exist.', $callable);
        }
    }
    if (\is_object($callable)) {
        $availableMethods = $this->getClassMethodsWithoutMagicMethods($callable);
        $alternativeMsg = $availableMethods ? \sprintf(' or use one of the available methods: "%s"', implode('", "', $availableMethods)) : '';
        return \sprintf('Controller class "%s" cannot be called without a method name. You need to implement "__invoke"%s.', get_debug_type($callable), $alternativeMsg);
    }
    if (!\is_array($callable)) {
        return \sprintf('Invalid type for controller given, expected string, array or object, got "%s".', get_debug_type($callable));
    }
    if (!isset($callable[0]) || !isset($callable[1]) || 2 !== \count($callable)) {
        return 'Invalid array callable, expected [controller, method].';
    }
    [
        $controller,
        $method,
    ] = $callable;
    if (\is_string($controller) && !class_exists($controller)) {
        return \sprintf('Class "%s" does not exist.', $controller);
    }
    $className = \is_object($controller) ? get_debug_type($controller) : $controller;
    if (method_exists($controller, $method)) {
        return \sprintf('Method "%s" on class "%s" should be public and non-abstract.', $method, $className);
    }
    $collection = $this->getClassMethodsWithoutMagicMethods($controller);
    $alternatives = [];
    foreach ($collection as $item) {
        $lev = levenshtein($method, $item);
        if ($lev <= \strlen($method) / 3 || str_contains($item, $method)) {
            $alternatives[] = $item;
        }
    }
    asort($alternatives);
    $message = \sprintf('Expected method "%s" on class "%s"', $method, $className);
    if (\count($alternatives) > 0) {
        $message .= \sprintf(', did you mean "%s"?', implode('", "', $alternatives));
    }
    else {
        $message .= \sprintf('. Available methods: "%s".', implode('", "', $collection));
    }
    return $message;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal