本篇magento开发学习文章更新:2019年03月23日
学习magento开发的学者都会碰到重写核心的代码的时候,这个时候我们最好不要直接更改核心的代码,这样的为以后的更新不利。 最好的办法重写以前的方法,之前我已经讲过了关于重写后台的controller的方法,
现在我将为大家讲述下重写前端的controller的方法的应用。
第一步:我们创建好自己的自己模块
我们在app/code/local/目录下创建的 自己自定义的模块Sky8g/Customer/两个主次目录。在这个Customer目录下面分别创建一下的目录和文件。controllers目录和etc两个目录
(注意):Sky8g/Customer 首字母都是大写。
第二步:创建magento需要重写的controller文件
a . 在etc的目录下创建 config.xml 文件,里面的代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?xml version="1.0" encoding="UTF-8"?> <config> <modules> <Sky8g_Customer> <version>0.0.1</version> </Sky8g_Customer> </modules> <frontend> <routers> <customer> <args> <modules> <Sky8g_Customer before="Mage_Customer">Sky8g_Customer</Sky8g_Customer> </modules> </args> </customer> </routers> </frontend> </config> |
b. 在controllers的目录下创建AccountController.php 文件,里面的代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | <?php require_once 'Mage/Customer/controllers/AccountController.php'; class Sky8g_Customer_AccountController extends Mage_Customer_AccountController { public function loginPostAction() { print_r('执行的这里的文件'); exit; if (!$this->_validateFormKey()) { $this->_redirect('*/*/'); return; } if ($this->_getSession()->isLoggedIn()) { $this->_redirect('*/*/'); return; } $session = $this->_getSession(); if ($this->getRequest()->isPost()) { $login = $this->getRequest()->getPost('login'); if (!empty($login['username']) && !empty($login['password'])) { try { $session->login($login['username'], $login['password']); if ($session->getCustomer()->getIsJustConfirmed()) { $this->_welcomeCustomer($session->getCustomer(), true); } } catch (Mage_Core_Exception $e) { switch ($e->getCode()) { case Mage_Customer_Model_Customer::EXCEPTION_EMAIL_NOT_CONFIRMED: $value = $this->_getHelper('customer')->getEmailConfirmationUrl($login['username']); $message = $this->_getHelper('customer')->__('This account is not confirmed. <a href="%s" style="color:#ff0000;">Click here</a> to resend confirmation email.', $value); break; case Mage_Customer_Model_Customer::EXCEPTION_INVALID_EMAIL_OR_PASSWORD: $message = $e->getMessage(); break; default: $message = $e->getMessage(); } $session->addError($message); $session->setUsername($login['username']); } catch (Exception $e) { // Mage::logException($e); // PA DSS violation: this exception log can disclose customer password } } else { $session->addError($this->__('Login and password are required.')); } } $this->_loginPostRedirect(); } } |
第三步:访问你的url路由
以上的代码保存上传到你服务器上,然后访问https://www.sky8g.com/customer/account/loginPost/将会看到页面出现的下面的结果:
1 | 执行这里的文件 |
希望这篇文章能够帮助你,我们会不懈的努力为学习magento的开发者更新更高质量的文章,欢迎你下次再来,如果有什么不明白的地方请留言,我们会第一时间回复,谢谢你的观赏。
文章不错支持一下吧