- Создать папку application/src/service
- Создать нужный класс
- Зарегестрировать его в сервисах application.config.php
- Использовать его в контроллере (получить сервис менеджер; получить сервис с заданным ключом)
IndexController.php
<?php
/**
* @link http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class IndexController extends AbstractActionController {
public function indexAction() {
return new ViewModel();
}
public function pageAction() {
// $serviceManager = $this->getEvent()->getApplication()->getServiceManager();
// $convert = $serviceManager->get("currencyService");
//
// echo "<pre>";
// var_dump($convert->converterEurToUsd(1000));
// echo "</pre>";
return new ViewModel([
'number' => 123456
]);
}
public function oneTwoThreeAction() {
return new ViewModel([
'string' => 'its ok'
]);
}
}
CurrencyConverter.php
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace Application\Service;
class CurrencyConverter
{
public function converterEurToUsd($amount)
{
return $amount * 1.25;
}
}
application.config.php
<?php
/**
* If you need an environment-specific system or application configuration,
* there is an example in the documentation
* @see https://docs.zendframework.com/tutorials/advanced-config/#environment-specific-system-configuration
* @see https://docs.zendframework.com/tutorials/advanced-config/#environment-specific-application-configuration
*/
return [
// Retrieve list of modules used in this application.
'modules' => require __DIR__ . '/modules.config.php',
// These are various options for the listeners attached to the ModuleManager
'module_listener_options' => [
// This should be an array of paths in which modules reside.
// If a string key is provided, the listener will consider that a module
// namespace, the value of that key the specific path to that module's
// Module class.
'module_paths' => [
'./module',
'./vendor',
],
// An array of paths from which to glob configuration files after
// modules are loaded. These effectively override configuration
// provided by modules themselves. Paths may use GLOB_BRACE notation.
'config_glob_paths' => [
realpath(__DIR__) . '/autoload/{{,*.}global,{,*.}local}.php',
],
// Whether or not to enable a configuration cache.
// If enabled, the merged configuration will be cached and used in
// subsequent requests.
'config_cache_enabled' => true,
// The key used to create the configuration cache file name.
'config_cache_key' => 'application.config.cache',
// Whether or not to enable a module class map cache.
// If enabled, creates a module class map cache which will be used
// by in future requests, to reduce the autoloading process.
'module_map_cache_enabled' => true,
// The key used to create the class map cache file name.
'module_map_cache_key' => 'application.module.cache',
// The path in which to cache merged configuration.
'cache_dir' => 'data/cache/',
// Whether or not to enable modules dependency checking.
// Enabled by default, prevents usage of modules that depend on other modules
// that weren't loaded.
// 'check_dependencies' => true,
],
// Used to create an own service manager. May contain one or more child arrays.
// 'service_listener_options' => [
// [
// 'service_manager' => $stringServiceManagerName,
// 'config_key' => $stringConfigKey,
// 'interface' => $stringOptionalInterface,
// 'method' => $stringRequiredMethodName,
// ],
// ],
// Initial configuration with which to seed the ServiceManager.
// Should be compatible with Zend\ServiceManager\Config.
'service_manager' => [
'services' => [
'currencyService' => new \Application\Service\CurrencyConverter()
],
],
];
page.phtml
<?php
$this->escapeHtml($number);
$this->headTitle('Page');