1
私はZf2を初めて使用しています。私はユーザーと管理モジュールを持っています。ユーザーがログインしているとき、私は自分のプロフィールとパスワードを変更する管理者のプロフィールを作成しました。 変更パスワードで作業していますが、パスワードを変更する方法を理解することはできません。私のコードは以下の通りです:ログインしたユーザーのパスワードをzf2で変更してください
マイProfileControllerです:
<?php
namespace Admin\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Admin\Model\Profile;
use Admin\Form\ProfileForm;
class ProfileController extends AbstractActionController
{
protected $profileTable;
public function getAuthService()
{
$this->authservice = $this->getServiceLocator()->get('AuthService');
return $this->authservice;
}
public function indexAction()
{
$this->layout("layout/layout_admin");
/* this is used to show user email of logined user. */
$user_email = $this->getAuthService()->getStorage()->read();
// below condition is new and for back browser validation
if (!$this->getServiceLocator()
->get('AuthService')->hasIdentity()) {
return $this->plugin(redirect)->toRoute('login', array('action' => 'index'));
}
return new ViewModel(array(
'admin_profile' => $this->getProfileTable()->fetchAll($user_email),
));
}
public function editAction()
{
$this->layout("layout/layout_admin");
$id = (int) $this->params()->fromRoute('id', 0);
if (!$id) {
return $this->redirect()->toRoute('login', array(
'action' => 'index'
));
}
// Get the Profile with the specified id. An exception is thrown
// if it cannot be found, in which case go to the index page.
try {
$profile = $this->getProfileTable()->getProfile($id);
}
catch (\Exception $ex) {
return $this->redirect()->toRoute('profile', array(
'action' => 'index'
));
}
$form = new ProfileForm();
$form->bind($profile);
$form->get('submit')->setAttribute('value', 'Edit');
$request = $this->getRequest();
if ($request->isPost()) {
$form->setInputFilter($profile->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$this->getProfileTable()->saveProfile($profile);
// Redirect to profile
return $this->redirect()->toRoute('profile');
}
}
return array(
'id' => $id,
'form' => $form,
);
}
public function changepasswordAction()
{
$this->layout("layout/layout_admin");
$id = (int) $this->params()->fromRoute('id', 0);
if (!$id) {
return $this->redirect()->toRoute('login', array(
'action' => 'index'
));
}
// Get the Profile with the specified id. An exception is thrown
// if it cannot be found, in which case go to the index page.
try {
$profile = $this->getProfileTable()->getProfile($id);
}
catch (\Exception $ex) {
return $this->redirect()->toRoute('profile', array(
'action' => 'index'
));
}
$form = new ProfileForm();
$form->bind($profile);
$form->get('submit')->setAttribute('value', 'Change');
$request = $this->getRequest();
if ($request->isPost()) {
$form->setInputFilter($profile->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
echo "form is valid now save the data";
}else echo "form is invalid, don't save data";
}
return array(
'id' => $id,
'form' => $form,
);
}
public function getProfileTable()
{
if (!$this->profileTable) {
$sm = $this->getServiceLocator();
$this->profileTable = $sm->get('Admin\Model\ProfileTable');
}
return $this->profileTable;
}
}
ProfileForm.php:
<?php
namespace Admin\Form;
use Zend\Form\Form;
class ProfileForm extends Form
{
public function __construct($name = null)
{
parent::__construct('profile');
$this->add(array(
'name' => 'id',
'type' => 'Hidden',
));
$this->add(array(
'name' => 'name',
'attributes' => array(
'type' => 'text',
'required' => 'required',
),
'options' => array(
'label' => 'Name',
),
));
$this->add(array(
'name' => 'email',
'attributes' => array(
'type' => 'text',
'required' => 'required',
),
'options' => array(
'label' => 'Email',
),
));
$this->add(array(
'name' => 'old_password',
'attributes' => array(
'type' => 'password',
'required' => 'required',
),
'options' => array(
'label' => 'Old Password ',
),
));
$this->add(array(
'name' => 'new_password',
'attributes' => array(
'type' => 'password',
'required' => 'required',
),
'options' => array(
'label' => 'New Password ',
),
));
$this->add(array(
'name' => 'confirm_password',
'attributes' => array(
'type' => 'password',
'required' => 'required',
),
'options' => array(
'label' => 'Confirm Password ',
),
));
$this->add(array(
'name' => 'submit',
'type' => 'Submit',
'attributes' => array(
'value' => 'Go',
'id' => 'submitbutton',
),
));
}
}
モデル/ Profile.php:
<?php
namespace Admin\Model;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
class Profile implements InputFilterAwareInterface
{
public $id;
public $name;
public $email;
public $password;
protected $inputFilter;
public function exchangeArray($data)
{
$this->id = (!empty($data['id'])) ? $data['id'] : null;
$this->name = (!empty($data['name'])) ? $data['name'] : null;
$this->email = (!empty($data['email'])) ? $data['email'] : null;
$this->password = (!empty($data['password'])) ? $data['password'] : null;
}
public function __set($name, $value) {
$method = 'set' . $name;
if (!method_exists($this, $method)) {
throw new Exception('Invalid Method');
}
$this->$method($value);
}
public function __get($name) {
$method = 'get' . $name;
if (!method_exists($this, $method)) {
throw new Exception('Invalid Method');
}
return $this->$method();
}
public function getArrayCopy()
{
return get_object_vars($this);
}
public function setInputFilter(InputFilterInterface $inputFilter)
{
throw new \Exception("Not used");
}
public function getInputFilter()
{
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$inputFilter->add(array(
'name' => 'id',
'required' => true,
'filters' => array(
array('name' => 'Int'),
),
));
$inputFilter->add(array(
'name' => 'name',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
));
$inputFilter->add(array(
'name' => 'email',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'EmailAddress',
'options' => array(
'domain' => true,
),
),
),
));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
}
モデル/ ProfileTable.php :
<?php
namespace Admin\Model;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Adapter\Adapter;
class ProfileTable
{
protected $tableGateway;
public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function fetchAll($user_email)
{
$resultSet = $this->tableGateway->select(array('email' =>$user_email));
return $resultSet;
}
public function getProfile($id)
{
$id = (int) $id;
$rowset = $this->tableGateway->select(array('id' => $id));
$row = $rowset->current('id');
if (!$row) {
throw new \Exception("Could not find row $id");
}
return $row;
}
public function saveProfile(Profile $profile)
{
$data = array(
'name' => $profile->name,
'email' => $profile->email,
);
$id = (int) $profile->id;
if ($id == 0) {
$this->tableGateway->insert($data);
} else {
if ($this->getProfile($id)) {
$this->tableGateway->update($data, array('id' => $id));
} else {
throw new \Exception('Profile id does not exist');
}
}
}
}
ビュー/ changepassword.phtml:
<div class="jumbotron">
<?php
$title = 'Change your password Here:';
$this->headTitle($title);
?>
<h2 align="center"><?php echo $this->escapeHtml($title); ?></h2>
<div align="right">
<a href="<?php echo $this->url('profile');?>">View Profile</a>
</div>
<br/>
<?php
$form = $this->form;
$form->setAttribute('action', $this->url('profile',
array(
'action' => 'changepassword',
'id' => $this->id,
)
));
$form->prepare();
?>
<div align="center">
<?php
echo $this->form()->openTag($form);
echo $this->formHidden($form->get('id'));
echo $this->formRow($form->get('old_password'));
echo "<br/>";
echo $this->formRow($form->get('new_password'));
echo "<br/>";
echo $this->formRow($form->get('confirm_password'));
echo "<br/>";
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();
?>
<a href="<?php echo $this->url('profile');?>">Cancel</a>
</div>
</div>