【ZF2】Zend Framework 2 Helloword 入门实例

本实例是一个hello word例子(本实例基于2.2.1版本构建,前提是你已经搭建好了本框架)

step 1:

在 Moduel 目录下建立我们的新模块 Helloword,并建立如下文件目录

/ Moduel
/ Helloword
 / config
/ src
/ Helloword
/ Controller
/Model
/ view
/ helloword
/helloword

step 2:

建立 Module.php 文件 (path:/module/Helloword/Module.php)

<?php
namespace Helloword;

class Module
{
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
}

 

step 3:

建立 module.config.php 文件 ( path:/module/Helloword/config/module.config.php )

<?php

return array(

'controllers' => array(
'invokables' => array(
'Helloword\Controller\Helloword' => 'Helloword\Controller\HellowordController',
),
),

'view_manager' => array(
'template_path_stack' => array(
'album' => __DIR__ . '/../view',
),
),
);

step 4:

在主模块中添加当前模块
打开:config/application.config.php文件
找到

'modules' => array(
'Application',
),

在其中添加helloword模块,即修改为

'modules' => array(
'Application',
'Helloword',
),

step 5:

在建立我们的第一个控制器:HellowordController.php (path:/module/helloword/src/Helloword/Controller/HellowordController.php)

<?php
namespace Helloword\Controller;

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

class HellowordController extends AbstractActionController
{
public function indexAction()
{
return new ViewModel(array('hello' => 'hi'));
}
}

step 6:

建立视图文件 index.phtml(这个视图文件同HellowordController::indexAction对应)
( path: /module/Helloword/view/helloword/helloword/index.phtml )

<?php  
echo $this->hello;
?>
这是一个helloword程序

输出:

hi
这是一个helloword程序

step 7:

设置路由使得我们能够访问

( path: /module/Helloword/config/module.config.php )

<?php
return array(

'router' => array(
'routes' => array(
'helloword' => array(
'type'    => 'segment',
'options' => array(
'route'    => '/helloword[/:action]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'helloword\Controller\helloword',
'action'     => 'index',
),
),
),
)
),

......

);

最后就可以访问:site/helloword了;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值