Add this Code to vendor\Module\Controller\Index/Index.php
<?php
namespace vendor\Module\Controller\Index;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\Action\Context;
class Index extends \Magento\Framework\App\Action\Action {
/**
* @var \Magento\Framework\Controller\Result\ForwardFactory
*/
protected $resultPageFactory;
/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory
*/
public function __construct(
\Magento\Framework\App\Action\Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory
) {
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context);
}
public function execute() {
$resultPageFactory = $this->resultPageFactory->create();
// Add page title
$resultPageFactory->getConfig()->getTitle()->set(__('Sample Module'));
// Add breadcrumb
/** @var \Magento\Theme\Block\Html\Breadcrumbs */
$breadcrumbs = $resultPageFactory->getLayout()->getBlock('breadcrumbs');
$breadcrumbs->addCrumb('home', [
'label' => __('Home'),
'title' => __('Home'),
'link' => $this->_url->getUrl('')
]
);
$breadcrumbs->addCrumb('custom_module', [
'label' => __('Test'),
'title' => __('Test')
]
);
return $resultPageFactory;
}
}
After Run
bin/magento setup:upgrade
remove var/*
rm -rf var/*
Now, check This is Works for me.
create a controller Save.php
and copy paste in your module.
<?php
namespace [Namespace]\[ModuleName]\Controller\Index;
class Save extends \Magento\Framework\App\Action\Action
{
/**
* @var \Magento\Framework\View\Result\PageFactory
*/
protected $resultPageFactory;
/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory
)
{
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context);
}
/**
* Default customer account page
*
* @return void
*/
public function execute()
{
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
try{
$request = $this->getRequest()->getParams();
$email = $request['email'];
$this->resultPageFactory->create();
return $resultRedirect->setPath('contactus/index/index');
}catch (\Exception $e){
$this->messageManager->addExceptionMessage($e, __('We can\'t submit your request, Please try again.'));
$this->_objectManager->get('Psr\Log\LoggerInterface')->critical($e);
return $resultRedirect->setPath('contactus/index/index');
}
}
}
?>
create save.phtml
file from [Namespace]/[ModuleName/view/frontend/templates
and copy paste it below code.
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->create('\Magento\Store\Model\StoreManagerInterface');
$baseURL_l = $social_image_url = $storeManager->getStore()->getBaseUrl();
?>
<form action="<?php /* @escapeNotVerified */ echo $baseURL_l . 'contactus/index/save' ?>" method="post" id="form-validate" enctype="multipart/form-data" autocomplete="off" data-mage-init='{"validation":{}}' data-hasrequired="<?php /* @escapeNotVerified */ echo __('* Required Fields') ?>">
<div class="field required">
<label for="email_address" class="label"><span><?php /* @escapeNotVerified */ echo __('Email') ?></span><span class="span">*</span></label>
<div class="control">
<input type="email" name="email" autocomplete="email" id="email_address" value="" title="<?php /* @escapeNotVerified */ echo __('Email') ?>" class="input-text" data-validate="{required:true, 'validate-email':true}">
</div>
</div><br/>
<div class="primary">
<button type="submit" class="action submit primary" title="<?php /* @escapeNotVerified */ echo __('Submit') ?>"><span><?php /* @escapeNotVerified */ echo __('Submit') ?></span></button>
</div>
</form>
Now enter the email and hit the Submit button you will get the email $request['email']
this variable. So you can do it from your logic.
Best Answer
Just create an entry in Admin Panel > Marketing > SEO & Search > URL Rewrites
with request path as slug and target path as trails/post/view/id/13
This should help.