Return JSON 管理 Magento 中的数据
Return JSON data in admin Magento
下午好,
我正在尝试在 Magento 管理员中创建 AJAX 登录。但我不知道 Magento 登录功能和登录操作是怎么回事。我对 Magento 很陌生,对它感到很困惑...
我已经知道有两个地方可以处理登录。
/core/Mage/Admin/Model/Session.php
(登录函数)
和
/core/Mage/Adminhtml/controllers/IndexController.php
(登录操作)
我必须对 return JSON 数据做什么?而Url(index.php/admin/login
?)来处理呢?
为了清楚,有两个问题我需要自己回答。
- 什么是 login() 函数?它是如何工作的?
- 什么是 loginAction() 函数?它是如何工作的?
如有任何帮助,我们将不胜感激。谢谢大家!
Magento Admin 表单的默认流程是:
1) 当验证成功并且用户输入凭据时,magento 将其发送到 Admin/Model/Session.php 登录函数。
2)请求有2个参数:
- username
- password
- key(we will get into it later below, lets say key is 123456)
- form_key posted with form by default
3) 在登录函数内部,magento 检查是否有任何用户具有这些凭据。如果是,则将当前管理员用户设置为此用户表单,在表单中输入凭据。
4) 在此之后,magento 将控件重定向到 adminhtml/index/index/key/123456
在这里你可以看到我们在表单中得到的密钥是用来保持真实性的。
现在,如果你想做到 ajax,
创建自定义控制器让我们说 AjaxloginAction。您可以在其中编写以下函数:
public function AjaxloginAction(){
$params = $this->getRequest()->getParams();
$username = $params['username'];
$password = $params['password'];
//call the model function here
$flag = Mage::getModel('admin/session')->login($username, $password);
if($flag == true){
//user if logged in, do further redirection or processing
}
else{
// invalid credentals, return warning on frontend
}
}
为确保 session.php 模型默认不重定向它,覆盖它并将其更改为以下内容:
public function login($username, $password, $request = null)
{
if (empty($username) || empty($password)) {
return;
}
try {
/** @var $user Mage_Admin_Model_User */
$user = Mage::getModel('admin/user');
$user->login($username, $password);
if ($user->getId()) {
$this->renewSession();
if (Mage::getSingleton('adminhtml/url')->useSecretKey()) {
Mage::getSingleton('adminhtml/url')->renewSecretUrls();
}
$this->setIsFirstPageAfterLogin(true);
$this->setUser($user);
$this->setAcl(Mage::getResourceModel('admin/acl')->loadAcl());
$requestUri = $this->_getRequestUri($request);
if ($requestUri) {
Mage::dispatchEvent('admin_session_user_login_success', array('user' => $user));
//header('Location: ' . $requestUri);
//exit;
return true;
}
} else {
return 'Invalid User Name or Password.';
}
} catch (Mage_Core_Exception $e) {
Mage::dispatchEvent('admin_session_user_login_failed',
array('user_name' => $username, 'exception' => $e));
if ($request && !$request->getParam('messageSent')) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
$request->setParam('messageSent', true);
}
}
return $user;
}
Hope this helps. :)
1) 覆盖 IndexController.php 后,您可以在其中创建 AjaxLoginAction。
2) 在您的 $.ajax, url 中,您将指定为:
url : Mage::helper("adminhtml")->getUrl("mage_adminhtml/index/AjaxLogin");
//check once if the above url returns your action urls or not
3) 在您的控制器中,您可以使用以下命令将输出发送回 json 中的前端:
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($reply));
return;
//where reply can be a text or can be multidimentional arrray`enter code here`
下午好,
我正在尝试在 Magento 管理员中创建 AJAX 登录。但我不知道 Magento 登录功能和登录操作是怎么回事。我对 Magento 很陌生,对它感到很困惑...
我已经知道有两个地方可以处理登录。
/core/Mage/Admin/Model/Session.php
(登录函数)
和
/core/Mage/Adminhtml/controllers/IndexController.php
(登录操作)
我必须对 return JSON 数据做什么?而Url(index.php/admin/login
?)来处理呢?
为了清楚,有两个问题我需要自己回答。
- 什么是 login() 函数?它是如何工作的?
- 什么是 loginAction() 函数?它是如何工作的?
如有任何帮助,我们将不胜感激。谢谢大家!
Magento Admin 表单的默认流程是:
1) 当验证成功并且用户输入凭据时,magento 将其发送到 Admin/Model/Session.php 登录函数。 2)请求有2个参数:
- username
- password
- key(we will get into it later below, lets say key is 123456)
- form_key posted with form by default
3) 在登录函数内部,magento 检查是否有任何用户具有这些凭据。如果是,则将当前管理员用户设置为此用户表单,在表单中输入凭据。 4) 在此之后,magento 将控件重定向到 adminhtml/index/index/key/123456 在这里你可以看到我们在表单中得到的密钥是用来保持真实性的。
现在,如果你想做到 ajax,
创建自定义控制器让我们说 AjaxloginAction。您可以在其中编写以下函数:
public function AjaxloginAction(){
$params = $this->getRequest()->getParams();
$username = $params['username'];
$password = $params['password'];
//call the model function here
$flag = Mage::getModel('admin/session')->login($username, $password);
if($flag == true){
//user if logged in, do further redirection or processing
}
else{
// invalid credentals, return warning on frontend
}
}
为确保 session.php 模型默认不重定向它,覆盖它并将其更改为以下内容:
public function login($username, $password, $request = null)
{
if (empty($username) || empty($password)) {
return;
}
try {
/** @var $user Mage_Admin_Model_User */
$user = Mage::getModel('admin/user');
$user->login($username, $password);
if ($user->getId()) {
$this->renewSession();
if (Mage::getSingleton('adminhtml/url')->useSecretKey()) {
Mage::getSingleton('adminhtml/url')->renewSecretUrls();
}
$this->setIsFirstPageAfterLogin(true);
$this->setUser($user);
$this->setAcl(Mage::getResourceModel('admin/acl')->loadAcl());
$requestUri = $this->_getRequestUri($request);
if ($requestUri) {
Mage::dispatchEvent('admin_session_user_login_success', array('user' => $user));
//header('Location: ' . $requestUri);
//exit;
return true;
}
} else {
return 'Invalid User Name or Password.';
}
} catch (Mage_Core_Exception $e) {
Mage::dispatchEvent('admin_session_user_login_failed',
array('user_name' => $username, 'exception' => $e));
if ($request && !$request->getParam('messageSent')) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
$request->setParam('messageSent', true);
}
}
return $user;
}
Hope this helps. :)
1) 覆盖 IndexController.php 后,您可以在其中创建 AjaxLoginAction。 2) 在您的 $.ajax, url 中,您将指定为:
url : Mage::helper("adminhtml")->getUrl("mage_adminhtml/index/AjaxLogin");
//check once if the above url returns your action urls or not
3) 在您的控制器中,您可以使用以下命令将输出发送回 json 中的前端:
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($reply));
return;
//where reply can be a text or can be multidimentional arrray`enter code here`