您好,UncleToo欢迎您!  为了更好的浏览本站,请使用高版本浏览器
RSS  Tag     设为首页 | 加入收藏
 您所在的位置:首页 > PHP框架 > ZendFramework

在 Zend Framework 2 中扩展 ZfcUser 模块

作者:UncleToo  来源:原创  日期:2014-05-23 7:54:25
收藏  评论:( 1 )  阅读:593

       ZfcUser是Zend Framework 2中用户注册和身份验证模块。在这篇文章中,我们将讨论Zend Framework 2如何扩展使用ZfcUser模块


创建 Zend Framework 2 模块

       本文使用ZFTool工具创建模块(点击这里了解更多ZFTool工具),创建命令如下:

zftool.phar create module UncleTooBlog ./

       执行后,该工具将为我们创建一个名为UncleTooBlog的模块,并且生成该模块所需要的文件及文件夹,而且还会将新增加的模块添加到 application.config.php 文件的模块数组中。

       现在我们在地址栏输入http://localhost/UncleTooBlog,会出现404错误,这是对的,因为我么还没有创建任何视图与控制器

       打开 /module/UncleTooBlog/config/module.config.php 文件,并添加如下代码:

return array(
        'controllers' => array(
                'invokables' => array(
                        'UncleTooBlog\Controller\UncleTooBlog' => 'UncleTooBlog\Controller\BlogController'
                ),
        ),
        'view_manager' => array(
                'template_path_stack' => array(
                        'blog' => __DIR__ . '/../view',
                ),
        ),
);


准备UncleTooBlog模块

下面我们需要在模块配置文件中添加一些路由。

打开 /module/Blog/config/module.config.php 文件,并添加如下代码:

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


下面我们继续创建控制器与视图

创建控制器

在 /module/UncleTooBlog/src/Controller/目录下创建BlogController.php文件,并添加如下代码:

<?php
namespace UncleTooBlog\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class BlogController extends AbstractActionController
{
    /**
     * (non-PHPdoc)
     * @see \Zend\Mvc\Controller\AbstractActionController::indexAction()
     */
    public function indexAction()
    {
        return new ViewModel();
    }
}

创建视图

在 /module/UncleTooBlog/view/blog/blog/目录下创建index.phtml文件,并添加如下代码:

<div class="page-header">
    <h1><?php echo $this->translate('UncleTooBlog'); ?></h1>
</div>


扩展 ZfcUser 视图

要覆盖 ZfcUser 视图,只需要在我们创建的模块目录中创建新的ZfcUser视图文件即可。

创建如下文件:

/module/UncleTooBlog/view/zfc-user/user/register.phtml

添加如下代码:

<div class="page-header">
    <h1><?php echo $this->translate('Register'); ?></h1>
</div>
<?php
if (!$this->enableRegistration) {
    print "禁止注册";
    return;
}
$form = $this->registerForm;
$form->prepare();
$form->setAttribute('action', $this->url('zfcuser/register'));
$form->setAttribute('method', 'post');
echo $this->form()->openTag($form);
?>
<?php foreach ($form as $element) : ?>
    <div style="width: 330px;" class="form-group <?php if ($this->formElementErrors($element)) echo "has-error" ?>">
        <?php
            if ('submit' != $element->getAttribute('type')) { ?>
                <label class="control-label"><?php echo $element->getLabel() ?></label>
                <?php
                $element->setAttribute('class', 'form-control')
                        ->setAttribute('placeholder', $element->getLabel());
            } else {
                $element->setAttribute('class', 'btn btn-success');
            }
            if ($element instanceof Zend\Form\Element\Captcha) {
                echo $this->formCaptcha($element);
            } else {
                echo $this->formElement($element);
            }
            if ($this->formElementErrors($element)) : ?>
                <?php
                echo $this->formElementErrors()
                    ->setMessageOpenFormat('<p class="help-block">')
                    ->setMessageSeparatorString('</p><p class="help-block">')
                    ->setMessageCloseString('</p>')
                    ->render($element);
                ?>
            <?php endif; ?>
    </div>
