0

I am trying to build a service that will leverage the Pathauto service so my class looks like this.

namespace Drupal\some_system;

use Drupal\pathauto\PathautoGenerator;
use Drupal\pathauto\AliasCleaner;
use Symfony\Component\DependencyInjection\ContainerInterface;

class ProcessUrl
{
  /**
   * @var PathautoGenerator
   */

  protected $pathautoGenerator;

  /**
   * @var AliasCleaner
   */
  protected $aliasCleaner;

  /**
   * ProcessUrl constructor.
   * @param PathautoGenerator $pathautoGenerator
   * @param AliasCleaner $aliasCleaner
   */

  public function __construct(PathautoGenerator $pathautoGenerator, AliasCleaner $aliasCleaner)
  {
    $this->pathautoGenerator = $pathautoGenerator;
    $this->aliasCleaner = $aliasCleaner;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('pathauto.generator'),
      $container->get('pathauto.alias_cleaner')
    );
  }

  public function testurl(){
    dump("Need to reach Here");
    exit();
  }

}

and my services.yml file looks like this.

services:
 some_group_system.process_url:
    class: Drupal\some_system\ProcessUrl
    argumrnts: ['@pathauto.generator','@pathauto.alias_cleaner']

Now when I call this service \Drupal::service('some_group_system.process_url')->testurl(); it throws an error to me saying

Too few arguments to function Drupal\some_system\ProcessUrl::__construct(), 0 passed in /var/www/html/web/core/lib/Drupal/Component/DependencyInjection/Container.php on line 265 and exactly 2 expected

I cleared cache and reran but not sure why it is saying this. Please help on this.

4

You have a typo in your services.yml file, you misspelled arguments.

Additionally, for a service, you don't need to (and shouldn't) define the create method. Your dependent services are injected from services.yml, but keep your constructor as is.

The only time you need to define the create method to inject dependencies is when working with plugins, controllers, forms, etc as mentioned in the dependency injection documentation on d.o.

-1

Sorry it was a spelling mistake

 argumrnts: ['@pathauto.generator','@pathauto.alias_cleaner']

should be

arguments: ['@pathauto.generator','@pathauto.alias_cleaner']

Do not need even create method but got confused.

Not the answer you're looking for? Browse other questions tagged or ask your own question.