Php 使用zend模型和控制器实现登录身份验证
我正在使用Zend 2.0,我从未将EXTJS与Zend结合使用 以下是我在view/login/index.phtml中的extjs代码:Php 使用zend模型和控制器实现登录身份验证,php,extjs,zend-db,zend-framework2,Php,Extjs,Zend Db,Zend Framework2,我正在使用Zend 2.0,我从未将EXTJS与Zend结合使用 以下是我在view/login/index.phtml中的extjs代码: <?php $this->inlineScript()->captureStart() ?> var LoginWindow Ext.onReady(function() { LoginWindow = new Ext.create('Ext.window.Window',{ title: 'Login',
<?php $this->inlineScript()->captureStart() ?>
var LoginWindow
Ext.onReady(function() {
LoginWindow = new Ext.create('Ext.window.Window',{
title: 'Login',
closable: false,
draggable: false,
resizable: false,
width: 370,
items: [
Ext.create('Ext.form.Panel', {
id: 'LoginForm',
bodyPadding: 5,
width: 350,
url: '/',
layout: 'anchor',
defaults: {
anchor: '100%'
},
// The fields
defaultType: 'textfield',
items: [{
fieldLabel: 'Username',
name: 'user',
allowBlank: false
},{
fieldLabel: 'Password',
inputType: 'password',
name: 'pw',
allowBlank: false
}],
// Reset and Submit buttons
buttons: [{
text: 'Reset',
handler: function() {
this.up('form').getForm().reset();
}
},
{
text: 'Submit',
formBind: true,
disabled: true,
handler: function() {
var form = this.up('form').getForm();
}
}]
})
]
});
Ext.getBody().mask()
LoginWindow.show()
});
<?php $this->inlineScript()->captureEnd() ?>
var LoginWindow
Ext.onReady(函数(){
LoginWindow=new Ext.create('Ext.window.window'{
标题:“登录”,
可关闭:错误,
可拖动:错误,
可调整大小:false,
宽度:370,
项目:[
Ext.create('Ext.form.Panel'{
id:'登录信息',
车身衬垫:5,
宽度:350,
url:“/”,
布局:“锚定”,
默认值:{
主持人:“100%”
},
//田野
defaultType:'textfield',
项目:[{
fieldLabel:'用户名',
名称:“用户”,
allowBlank:false
},{
fieldLabel:“密码”,
输入类型:“密码”,
名称:‘pw’,
allowBlank:false
}],
//重置和提交按钮
按钮:[{
文本:“重置”,
处理程序:函数(){
this.up('form').getForm().reset();
}
},
{
文本:“提交”,
是的,
残疾人:对,,
处理程序:函数(){
var form=this.up('form').getForm();
}
}]
})
]
});
Ext.getBody().mask()
LoginWindow.show()
});
现在我不知道如何将用户名/密码发送到LoginController.php,并使用该模型从数据库表中验证用户名/密码
任何例子或可能的解决方案都会很有帮助。一个问题中有几个问题。首先学习Zend_Auth如何验证密码,Zend_Controller如何从请求中检索参数,Zend_表单如何发送这些参数,Zend_视图如何将一些js与应用程序的其余部分关联 简而言之,密码和用户名通过表单发送,应用程序通过请求对象的getParams方法获取它们
我没有太多的链接要发送给你,目前ZF2的文档有很多。你问的问题不太清楚,所以我要写一个一般性的答案,它不会特定于zend framework,因为我不知道zend framework。但以下几点适用于用于身份验证的任何技术
form.submit(options)
或者通过设置standardSubmit:true
on来执行普通HTML提交
表格。两者的文档都是&。作为
在Ext.form.Panel
虽然未列为FormPanel的配置选项,但
FormPanel类接受
Ext.form.Basic类,并将它们传递给内部
创建时的基本表单
select * from users where username='username' and password='password'
传递给上述查询的密码也应加密。这个
上述查询的结果应确定用户是否
已成功通过身份验证。如果查询返回0个结果,
那么身份验证失败了。此外,使用
上面的样式是您有一个用户对象,您可以
无需进行额外查询即可与会话关联我希望这能回答你的问题。 < P>请不要认为以下是一个现成的解决方案。我还添加了一些在开始管理登录时会很有用的部分。您的主要部分应该是控制器的路由。我还保留了视图内容,除了其中的ExtJS部分。不管怎样,我希望这对你有帮助 首先是表单中的一些问题。试试下面的一个
var loginWindow;
Ext.onReady(function() {
loginWindow = new Ext.create('Ext.window.Window',{
title: 'Login',
closable: false,
draggable: false,
resizable: false,
width: 370,
modal: true,
items: [
Ext.create('Ext.form.Panel', {
id: 'LoginForm',
bodyPadding: 5,
width: 350,
layout: 'anchor',
defaults: {
anchor: '100%'
},
defaultType: 'textfield',
items: [{
fieldLabel: 'Username',
name: 'user',
allowBlank: false
},{
fieldLabel: 'Password',
inputType: 'password',
name: 'pw',
allowBlank: false
}],
url: 'Login/Auth', // first one should be your controller, second one the controller action (this one need to accept post)
buttons: [
{
text: 'Reset',
handler: function() {
this.up('form').getForm().reset();
}
},
{
text: 'Submit',
formBind: true,
disabled: true,
handler: function() {
var form = this.up('form').getForm();
if (form.isValid()) {
form.submit({
success: function(form, action) {
Ext.Msg.alert('Success', 'Authenticated!');
},
failure: function(form, action) {
Ext.Msg.alert('Failed', 'Authentication Failed');
}
});
}
}
}
]
})
]
}).show();
// Ext.getBody().mask(); <- modal property does the same
});
var loginWindow;
Ext.onReady(函数(){
loginWindow=new Ext.create('Ext.window.window'{
标题:“登录”,
可关闭:错误,
可拖动:错误,
可调整大小:false,
宽度:370,
莫代尔:是的,
项目:[
Ext.create('Ext.form.Panel'{
id:'登录信息',
车身衬垫:5,
宽度:350,
布局:“锚定”,
默认值:{
主持人:“100%”
},
defaultType:'textfield',
项目:[{
fieldLabel:'用户名',
名称:“用户”,
allowBlank:false
},{
fieldLabel:“密码”,
输入类型:“密码”,
名称:‘pw’,
allowBlank:false
}],
url:'Login/Auth',//第一个应该是您的控制器,第二个应该是控制器操作(这个需要接受post)
按钮:[
{
<?php
return array(
'controllers' => array(
'invokables' => array(
'Login\Controller\Login' => 'Login\Controller\LoginController',
),
),
// The following section is new and should be added to your file
'router' => array(
'routes' => array(
'login' => array(
'type' => 'segment',
'options' => array(
'route' => '/login[/:action][/:username][/:password]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'username' => '[a-zA-Z][a-zA-Z0-9_-]*',
'password' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Login\Controller\Login',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'login' => __DIR__ . '/../view',
),
),
);
<?php
namespace Login\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class LoginController extends AbstractActionController
{
public function authenticateAction($username, $password)
{
$params = array('driver' => 'driver',
'dbname' => 'name');
$db = new DbAdapter($params);
use Zend\Authentication\Adapter\DbTable as AuthAdapter;
// Oversimplified example without even hashing the password
$adapter = new AuthAdapter($db,
'Logins',
'username',
'password'
);
// get select object (by reference)
$this->_adapter->setIdentity($username);
$this->_adapter->setCredential($password);
$result = $adapter->authenticate();
if($result->isValid()) {
// authenticated
}
}
protected $loginTable;
public function getLoginTable()
{
if (!$this->loginTable) {
$sm = $this->getServiceLocator();
$this->loginTable = $sm->get('Login\Model\LoginTable');
}
return $this->loginTable;
}
}
<?php
namespace Login\Model;
class Login
{
public $id;
public $username;
public $password;
public function exchangeArray($data)
{
$this->id = (isset($data['id'])) ? $data['id'] : null;
$this->username = (isset($data['username'])) ? $data['username'] : null;
$this->password = (isset($data['password'])) ? $data['password'] : null;
}
}
<?php
namespace Login\Model;
use Zend\Db\TableGateway\TableGateway;
class LoginTable
{
protected $tableGateway;
public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function fetchAll()
{
$resultSet = $this->tableGateway->select();
return $resultSet;
}
public function getLogin($id)
{
$id = (int) $id;
$rowset = $this->tableGateway->select(array('id' => $id));
$row = $rowset->current();
if (!$row) {
throw new \Exception("Could not find row $id");
}
return $row;
}
public function saveLogin(Login $login)
{
$data = array(
'username' => $login->password,
'password' => $login->username,
);
$id = (int)$login->id;
if ($id == 0) {
$this->tableGateway->insert($data);
} else {
if ($this->getLogin($id)) {
$this->tableGateway->update($data, array('id' => $id));
} else {
throw new \Exception('Form id does not exist');
}
}
}
public function deleteLogin($id)
{
$this->tableGateway->delete(array('id' => $id));
}
}
<?php
namespace Login;
// Add these import statements:
use Login\Model\Login;
use Login\Model\LoginTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
class Module
{
// getAutoloaderConfig() and getConfig() methods here
// Add this method:
public function getServiceConfig()
{
return array(
'factories' => array(
'Login\Model\LoginTable' => function($sm) {
$tableGateway = $sm->get('LoginTableGateway');
$table = new LoginTable($tableGateway);
return $table;
},
'LoginTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Login());
return new TableGateway('login', $dbAdapter, null, $resultSetPrototype);
},
),
);
}
}
<?php
return array(
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=zf2tutorial;host=localhost',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
),
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter'
=> 'Zend\Db\Adapter\AdapterServiceFactory',
),
),
);
<?php
return array(
'db' => array(
'username' => 'YOUR USERNAME HERE',
'password' => 'YOUR PASSWORD HERE',
),
);