<?php
endforeach;
    if ($this->redirect): ?>
        <input type="hidden" name="redirect" value="<?php echo $this->escapeHtml($this->redirect) ?>" />
    <?php endif ?>
<?php echo $this->form()->closeTag() ?>

创建如下文件:

/module/UncleTooBlog/view/zfc-user/user/login.phtml:

添加如下代码:

<div class="page-header">
    <h1><?php echo $this->translate('登录'); ?></h1>
</div>
<?php
$form = $this->loginForm;
$form->prepare();
$form->setAttribute('action', $this->url('zfcuser/login'));
$form->setAttribute('method', 'post');
?>
<?php echo $this->form()->openTag($form) ?>
<?php foreach ($form as $element) : ?>
    <div style="width: 330px;" class="form-group <?php if ($this->formElementErrors($element)) echo "has-error" ?>">
        <?php
            if ('submit' != $element->getAttribute('type')) { ?>
                <label class="control-label"><?php echo $element->getLabel() ?></label>
                <?php
                $element->setAttribute('class', 'form-control')
                        ->setAttribute('placeholder', $element->getLabel());
            } else {
                $element->setAttribute('class', 'btn btn-success');
            }
            echo $this->formElement($element);
            if ($this->formElementErrors($element)) : ?>
                <?php
                echo $this->formElementErrors()
                    ->setMessageOpenFormat('<p class="help-block">')
                    ->setMessageSeparatorString('</p><p class="help-block">')
                    ->setMessageCloseString('</p>')
                    ->render($element);
                ?>
            <?php endif; ?>
    </div>
<?php
endforeach;
echo $this->form()->closeTag() ?>
<?php if ($this->enableRegistration) : ?>
<?php echo $this->translate('Not registered?'); ?> <a href="<?php echo $this->url('zfcuser/register') . ($this->redirect ? '?redirect='.$this->redirect : '') ?>"><?php echo $this->translate('注册!'); ?></a>
<?php endif; ?>

创建如下文件:

/module/UncleTooBlog/view/zfc-user/user/index.phtml:

添加如下代码:

<div class="page-header">
    <h1><?php echo $this->translate('User details'); ?></h1>
</div>
<div class="container">
<div class="row">
<div style="float:left; padding-right:16px;"><?php echo $this->gravatar($this->zfcUserIdentity()->getEmail()) ?></div>
<h3><?php echo $this->translate('Hello'); ?>, <?php echo $this->zfcUserDisplayName() ?>!</h3>
<a href="<?php echo $this->url('zfcuser/logout') ?>">[<?php echo $this->translate('注册'); ?>]</a>
</div>
</div>

创建如下文件:

/module/UncleTooBlog/view/zfc-user/user/changepassword.phtml

添加如下代码:

<?php echo $this->form()->closeTag(); ?>
<div class="page-header">
    <h1><?php echo sprintf($this->translate('Change Password for %s'), $this->zfcUserDisplayName()); ?></h1>
</div>
<?php if ($status === true) : ?>
<div class="alert alert-success"><?php echo $this->translate('密码修改成功.');?></div>
<?php elseif ($status === false) : ?>
<div class="alert alert-danger"><?php echo $this->translate('密码修改失败,请重试.'); ?></div>
<?php endif; ?>
<?php
    $form = $this->changePasswordForm;
    $form->prepare();
    $form->setAttribute('action', $this->url('zfcuser/changepassword'));
    $form->setAttribute('method', 'post');
    $emailElement = $form->get('identity');
    $emailElement->setValue($this->zfcUserIdentity()->getEmail());
    echo $this->form()->openTag($form);
