Zend 框架 - 在控制器中获取配置 [英] Zend framework - get config inside controller

查看:32
本文介绍了Zend 框架 - 在控制器中获取配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用它来构建 zend 应用程序.http://github.com/zendframework/ZendSkeletonApplication

I'm using this to build zend application. http://github.com/zendframework/ZendSkeletonApplication

我正在尝试获取我放在 config/autoload/global.phpconfig/local.php.dist 中的配置数据,但是它返回

I'm trying to get the config data I put inside the config/autoload/global.php and config/local.php.dist with the bottom line but it returns

Zend\ServiceManager\Exception\ServiceNotFoundException

Zend\ServiceManager\Exception\ServiceNotFoundException

还有

在插件管理器 Zend\Mvc\Controller\PluginManager 中找不到名为getServiceLocator"的插件

A plugin by the name "getServiceLocator" was not found in the plugin manager Zend\Mvc\Controller\PluginManager

知道如何获取配置吗?

$config = $this->getServiceLocator()->get('config');

推荐答案

这是为了澄清

ZF3 中,如果您要在应用程序中创建任何需要的类,请使它们可用,并通过 ServiceManager 在您的应用程序中使用它们.ServiceManager 实现了一个存储注册服务的容器.那怎么样?ZF 使用一种称为 factory 的方法(简而言之,它创建对象).它有助于将服务存储到容器中.然后,我们可以使用 ServiceManager 从该容器中提取服务.让我们看看如何?

In ZF3, if you are creating any classes that need in your application, make them serviceable, make them available in your application via ServiceManager. ServiceManager implements a container which stores registered services. So how is that? ZF uses a method called factory (in short, it creates object). It helps store services into container. We can then pull services from that container using ServiceManager. Let's see how?

ServiceManager 本身就是一项服务.

因此,使用工厂,让我们在控制器(例如,IndexController)中提供 ServiceManager 实例.以便我们可以使用它获得任何服务.

So using a factory let's make ServiceManager instance available in a controller (For example, IndexController). So that we can get any service using it.

Application\Controller\IndexControllerFactory

<?php
namespace Application\Controller;

// This is the container 
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;

class IndexControllerFactory implements FactoryInterface
{
    public function __invoke(ContainerInterface $container, $requestedName, array $options = NULL)
    {   
        $serviceManager = $container->get('ServiceManager');
        return new IndexController($serviceManager);
    }    
}

让我们将 IndexControllerFactory 注册为 IndexController 的工厂,以便我们可以使用它.在 module.config.php

'controllers' => [
    'factories' => [
        Controller\IndexController::class => Controller\IndexControllerFactory::class,
    ],
],

一旦 IndexControllerIndexControllerFactory 实例化(通过上述配置),ServiceManager 实例就可以通过 IndexController 的构造函数使用了.

Once the IndexController is instantiated by IndexControllerFactory (by above configurations) the ServiceManager instance becomes available through IndexController's constructor.

<?php
namespace Application\Controller;

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

class IndexController extends AbstractActionController
{
    protected $serviceManager;

    public function __construct(ServiceManager $serviceManager) 
    {
        // Here we set the service manager instance 
        $this->serviceManager = $serviceManager;
    }

    public function indexAction()
    {
        // Use this as you want
        $config = $this->serviceManager->get('config');

        return new ViewModel();
    }

如果我们需要来自另一个类中的 config 服务而不是 控制器 的东西怎么办?例如,我们要将图像上传到特定目的地.那么我们如何修复上传路径呢?请参阅以下示例.

What if we need something from config service inside another class instead of the controller? For example, we want to upload images into a specific destination. So how would we fix the upload path? See the following example.

我们将通过RenameUpload 过滤器上传图片.它有一个名为 target 的选项,用于指定上传路径的目的地.让我们为上传过滤器创建另一个工厂.

We will upload images through RenameUpload filter. It has an option named target which specifies the destination of upload path. Let's create another factory for upload filter.

Application\Controller\Form\Filter\UploadFilterFactory

<?php
namespace Application\Form\Filter;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
use Application\Form\Filter\UploadFilter;

class UploadFilterFactory implements FactoryInterface
{

    public function __invoke(ContainerInterface $container, $requestedName, array $options = NULL)
    { 
        $config = $container->get('config');

        // Look! here we fix the upload path
        $uploadPath = $config['module_config']['upload_path'];

        // Here we're injecting that path
        return new UploadFilter($uploadPath);
    }
}

如果需要,对 UploadForm 执行相同的操作.这将是 UploadFormFactory

Do the same for the UploadForm if you need. This will be UploadFormFactory

将以下两个片段放入module.config.php.这是用于 UploadFilterFactory.

Put the following two snippets in the module.config.php. This is for UploadFilterFactory.

'service_manager' => [
    'factories' => [
        // UploadForm::class => UploadFormFactory::class, 
        UploadFilter::class => UploadFilterFactory::class,
    ],

    // Make an alias so that we can use it where we need
    // it could be uploadAction() inside any controller
    // $inputForm = $this->serviceManager->get('UploadForm');
    // $inputFilter = $this->serviceManager->get('UploadFilter');
    // $uploadForm->setInputFilter($inputFilter), for example
    'aliases' => [
        // 'UploadForm' => UploadForm::class,
        'UploadFilter' => UploadFilter::class,
    ],
],

这个用于上传路径,无论你想上传到哪里.

and this one for the upload path wherever you want to upload.

'module_config' => [
    // Set the path as you want
    'upload_path' => __DIR__ . '/../data/upload',
],

这是Application\Form\Filter\UploadFilter.

<?php
namespace Application\Form\Filter;

use Zend\InputFilter\InputFilter;
use Zend\Filter\File\RenameUpload;

class UploadFilter extends InputFilter
{
    protected $uploadPath;

    public function __construct(string $uploadPath)
    { 
        // We're assigning here so that we can use it
        // on the filter section.
        $this->uploadPath = $uploadPath;

        $this->prepareFilters();
    }

    public function prepareFilters()
    { 
        $this->add(array(
            'name' => 'image',
            'required' => true,
            'filters' => array(
                array(
                    'name' => RenameUpload::class,
                    'options' => array(
                        // Thus here we use it
                        'target' => $this->uploadPath,
                        'overwrite' => true,
                        'randomize' => true,
                        'use_upload_extension' => true,
                    ),        
                ),
            ),
            'validators' => array(),
        ));
    }
}

这是使事情变得有用的一种方式.那么为什么是 ServiceManager?这是为了停止分散的对象使用.它删除了隐藏的依赖项.这使代码干净且易于理解.原则是好的设计.

This is a one way of making things serviceable. So why is ServiceManager? This is for making scattered uses of objects stop. It removes hidden dependencies. This makes code clean and easier to understand. The principle is Good Design.

这篇关于Zend 框架 - 在控制器中获取配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