0

しばらく検索して試してみましたが、何が間違っているのかわかりません...かなりのドキュメントを読んで、さまざまなアプローチを試しました。

あなたの一人が私を助けてくれることを願っています..

私の考え: Application というモジュールがあります。このモジュールには\Application\Controller\IndexController、アプリケーションのホームページを表示するために使用される があります。IndexController には のオブジェクトが必要な\Zend\http\Clientので、返される構成で factory キーを使用し、\Application\Module->getServiceConfig()必要な param を持つコンストラクターを追加する必要があり\Zend\Http\Client $clientます。

残念ながら、これは機能していません..

私のコード:

<?php
// module/Application/Module.php

namespace Application;

use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;

class Module
{
    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'IndexController' => function($serviceManager) {
                    $httpClient = \Zend\Http\Client;

                    return new IndexController($httpClient);
                },
            ),
        );
    }

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }
}

<?php
// module/Application/config/module.config.php

return array(
    'router' => array(
        'routes' => array(
            'home' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route' => '/',
                    'defaults' => array(
                        'controller' => 'Application\Controller\Index',
                        'action' => 'index',
                    ),
                ),
            )
        ),
    ),
    'service_manager' => array(
        'factories' => array(
            'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
        ),
    ),
    'translator' => array(
        'locale' => 'en_US',
        'translation_file_patterns' => array(
            array(
                'type' => 'gettext',
                'base_dir' => __DIR__ . '/../language',
                'pattern' => '%s.mo',
            ),
        ),
    ),
    'controllers' => array(
        'invokables' => array(
            'Application\Controller\Index' => 'Application\Controller\IndexController'
        ),
    ),
    'view_manager' => array(
        'display_not_found_reason' => true,
        'display_exceptions' => true,
        'doctype' => 'HTML5',
        'not_found_template' => 'error/404',
        'exception_template' => 'error/index',
        'template_map' => array(
            'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
            'application/index/index' => __DIR__ . '/../view/index/index.phtml',
            'error/404' => __DIR__ . '/../view/error/404.phtml',
            'error/index' => __DIR__ . '/../view/error/index.phtml',
        ),
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),
    ),
);

<?php
// module/Application/src/Application/Controller/IndexController.php

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class IndexController extends AbstractActionController
{
    protected $httpClient;

    public function __construct(\Zend\Http\Client $client)
    {
        $this->httpClient = $client;
    }

    public function getHttpClient()
    {
        return $this->httpClient;
    }

    public function indexAction()
    {                
        $httpClient = $this->getHttpClient();

        return new ViewModel();
    }
}

エラー:

キャッチ可能な致命的なエラー: Application\Controller\IndexController::__construct() に渡される引数 1 は Zend\Http\Client のインスタンスである必要があります。 170 であり、/var/www/module/Application/src/Application/Controller/IndexController.php の 13 行目に定義されています

4

1 に答える 1

3

求めていることを実行するには、Module.php にメソッドを実装する必要がありgetControllerConfigます。コントローラーには独自のマネージャーがあり、getServiceConfig から持っているものを削除し、代わりに以下を使用します。

public function getControllerConfig()
{
    return array(
        'factories' => array(
            'Application\Controller\Index' => function($serviceManager) {
                $httpClient = new \Zend\Http\Client;
                return new \Application\Controller\IndexController($httpClient);
            },
        ),
    );
}

ファクトリと競合するため、module.config.php のコントローラー呼び出し可能オブジェクトからもこの行を削除する必要があります。

'controllers' => array(
    'invokables' => array(
        // this line's no longer needed
        //'Application\Controller\Index'      => 'Application\Controller\IndexController',
    ),
),
于 2013-03-08T15:34:35.447 に答える