Php Zend framework-获取控制器内部的配置
我用它来构建zend应用程序 我试图获取我放在config/autoload/global.php和config/local.php.dist中的配置数据,但它返回 Zend\ServiceManager\Exception\ServiceNotFoundException 而且 在插件管理器Zend\Mvc\Controller\PluginManager中未找到名为getServiceLocator的插件 知道我怎样才能得到配置吗Php Zend framework-获取控制器内部的配置,php,zend-framework,zend-framework3,Php,Zend Framework,Zend Framework3,我用它来构建zend应用程序 我试图获取我放在config/autoload/global.php和config/local.php.dist中的配置数据,但它返回 Zend\ServiceManager\Exception\ServiceNotFoundException 而且 在插件管理器Zend\Mvc\Controller\PluginManager中未找到名为getServiceLocator的插件 知道我怎样才能得到配置吗 $config = $this->getService
$config = $this->getServiceLocator()->get('config');
为了做到这一点,您需要注入配置,因为getServiceLocator和所有其他定位器都已从ZF3中删除 在您的模块配置中,您有:
'controllers' => [
'factories' => [
Controller\IndexController::class => InvokableFactory::class,
],
],
您可以更改工厂以创建自己的工厂
Controller\IndexController::class => Controller\IndexControllerFactory::class,
代码如下:
final class IndexControllerFactory
{
public function __invoke(Container $container) : IndexController
{
$config = $container->get('config');
if (!isset($config['stuff']['stuff']) {
throw new \Exception('Please add the stuff.stuff parameter in the config');
}
$myParam = $config['stuff']['stuff'];
return new IndexController($myParam);
}
}
容器是一个PSR容器
在控制器中添加构造函数以接收所需的配置:
public function __construct(string $param)
{
$this->param = $param;
}
在这里,您的配置作为属性存在于您的类中。为了做到这一点,您需要注入配置,因为getServiceLocator和所有其他定位器都已从ZF3中删除 在您的模块配置中,您有:
'controllers' => [
'factories' => [
Controller\IndexController::class => InvokableFactory::class,
],
],
您可以更改工厂以创建自己的工厂
Controller\IndexController::class => Controller\IndexControllerFactory::class,
代码如下:
final class IndexControllerFactory
{
public function __invoke(Container $container) : IndexController
{
$config = $container->get('config');
if (!isset($config['stuff']['stuff']) {
throw new \Exception('Please add the stuff.stuff parameter in the config');
}
$myParam = $config['stuff']['stuff'];
return new IndexController($myParam);
}
}
容器是一个PSR容器
在控制器中添加构造函数以接收所需的配置:
public function __construct(string $param)
{
$this->param = $param;
}
在这里,您的配置作为属性存在于类中。目前使用Zend Framework 3的主分支。控制器中的getServiceLocator已被删除
所以,如果您想将一些变量从服务传递到控制器,您应该创建一个工厂。并在工厂实例化控制器时传递变量
例如:
您的控制器名称是应用程序模块中的IndexController。工厂类是IndexController工厂
应用程序\控制器\索引控制器工厂
希望这对目前使用Zend Framework 3的主分支有所帮助。控制器中的getServiceLocator已被删除
所以,如果您想将一些变量从服务传递到控制器,您应该创建一个工厂。并在工厂实例化控制器时传递变量
例如:
您的控制器名称是应用程序模块中的IndexController。工厂类是IndexController工厂
应用程序\控制器\索引控制器工厂
希望这有帮助这是为了澄清
在ZF3中,如果要在应用程序中创建任何需要的类,请使它们可用,并通过ServiceManager使它们在应用程序中可用。ServiceManager实现了一个存储已注册服务的容器。这是怎么回事?ZF使用一种称为factory的方法,简而言之,它创建对象。它帮助将服务存储到容器中。然后,我们可以使用ServiceManager从该容器中提取服务。让我们看看怎么做
ServiceManager本身就是一种服务
因此,使用工厂让ServiceManager实例在控制器(例如IndexController)中可用。这样我们就可以使用它获得任何服务
应用程序\控制器\索引控制器工厂
一旦IndexController工厂通过上述配置实例化IndexController,ServiceManager实例就可以通过IndexController的构造函数使用
<?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();
}
这是一个上传路径,无论你想上传到哪里
'module_config' => [
// Set the path as you want
'upload_path' => __DIR__ . '/../data/upload',
],
这是Application\Form\Filter\UploadFilter
这是使事物可用的一种方法。那么为什么是ServiceManager?这是为了使分散使用的对象停止。它删除隐藏的依赖项。这使得代码更清晰,更容易理解。原则是好的设计。这是为了澄清
在ZF3中,如果要在应用程序中创建任何需要的类,请使它们可用,并通过ServiceManager使它们在应用程序中可用。ServiceManager实现了一个存储已注册服务的容器。这是怎么回事?ZF使用一种称为factory的方法,简而言之,它创建对象。它帮助将服务存储到容器中。然后,我们可以使用ServiceManager从该容器中提取服务。让我们看看怎么做
ServiceManager本身就是一种服务
因此,使用工厂让ServiceManager实例在控制器(例如IndexController)中可用。这样我们就可以使用它获得任何服务
应用程序\控制器\索引控制器工厂
一旦IndexController工厂通过上述配置实例化IndexController,ServiceManager实例就可以通过IndexController的构造函数使用
<?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();
}
这是一个上传路径,无论你想上传到哪里
'module_config' => [
// Set the path as you want
'upload_path' => __DIR__ . '/../data/upload',
],
这是Application\Form\Filter\UploadFilter
这是使事物可用的一种方法。那么为什么是ServiceManager?这是为了使分散使用的对象停止。它删除隐藏的依赖项。这使得代码更清晰,更容易理解。原则是好的设计
'module_config' => [
// Set the path as you want
'upload_path' => __DIR__ . '/../data/upload',
],
<?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(),
));
}
}