-1

I'm following this Drupal 8 module development 101 tutorial. It's between 37:15 to 45:14 on the YouTube video. I kept getting this error:

Fatal error: Class 'Drupal\dino_roar\DinoServices\HelloGenerator' not found in C:\Users\myName\Sites\devdesktop\drupal-8.0.5\modules\dino_roar\src\Controller\RoarController.php on line 11

Folder structure: enter image description here

HelloGenerator.php

<?php

namespace Drupal\dino_roar\DinoServices;

class HelloGenerator
{
    public function getHello($count){
        return "Gotten Hello ".$count;
    }
}

RoarController.php

<?php

namespace Drupal\dino_roar\Controller;

//use Drupal\dino_roar\DinoServices\HelloGenerator;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class RoarController extends Controller
{
    public function roar($count){
        //$helloGenerator = new HelloGenerator();
        $helloGenerator = $this->get('dino_roar.hello_generator');
        $hello = $helloGenerator->getHello($count);
        return new Response($hello);

        //return new Response("Hello World ".$count);
    }
}

dino_roar.info.yml

name: Dino ROAR
type: module
description: "ROAR at you"
package: Custom
core: 8.x

dino_roar.routing.yml

dino_says:
    path: /dino/says/{count}
    defaults:
        _controller: '\Drupal\dino_roar\Controller\RoarController::roar'
    requirements:
        _permission: 'access content'

dino_roar.services.yml

services:
    dino_roar.hello_generator:
        class: Drupal\dino_roar\DinoServices\HelloGenerator

The fatal error points to this line of code in the RoarController.php file: $helloGenerator = new HelloGenerator();

This is the Symfony version. I can't find it say 1,2, or 3 in this window.

enter image description here

2myCharlie
  • 1,587
  • 2
  • 21
  • 35

1 Answers1

1

First of all, your RoarController class needs to extends the Controller class

class RoarController

to

use Symfony\Bundle\FrameworkBundle\Controller\Controller

class RoarController extends Controller

EDIT

Ok now change

public function roar($count){
    $helloGenerator = new HelloGenerator();
    $hello = $helloGenerator->getHello($count);
    return new Response($hello);

    //return new Response("Hello World ".$count);
}

to

public function roar($count){
    $helloGenerator = $this->get('dino_roar.hello_generator');
    $hello = $helloGenerator->getHello($count);
    return new Response($hello);

    //return new Response("Hello World ".$count);
}

You didn't understand how use services that why I invite you to read this http://symfony.com/doc/current/book/service_container.html#creating-configuring-services-in-the-container

François Dupont
  • 406
  • 1
  • 4
  • 19
  • Just update my RoarController as you've suggested and I still get the same error. – 2myCharlie Apr 13 '16 at 12:45
  • Thanks! I'll take a look at the reading; however, I just tried your new Edit and it's still not working..same error. I'm not sure what I did that is different because the YouTube tutorial I'm following work just fine in the video. – 2myCharlie Apr 13 '16 at 13:21
  • Fatal error: Class 'Symfony\Bundle\FrameworkBundle\Controller\Controller' not found in C:\Users\myName\Sites\devdesktop\drupal-8.0.5\modules\dino_roar\src\Controller\RoarController.php on line 10 – 2myCharlie Apr 13 '16 at 13:36
  • What version of Symfony did you use ? – François Dupont Apr 13 '16 at 13:38
  • Line 10 is the { after the class RoarController extends Controller line. – 2myCharlie Apr 13 '16 at 13:38
  • I can't find it in the Symfony plugin window showing my initial post. – 2myCharlie Apr 13 '16 at 13:43