?>
<?php foreach ($form as $element) : ?>
    <div style="width: 330px;" class="form-group <?php if ($this->formElementErrors($element)) echo "has-error" ?>">
        <?php
            if ('submit' != $element->getAttribute('type')) { ?>
                <label class="control-label"><?php echo $element->getLabel() ?></label>
                <?php
                $element->setAttribute('class', 'form-control')
                        ->setAttribute('placeholder', $element->getLabel());
            } else {
                $element->setAttribute('class', 'btn btn-success');
            }
            if ($element instanceof Zend\Form\Element\Captcha) {
                echo $this->formCaptcha($element);
            } else {
                echo $this->formElement($element);
            }
            if ($this->formElementErrors($element)) : ?>
                <?php
                echo $this->formElementErrors()
                    ->setMessageOpenFormat('<p class="help-block">')
                    ->setMessageSeparatorString('</p><p class="help-block">')
                    ->setMessageCloseString('</p>')
                    ->render($element);
                ?>
            <?php endif; ?>
    </div>
<?php
endforeach;?>
<?php if ($this->redirect): ?>
        <input type="hidden" name="redirect" value="<?php echo $this->escapeHtml($this->redirect) ?>" />
    <?php endif ?>
<?php echo $this->form()->closeTag() ?>

创建如下文件:

module/Blog/view/zfc-user/user/changeemail.phtml

添加如下代码:

<div class="page-header">
    <h1><?php echo sprintf($this->translate('Change Email for %s'), $this->zfcUserDisplayName()); ?></h1>
</div>
<?php if ($status === true) : ?>
<div class="alert alert-success"><?php echo $this->translate('邮箱修改成功.'); ?></div>
<?php elseif ($status === false) : ?>
<div class="alert alert-danger"><?php echo $this->translate('邮箱修改失败,请重试.'); ?></div>
<?php endif; ?>
<?php
    $form = $this->changeEmailForm;
    $form->prepare();
    $form->setAttribute('action', $this->url('zfcuser/changeemail'));
    $form->setAttribute('method', 'post');
    echo $this->form()->openTag($form);
?>
<?php foreach ($form as $element) : ?>
    <div style="width: 330px;" class="form-group <?php if ($this->formElementErrors($element)) echo "has-error" ?>">
        <?php
            if ('submit' != $element->getAttribute('type')) { ?>
                <label class="control-label"><?php echo $element->getLabel() ?></label>
                <?php
                $element->setAttribute('class', 'form-control')
                        ->setAttribute('placeholder', $element->getLabel());
            } else {
                $element->setAttribute('class', 'btn btn-success');
            }
            if ($element instanceof Zend\Form\Element\Captcha) {
                echo $this->formCaptcha($element);
            } else {
                echo $this->formElement($element);
            }
            if ($this->formElementErrors($element)) : ?>
                <?php
                echo $this->formElementErrors()
                    ->setMessageOpenFormat('<p class="help-block">')
                    ->setMessageSeparatorString('</p><p class="help-block">')
                    ->setMessageCloseString('</p>')
                    ->render($element);
                ?>
            <?php endif; ?>
    </div>
<?php
endforeach;?>
<input class="btn btn-success" type="submit" value="Submit" />
<?php if ($this->redirect): ?>
        <input type="hidden" name="redirect" value="<?php echo $this->escapeHtml($this->redirect) ?>" />
    <?php endif ?>
<?php echo $this->form()->closeTag() ?>

代码里,我们使用Bootstrap美化ZfcUser表单,点击这里可以了解更多关于Bootstrap



除非特别声明,本站所有PHP教程及其他教程/文章均为原创、翻译或网友投稿,版权均归UncleToo中文网所有, 转载请注明作者及出处。
原文网址:http://www.uncletoo.com/html/zend/962.html
读完这篇文章后,你是否有所收获? 分享是一种生活的信念!
  • 0
  • 0
我来说两句
更多>>网友评论
UncleToo,一档有态度的PHP中文网

图片教程