Skip to content

Instantly share code, notes, and snippets.

@zhouyl
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save zhouyl/f0472eab79207b5e3e1a to your computer and use it in GitHub Desktop.

Select an option

Save zhouyl/f0472eab79207b5e3e1a to your computer and use it in GitHub Desktop.
phalcon view helper
<?php
namespace Base\View\Helper;
/**
* 视图助手 - 输出响应信息
*
* 示例:
*
* $helper = new \Base\View\Helper\Message;
* $helper->success('操作执行成功!')->addLink(link_to('/', '首页'));
* $helper->error('操作失败!');
*/
class Message extends \Phalcon\DI\Injectable
{
/**
* 消息类型 (message|success|error|warning)
*
* @var string
*/
public $type = 'message';
/**
* 消息标题
*
* @var string
*/
public $title = 'Message !';
/**
* 消息内容
*
* @var string
*/
public $message = null;
/**
* 跳转链接
*
* @var array
*/
public $links = array();
/**
* 输出普通消息
*
* @param string $message
* @param mixed $links
*/
public function message($message, $links = array())
{
return $this->setType('message')
->setMessage($message, 'Message !')
->setLinks($links)
->forward();
}
/**
* 输出操作成功消息
*
* @param string $message
* @param mixed $links
*/
public function success($message, $links = array())
{
return $this->setType('success')
->setMessage($message, 'Success !')
->setLinks($links)
->forward();
}
/**
* 输出操作失败消息
*
* @param string $message
* @param mixed $links
*/
public function error($message, $links = array())
{
return $this->setType('error')
->setMessage($message, 'Error !')
->setLinks($links)
->forward();
}
/**
* 输出警告消息
*
* @param string $message
* @param mixed $links
*/
public function warning($message, $links = array())
{
return $this->setType('warning')
->setMessage($message, 'Warning !')
->setLinks($links)
->forward();
}
/**
* 设置消息类型
*
* @param string $type
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* 设置消息标题
*
* @param string $title
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* 设置消息内容
*
* @param string $message
* @param string $title
*/
public function setMessage($message, $title = null)
{
$this->message = $message;
if ($title !== null) {
$this->title = $title;
}
return $this;
}
/**
* 增加跳转链接
*
* @param string $link
*/
public function addLink($link)
{
$this->links[] = $link;
return $this;
}
/**
* 设置跳转链接列表
*
* @param string|array $links
*/
public function setLinks($links)
{
$this->links = is_array($links) ? $links : array($links);
return $this;
}
/**
* forward 并输出信息
*
* @param array $forward
* @param array $params
*/
public function forward(array $forward = null, array $params = null)
{
$this->dispatcher->setParams(array(
'type' => $this->type,
'title' => __($this->title),
'message' => __($this->message),
'links' => $this->links,
));
if (! empty($params)) {
$this->dispatcher->setParams($params);
}
if ($forward === null) {
$forward = array(
'controller' => 'helper',
'action' => 'message',
);
}
return $this->dispatcher->forward($forward);
}
}
bootstrap:
<?php
$di = new Phalcon\DI\FactoryDefault();
$di['helper'] = function () {
return new \Base\View\Helper\Message;
};
?>
controller:
<?php
$this->helper->success('操作执行成功!')->addLink(link_to('/', '首页'));
$this->helper->error('操作失败!');
?>
<?
$arr = array(
'message' => 'blue',
'success' => 'green',
'error' => 'red',
'warning' => 'yellow',
);
$color = isset($arr[$type]) ? $arr[$type] : 'default';
array_push($links, $this->tag->linkTo(array('javascript:history.back();', __('Back<h/>'))));
?>
<div class="row message message-<?=$type?>">
<div class="portlet <?=$color?> box">
<div class="portlet-title">
<div class="caption">
<?=$title?>
</div>
</div>
<div class="portlet-body">
<p class="margin-bottom-15">
<?=$message?>
</p>
<p>
<? foreach ($links as $link) { ?>
[ <?=$link?> ] &nbsp;
<? } ?>
</p>
</div>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment