ฉันกำลังพัฒนาโมดูลที่กำหนดเองซึ่งมีการส่งแบบฟอร์ม ฉันต้องการเพิ่ม captcha ลงไป และเราต้องการใช้ไลบรารี captcha ที่เป็นค่าเริ่มต้นของ Magento เพื่อให้ captcha สอดคล้องกับไลบรารีในรูปแบบการลงทะเบียน
ฉันกำลังพัฒนาโมดูลที่กำหนดเองซึ่งมีการส่งแบบฟอร์ม ฉันต้องการเพิ่ม captcha ลงไป และเราต้องการใช้ไลบรารี captcha ที่เป็นค่าเริ่มต้นของ Magento เพื่อให้ captcha สอดคล้องกับไลบรารีในรูปแบบการลงทะเบียน
คำตอบ:
คุณต้องทำตามขั้นตอนในการใช้ magento captcha ในโมดูลที่กำหนดเอง
ขั้นตอนที่ 1 : Vendor/Module/etc/config.xml
<? xml version = "1.0"?>
<config xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: noNamespaceSchemaLocation = "โกศ: magento: โมดูล: Magento_Store: ฯลฯ / config.xsd">
<เริ่มต้น>
<ลูกค้า>
<CAPTCHA>
<shown_to_logged_in_user>
<custom_form> 1 </ custom_form>
</ shown_to_logged_in_user>
<always_for>
<custom_form> 1 </ custom_form>
</ always_for>
</ แจ้งลบความคิดเห็น>
</ ลูกค้า>
<captcha translate = "label">
<หน้า>
<พื้นที่>
<custom_form>
<label> ฟอร์มที่กำหนดเอง </label>
</ custom_form>
</ พื้นที่>
</ หน้า>
</ แจ้งลบความคิดเห็น>
</ เริ่มต้น>
</ config>
ขั้นตอนที่ 2: ไปที่ 'ผู้ดูแลระบบ -> ร้านค้า -> การกำหนดค่า -> ลูกค้า -> การกำหนดค่าลูกค้า -> แคปต์ชา ' และกำหนดค่า คุณสามารถเห็นค่าฟอร์มใหม่ 'ฟอร์มที่กำหนดเอง'
ขั้นตอนที่ 3: สร้าง Vendor/Module/view/frontend/layout/yourroutid_index_index.xml
<? xml version = "1.0"?>
<page xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" layout = "1column" xsi: noNamespaceSchemaLocation = "urn: magento: กรอบงาน: ดู / เลย์เอาต์ / etc / page_configuration.xsd">
<head>
<title> แบบฟอร์มที่กำหนดเอง </title>
</ head>
<body>
<referenceContainer name = "content">
<block class = "Vendor \ Module \ Block \ CaptchaForm" name = "contactForm" template = "Vendor_Module :: captchaform.phtml">
<container name = "form.additional.info" label = "แบบฟอร์มข้อมูลเพิ่มเติม">
<block class = "Magento \ Captcha \ Block \ Captcha" name = "captcha" after = "-" cacheable = "false">
<action method = "setFormId">
<argument name = "formId" xsi: type = "string"> custom_form </argument>
</ ดำเนินการ>
<action method = "setImgWidth">
<argument name = "width" xsi: type = "string"> 230 </argument>
</ ดำเนินการ>
<action method = "setImgHeight">
<argument name = "width" xsi: type = "string"> 50 </argument>
</ ดำเนินการ>
</ บล็อก>
</ ภาชนะ>
</ บล็อก>
</ referenceContainer>
<referenceBlock name = "head.components">
<block class = "Magento \ Framework \ View \ Element \ Js \ Components" name = "captcha_page_head_components" template = "Magento_Captcha :: js / components.phtml" />
</ referenceBlock>
</ body>
</ หน้า>
ขั้นตอนที่ 4: Vendor/Module/Block/CaptchaForm.php
เนมสเปซ Vendor \ Module \ Block;
คลาส CaptchaForm ขยาย \ Magento \ Framework \ View \ Element \ Template
{
ฟังก์ชั่นสาธารณะ getFormAction ()
{
ส่งคืน $ this-> getUrl ('yourroute / index / post', ['_secure' => true]);
}
}
ขั้นตอนที่ 5: Vendor/Moduel/view/frontend/templates/captchaform.phtml
<form class = "แบบฟอร์มติดต่อ"
การกระทำ = "<? php / * @escapeNotVerified * / echo $ block-> getFormAction ();?>"
id = "ติดต่อรูปแบบ"
วิธี = "โพสต์"
data-hasrequired = "<? php / * @escapeNotVerified * / echo __ ('* ฟิลด์ที่จำเป็น')?>"
ข้อมูลผู้วิเศษ-init = '{ "การตรวจสอบ": {}}'>
<fieldset class = "fieldset">
<legend class = "legend"> <span> <? php / * @escapeNotVerified * / echo __ ('เขียนถึงเรา')?> </span> </legend> <br />
<div class = "ต้องระบุชื่อฟิลด์">
<label class = "label" สำหรับ = "name"> <span> <? php / * @escapeNotVerified * / echo __ ('ชื่อ')?> </span> </label>
<div class = "control">
<input name = "name" id = "name" title = "<? php / * @escapeNotVerified * / echo __ ('ชื่อ')?>" value = "" class = "อินพุตข้อความ" type = "text" ข้อมูลการตรวจสอบ = "{จำเป็น: จริง}" />
</ div>
</ div>
<div class = "ต้องการอีเมลฟิลด์">
<label class = "label" สำหรับ = "อีเมล"> <span> <? php / * @escapeNotVerified * / echo __ ('อีเมล')?> </span> </label>
<div class = "control">
<input name = "email" id = "email" title = "<? php / * @escapeNotVerified * / echo __ ('อีเมล')?>" value = "" class = "อินพุตข้อความ" type = "อีเมล" data-validate = "{ต้องการ: true, 'validate-email': true}" />
</ div>
</ div>
<? php echo $ block-> getChildHtml ('form.additional.info'); ?>
</ fieldset>
<div class = "actions-toolbar">
<div class = "primary">
<input type = "hidden" name = "hideit" id = "hideit" value = "" />
<button type = "submit" title = "<? php / * @escapeNotVerified * / echo __ ('ส่ง')?>" class = "การกระทำส่งหลัก">
<span> <? php / * @escapeNotVerified * / echo __ ('ส่ง')?> </span>
</ ปุ่ม>
</ div>
</ div>
</ form>
ตอนนี้คุณสามารถเห็นแคปต์ชาในแบบฟอร์มของคุณ ตอนนี้ต้องทำการตรวจสอบ captcha ของคุณโดยใช้ผู้สังเกต ดังนั้นฉันใช้ postispatch เหตุการณ์ preispatch สำหรับการตรวจสอบ
ขั้นตอนที่ 6: Vendor/Module/etc/frontend/events.xml
<? xml version = "1.0"?>
<config xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: noNamespaceSchemaLocation = "โกศ: magento: กรอบงาน: กิจกรรม / etc / events.xsd">
<event name = "controller_action_predispatch_yourroute_index_post">
<observer name = "captcha_custom_form" instance = "ผู้ขาย \ Module \ Observer \ CheckCustomFormObserver" />
</ เหตุการณ์>
</ config>
ขั้นตอนที่ 7: Vendor/Module/Observer/CheckCustomFormObserver.php
เนมสเปซ Vendor \ Module \ Observer;
ใช้ Magento \ Framework \ Event \ ObserverInterface;
ใช้ Magento \ Framework \ App \ Request \ DataPersistorInterface;
ใช้ Magento \ Framework \ App \ ObjectManager;
ใช้ Magento \ Captcha \ Observer \ CaptchaStringResolver;
ระดับ CheckCustomFormObserver ใช้ ObserverInterface
{
/ **
* @var \ Magento \ Captcha \ Helper \ Data
* /
ป้องกัน $ _helper
/ **
* @var \ Magento \ Framework \ App \ ActionFlag
* /
ป้องกัน $ _actionFlag;
/ **
* @var \ Magento \ Framework \ Message \ Manager อินเทอร์เฟซ
* /
ป้องกัน $ messageManager;
/ **
* @var \ Magento \ Framework \ App \ Response \ RedirectInterface
* /
ป้องกันการเปลี่ยนเส้นทาง $;
/ **
* @var CaptchaStringResolver
* /
ป้องกัน $ captchaStringResolver;
/ **
* @var DataPersistorInterface
* /
ส่วนตัว $ dataPersistor;
/ **
* @param \ Magento \ Captcha \ Helper \ Data $ helper
* @param \ Magento \ Framework \ App \ ActionFlag $ actionFlag
* @param \ Magento \ Framework \ Message \ Manager อินเทอร์เฟซ $ messageManager
* @param \ Magento \ Framework \ App \ Response \ RedirectInterface $ redirect
* @param CaptchaStringResolver $ captchaStringResolver
* /
ฟังก์ชั่นสาธารณะ __ โครงสร้าง (
\ Magento \ Captcha \ Helper \ Data $ ผู้ช่วย
\ Magento \ Framework \ App \ ActionFlag $ actionFlag,
\ Magento \ Framework \ Message \ Manager อินเทอร์เฟซ $ messageManager
\ Magento \ Framework \ App \ Response \ RedirectInterface $ redirect
CaptchaStringResolver $ captchaStringResolver
) {
$ this -> _ helper = $ helper;
$ this -> _ actionFlag = $ actionFlag;
$ this-> messageManager = $ messageManager;
$ this-> redirect = $ redirect;
$ this-> captchaStringResolver = $ captchaStringResolver;
}
/ **
* ตรวจสอบ CAPTCHA ในแบบฟอร์มที่กำหนดเอง
* * * *
* @param \ Magento \ Framework \ Event \ Observer $ observer
* @return ถือเป็นโมฆะ
* /
ฟังก์ชั่นสาธารณะดำเนินการ (\ Magento \ Framework \ Event \ Observer $ observer)
{
$ formId = 'custom_form';
$ captcha = $ this -> _ helper-> getCaptcha ($ formId);
if ($ captcha-> isRequired ()) {
/ ** @var \ Magento \ Framework \ App \ Action $ Action $ controller * /
$ controller = $ observer-> getControllerAction ();
if (! $ captcha-> isCorrect ($ this-> captchaStringResolver-> แก้ไข ($ controller-> getRequest (), $ formId)))
$ this-> messageManager-> addError (__ ('CAPTCHA ไม่ถูกต้อง.'))
$ this-> getDataPersistor () -> set ($ formId, $ controller-> getRequest () -> getPostValue ());
$ this -> _ actionFlag-> set ('', \ Magento \ Framework \ App \ Action \ Action :: FLAG_NO_DISPATCH จริง);
$ this-> redirect-> redirect ($ controller-> getResponse (), 'yourroute / index / index');
}
}
}
/ **
* รับ Data Persistor
* * * *
* @return DataPersistorInterface
* /
ฟังก์ชั่นส่วนตัว getDataPersistor ()
{
if ($ this-> dataPersistor === null) {
$ this-> dataPersistor = ObjectManager :: getInstance ()
-> ได้รับ (DataPersistorInterface :: ชั้น);
}
ส่งคืน $ this-> dataPersistor;
}
}
สำหรับคนที่ไม่สามารถทำงานนี้ได้คุณอาจต้องทำสิ่งที่ฉันทำ:
เหตุผลที่คุณ captcha อาจไม่แสดงเป็นเพราะการตั้งค่าพื้นฐานคือการใช้บล็อก captcha เริ่มต้นซึ่งใน _toHtml จะตรวจสอบเพื่อดูว่าจำเป็นต้องมี captcha หรือไม่
หากคุณมีการตั้งค่าให้ captcha แสดงตลอดเวลากว่าที่คุณอาจไม่ได้พบกับปัญหานี้อย่างไรก็ตามหากไม่ได้ตั้งค่าให้แสดง captchas เสมอและคุณไม่ต้องการแสดง captchas เสมอ (เช่นสร้างบัญชี / เข้าสู่ระบบเป็นต้น) กว่าที่คุณต้องการ ตั้งค่าตรรกะสำหรับเฉพาะ captcha ที่คุณกำหนดเองเป็น "ต้องมีเสมอ"
ในบรรทัดที่ 69 ของผู้ขาย / magento / module-captcha / Block / Captcha / DefaultCaptcha.phpคุณจะเห็น:
/**
* Renders captcha HTML (if required)
*
* @return string
*/
protected function _toHtml()
{
if ($this->getCaptchaModel()->isRequired()) {
$this->getCaptchaModel()->generate();
return parent::_toHtml();
}
return '';
}
$this->getCaptchaModel()การโทร$this->_captchaData->getCaptcha()ที่อยู่ใน
ผู้ขาย / magento / module-captcha / Helper / Data.php
/**
* Get Captcha
*
* @param string $formId
* @return \Magento\Captcha\Model\CaptchaInterface
*/
public function getCaptcha($formId)
{
if (!array_key_exists($formId, $this->_captcha)) {
$captchaType = ucfirst($this->getConfig('type'));
if (!$captchaType) {
$captchaType = self::DEFAULT_CAPTCHA_TYPE;
} elseif ($captchaType == 'Default') {
$captchaType = $captchaType . 'Model';
}
$this->_captcha[$formId] = $this->_factory->create($captchaType, $formId);
}
return $this->_captcha[$formId];
}
ที่นี่เมธอด getCaptcha ตรวจสอบค่ากำหนดสำหรับประเภทของ captcha เพื่อแสดงผลและโหลดจากโรงงานด้วย $this->_factory->create()
อย่างไรก็ตามการก้าวเข้าสู่ระดับโรงงานคุณจะเห็น
public function create($captchaType, $formId)
{
$className = 'Magento\Captcha\Model\\' . ucfirst($captchaType);
$instance = $this->_objectManager->create($className, ['formId' => $formId]);
if (!$instance instanceof \Magento\Captcha\Model\CaptchaInterface) {
throw new \InvalidArgumentException(
$className . ' does not implement \Magento\Captcha\Model\CaptchaInterface'
);
}
return $instance;
}
ปัญหาที่นี่คือไม่ว่าโรงงานจะมีลักษณะอย่างไรในโมดูล Magento Captcha สำหรับโรงงานทุกรุ่น ..
เราจำเป็นต้องสร้างปลั๊กอินเพื่อล้อมรอบตัวช่วยและตรวจสอบคีย์ฟอร์มของเราและถ้ามันเป็นคีย์ฟอร์มของเราที่ใช้เราจำเป็นต้องสร้างคลาสโรงงานใหม่ที่โหลดโมเดลของเราที่ขยายโมเดล\ Magento \ Captcha \ Model \ DefaultModelและ overides วิธีการ isRequired () สิ่งที่มีลักษณะเช่นนี้:
ใน\ Your \ Module \ etc \ di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<!--Custom Captcha-->
<type name="\Magento\Captcha\Helper\Data">
<plugin name="custom-captcha" type="Your\Module\Plugin\Helper\CaptchaData" />
</type>
ใน\ Module \ Plugin \ Helper \ CaptchaData ของคุณ
<?php
namespace Your\Module\Plugin\Helper;
class CaptchaData
{
protected $_captcha = [];
public function __construct(
\Your\Module\Model\CaptchaFactory $captchaFactory
) {
$this->captchaFactory = $captchaFactory;
}
/**
* @param \Magento\Captcha\Helper\Data $subject
* @param \Closure $proceed
* @param $formId
* @return mixed
*/
public function aroundGetCaptcha(\Magento\Captcha\Helper\Data $subject, \Closure $proceed, $formId)
{
if ($formId == 'your_form_key') {
$this->_captcha[$formId] = $this->captchaFactory->create();
return $this->_captcha[$formId];
}
return $proceed($formId);
}
}
ใน\ Your \ Module \ Model \ CaptchaFactory
<?php
/**
* Captcha model factory
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Your\Module\Model;
class CaptchaFactory
{
/**
* @var \Magento\Framework\ObjectManagerInterface
*/
protected $_objectManager;
/**
* @param \Magento\Framework\ObjectManagerInterface $objectManager
*/
public function __construct(\Magento\Framework\ObjectManagerInterface $objectManager)
{
$this->_objectManager = $objectManager;
}
/**
* Get captcha instance
*
* @param string $captchaType
* @param string $formId
* @return \Magento\Captcha\Model\CaptchaInterface
* @throws \InvalidArgumentException
*/
public function create()
{
$instance = $this->_objectManager->create('Your\Module\Model\Captcha', ['formId' => 'event_subscriber']);
if (!$instance instanceof \Magento\Captcha\Model\CaptchaInterface) {
throw new \InvalidArgumentException(
'Your\Module\Model\Captcha does not implement \Magento\Captcha\Model\CaptchaInterface'
);
}
return $instance;
}
}
และในที่สุดโมเดลของคุณที่จะเอาชนะพารามิเตอร์ที่จำเป็นใน \ Your \ Module \ Model \ Captcha :
<?php
namespace Your\Module\Model;
class Captcha extends \Magento\Captcha\Model\DefaultModel
{
public function isRequired($login = null)
{
return true;
}
}
ฉันต้องการ captcha ในหน้าสมาชิกจดหมายข่าวขอบคุณ sohan ฉันใช้ผู้สังเกตการณ์จดหมายข่าวและ captcha ทำงานในหน้าจดหมายข่าวสำหรับฉัน
1) แอป / รหัส / Vendorname / Modulename / etc / config.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<customer>
<captcha>
<shown_to_logged_in_user>
<custom_newsletter>1</custom_newsletter>
</shown_to_logged_in_user>
<always_for>
<custom_newsletter>1</custom_newsletter>
</always_for>
</captcha>
</customer>
<captcha translate="label">
<frontend>
<areas>
<custom_newsletter>
<label>Newsletter Form</label>
</custom_newsletter>
</areas>
</frontend>
</captcha>
</default>
</config>
2) ไปที่ 'ผู้ดูแลระบบ -> ร้านค้า -> การกำหนดค่า -> ลูกค้า -> การกำหนดค่าลูกค้า -> แคปต์ชา' และกำหนดค่า คุณสามารถเห็นค่าแบบฟอร์มใหม่ 'แบบฟอร์มจดหมายข่าว'
3) คัดลอกไฟล์โครงร่างในชุดรูปแบบ (default.xml)
<block class="Magento\Newsletter\Block\Subscribe" name="subscribe form " template="Magento_Newsletter::subscribe.phtml">
<container name="form.additional.info" label="Form Additional Info">
<block class="Magento\Captcha\Block\Captcha" name="captcha" after="-" cacheable="false">
<action method="setFormId">
<argument name="formId" xsi:type="string">custom_newsletter</argument>
</action>
<action method="setImgWidth">
<argument name="width" xsi:type="string">230</argument>
</action>
<action method="setImgHeight">
<argument name="width" xsi:type="string">50</argument>
</action>
</block>
</container>
4) สร้างผู้สังเกตการณ์ -> สร้างไฟล์ event.xml ในแอพ / รหัส / Vendorname / Modulename / etc / frontend
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="controller_action_predispatch_newsletter_subscriber_new">
<observer name="captcha_newletter_form" instance="Vendorname/Modulename/Observer\CheckCustomFormObserver" />
</event>
</config>
5) สร้างแบบจำลองผู้สังเกตการณ์และตรวจสอบแอปแคปต์ชา / รหัส / Vendorname / Modulename / ผู้สังเกตการณ์ / CheckCustomFormObserver.php
public function execute(\Magento\Framework\Event\Observer $observer)
{ $formId = 'custom_newsletter';
$captcha = $this->_helper->getCaptcha($formId);
if ($captcha->isRequired()) {
/** @var \Magento\Framework\App\Action\Action $controller */
$controller = $observer->getControllerAction();
$params=$controller->getRequest()->getPost();
$currentpage = $params['currentpage'];
if (!$captcha->isCorrect($this->captchaStringResolver->resolve($controller->getRequest(), $formId))) {
$this->messageManager->addError(__('Incorrect CAPTCHA.'));
$this->getDataPersistor()->set($formId, $controller->getRequest()->getPostValue());
$this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
$this->redirect->redirect($controller->getResponse(), $currentpage);
}
}
}