-1

我正在关注这个Drupal 8 模块开发 101教程。YouTube 视频的时间是 37:15 到 45:14。我不断收到此错误:

致命错误:第 11 行的 C:\Users\myName\Sites\devdesktop\drupal-8.0.5\modules\dino_roar\src\Controller\RoarController.php 中找不到类 'Drupal\dino_roar\DinoServices\HelloGenerator'

文件夹结构: 在此处输入图像描述

HelloGenerator.php

<?php

namespace Drupal\dino_roar\DinoServices;

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

咆哮控制器.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);
    }
}

恐龙吼.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

致命错误指向 RoarController.php 文件中的这行代码: $helloGenerator = new HelloGenerator();

这是 Symfony 版本。我在这个窗口中找不到它说 1,2 或 3。

在此处输入图像描述

4

1 回答 1

1

首先,你的 RoarController 类需要扩展Controller 类

class RoarController

use Symfony\Bundle\FrameworkBundle\Controller\Controller

class RoarController extends Controller

编辑

好的,现在改变

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

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

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);
}

您不了解如何使用服务,我邀请您阅读此http://symfony.com/doc/current/book/service_container.html#creating-configuring-services-in-the-container

于 2016-04-12T14:40:57.987 回答