Tutorial Code
Below is the code used for the Zend Framework 2 Integration in Zend Studio tutorial. The code represents the final results of the tutorial.
-
Copy and paste the code into the created files.
-
Save before continuing to the next steps.
The code can also be downloaded together with all the project components from
Application Module
The code in this section belongs to the Application module in the project.
'module.config.php' file
<?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',
),
),
),
'application' => array(
'type' => 'Literal',
'options' => array(
'route' => '/application',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
),
),
'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/application/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
);
'index.phtml' file
<div class="hero-unit">
<h1><?php echo sprintf($this->translate('Welcome to %sZend Framework 2%s'), '<span class="zf-green">', '</span>') ?></h1>
<p><?php echo sprintf($this->translate('Congratulations! You have successfully installed the %sZF2 Skeleton Application%s. You are currently running Zend Framework version %s. This skeleton can serve as a simple starting point for you to begin building your application on ZF2.'), '<a href="https://github.com/zendframework/ZendSkeletonApplication" target="_blank">', '</a>', \Zend\Version\Version::VERSION) ?></p>
<p><a class="btn btn-success btn-large" href="download" target="_blank"><?php echo $this->translate('Download') ?> »</a></p>
</div>
<div class="row">
<div class="span4">
<h2><?php echo $this->translate('Follow Development') ?></h2>
<p><?php echo sprintf($this->translate('Zend Framework 2 is under active development. If you are interested in following the development of ZF2, there is a special ZF2 portal on the official Zend Framework website which provides links to the ZF2 %swiki%s, %sdev blog%s, %sissue tracker%s, and much more. This is a great resource for staying up to date with the latest developments!'), '<a href="http://framework.zend.com/wiki/display/ZFDEV2/Home">', '</a>', '<a href="http://framework.zend.com/zf2/blog">', '</a>', '<a href="http://framework.zend.com/issues/browse/ZF2">', '</a>') ?></p>
<p><a class="btn btn-success" href="http://framework.zend.com/zf2" target="_blank"><?php echo $this->translate('ZF2 Development Portal') ?> »</a></p>
</div>
<div class="span4">
<h2><?php echo $this->translate('Discover Modules') ?></h2>
<p><?php echo sprintf($this->translate('The community is working on developing a community site to serve as a repository and gallery for ZF2 modules. The project is available %son GitHub%s. The site is currently live and currently contains a list of some of the modules already available for ZF2.'), '<a href="https://github.com/zendframework/modules.zendframework.com">', '</a>') ?></p>
<p><a class="btn btn-success" href="http://modules.zendframework.com/" target="_blank"><?php echo $this->translate('Explore ZF2 Modules') ?> »</a></p>
</div>
<div class="span4">
<h2><?php echo $this->translate('Help & Support') ?></h2>
<p><?php echo sprintf($this->translate('If you need any help or support while developing with ZF2, you may reach us via IRC: %s#zftalk.2 on Freenode%s. We\'d love to hear any questions or feedback you may have regarding the beta releases. Alternatively, you may subscribe and post questions to the %smailing lists%s.'), '<a href="irc://irc.freenode.net/zftalk.2">', '</a>', '<a href="http://framework.zend.com/wiki/display/ZFDEV/Mailing+Lists">', '</a>') ?></p>
<p><a class="btn btn-success" href="http://webchat.freenode.net?channels=zftalk.2" target="_blank"><?php echo $this->translate('Ping us on IRC') ?> »</a></p>
</div>
</div>
Downloads Module
The code in this section belongs to the Downloads module in the project.
'module.config.php' file
<?php
return array(
'controllers' => array(
'invokables' => array(
'Downloads\Controller\Downloads' => 'Downloads\Controller\DownloadsController',
),
),
'router' => array(
'routes' => array(
'module-name-here' => array(
'type' => 'Literal',
'options' => array(
'route' => '/download',
'defaults' => array(
'__NAMESPACE__' => 'Downloads\Controller',
'controller' => 'Downloads',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'Downloads' => __DIR__ . '/../view',
),
),
'service_manager' => array(
'invokables' => array(
'downloadsService'=>'Downloads\DownloadsService',
),
),
'view_helpers' => array(
'invokables' => array(
'readableBytes' => 'Downloads\readableBytes',
),
)
);
'DownloadsController.php' file
<?php
namespace Downloads\Controller;
use Zend\Mvc\Controller\AbstractActionController;
class DownloadsController extends AbstractActionController
{
public function indexAction()
{
$downloadService = $this->getServiceLocator()->get('downloadsService');
return array("filesList"=> $downloadService->getFilesList(),
"filePath" => $downloadService->getFilesPath(),
"fileSize" => $downloadService->getFilesSize());
}
public function fooAction()
{
return array();
}
}
'DownloadsService.php' file
<?php
namespace Downloads;
class DownloadsService {
function getFilesList() {
return array("download1.zip", "download2.zip");
}
function getFilesPath() {
return "http://...";
}
function getFilesSize(){
return 50000000;
}
}
?>
'readableBytes.php' file
<?php
namespace Downloads;
use Zend\View\Helper\AbstractHelper;
class readableBytes extends AbstractHelper {
public function __invoke($bytes) {
return $bytes/ (1024*1024).'Mb';
}
}
'index.phtml' file
use Downloads\readableBytes;
use Downloads\readableBytes;
use Downloads\readableBytes;
<h1>Your Downloads</h1>
<?php
foreach ($filesList as $file) {
echo '<a href="'.$filePath.'">'.$file.'</a> ';
echo $this->readableBytes($fileSize).'<br>';
}
?>