这是我的module/Blog/config/module.config.php
<?php
return array(
'service_manager' => array(
'invokables' => array(
'Blog\Service\PostServiceInterface' => 'Blog\Service\PostService'
)
),
'controllers' => array(
'factories' => array(
'Blog\Controller\List' => 'Blog\Controller\ListControllerFactory'
)
),
'router' => array(
// Open configuration for all possible routes
'routes' => array(
// Define a new route called "post"
'post' => array(
// Define the routes type to be "Zend\Mvc\Router\Http\Literal", which is basically just a string
'type' => 'literal',
// Configure the route itself
'options' => array(
// Listen to "/blog" as uri
'route' => '/blog',
// Define default controller and action to be called when this route is matched
'defaults' => array(
'controller' => 'Blog\Controller\List',
'action' => 'index',
)
)
)
)
),
'view_manager' => array(
'template_path_stack' => array(
__DIR__ . '/../view',
),
)
);这是我的module/Blog/src/Blog/Controller/ListController.php
<?php
namespace Blog\Controller;
use Blog\Service\PostServiceInterface;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class ListController extends AbstractActionController
{
protected $postService;
public function __construct(PostServiceInterface $postService)
{
$this->postService = $postService;
}
public function indexAction()
{
return new ViewModel(array(
'posts' => $this->postService->findAllPosts()
));
}
}这是我的module/Blog/src/Blog/Factory/ListControllerFactory.php
<?php
namespace Blog\Factory;
use Blog\Controller\ListController;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class ListControllerFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
$realServiceLocator = $serviceLocator->getServiceLocator();
$postService = $realServiceLocator->get('Blog\Service\PostServiceInterface');
return new ListController($postService);
}
}最后,这是我的module/Blog/Module.php
<?php
namespace Blog;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use CodeGenerationUtils\Autoloader\AutoloaderInterface;
class Module implements ConfigProviderInterface,AutoloaderProviderInterface
{
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
// Autoload all classes from namespace 'Blog' from '/module/Blog/src/Blog'
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
)
)
);
}
}我得到了错误:
While attempting to create blogcontrollerlist(alias: Blog\Controller\List) an invalid factory was registered for this instance type.不知道这里出了什么问题,一切似乎都就绪了。
发布于 2015-10-22 15:17:44
在您的配置中,您指向Blog\Controller\ListControllerFactory,而您的文件位于Blog\Factory\ListControllerFactory中。
像这样更新,它会工作的:
'controllers' => array(
'factories' => array(
'Blog\Controller\List' => 'Blog\Factory\ListControllerFactory'
)
)https://stackoverflow.com/questions/33284482
复制相似问题