To continue with the question below
Magento 2: How to define Scope For Router.php
app\code\Custom\Module\view\frontend\templates\posts\view.phtml
<form class="form post reply"
action="<?php /* @escapeNotVerified */ echo $block->getSaveUrl(); ?>"
id="post_reply_form"
name="postReplyForm"
method="post"
data-hasrequired="<?php /* @escapeNotVerified */ echo __('* Required Fields') ?>"
data-mage-init='{"validation":{}}'
>
<fieldset class="fieldset required">
<?php echo $block->getBlockHtml('formkey') ?>
<div class="field post_reply_description required">
<label class="label" for="post_reply_description"><span><?php /* @escapeNotVerified */ echo __('Message') ?></span></label>
<div class="control">
<textarea name="post_reply_description" id="post_reply_description" title="<?php /* @escapeNotVerified */ echo __('Description') ?>" class="input-text" cols="5" rows="3" data-validate="{required:true}"></textarea>
</div>
</div>
</fieldset>
<div class="actions-toolbar">
<div class="primary">
<button type="submit" title="<?php /* @escapeNotVerified */ echo __('Submit') ?>" class="action submit primary">
<span><?php /* @escapeNotVerified */ echo __('Submit') ?></span>
</button>
</div>
</div>
</form>
app\code\Custom\Module\Block\Posts\ViewPosts.php
namespace Custom\Module\Block\Posts;
use Magento\Framework\Registry;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Data\Collection;
use Magento\Framework\Filesystem;
use Magento\Framework\Image\AdapterFactory;
use Magento\Framework\Message\ManagerInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
class ViewPosts extends Template {
/**
* @var Registry
*/
protected $coreRegistry;
/**
* @var ModuleURL
*/
protected $ModuleURL;
/**
* @var _filesystem
*/
protected $_filesystem;
/**
* @var _imageFactory
*/
protected $_imageFactory;
/**
* @param Context $context
* @param Registry $registry
* @param array $data
*/
public function __construct(
Context $context, Registry $registry, Filesystem $filesystem, AdapterFactory $imageFactory, ScopeConfigInterface $scopeConfig, array $data = []
) {
$this->coreRegistry = $registry;
$this->_filesystem = $filesystem;
$this->_imageFactory = $imageFactory;
$this->scopeConfig = $scopeConfig;
parent::__construct($context, $data);
}
protected function _prepareLayout() {
parent::_prepareLayout();
return $this;
}
/**
* Return the Url for saving.
*
* @return string
*/
public function getSaveUrl() {
return $this->_urlBuilder->getUrl('mymodule/posts/postReplyForm');
}
}
app\code\Custom\Module\Controller\Posts\PostReplyForm.php
namespace Custom\Module\Controller\Posts;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\Filesystem\DirectoryList;
class PostReplyForm {
/**
* @var DataPersistorInterface
*/
private $dataPersistor;
/**
* Post user post
*
* @return void
* @throws \Exception
*/
public function execute() {
$post = $this->getRequest()->getPostValue();
echo "<pre>";
print_r($_REQUEST);
exit;
}
}
What's reason why it's not going in controller file?
[UPDATE]
https://github.com/tzyganu/Magento2SampleModule/blob/master/Controller/Router.php
It's going to line number 151 & returing 404 page.
I would like to define scope (as my previous question) that when there is only detail page with .html extension then only it will call Router.php
file otherwise it works as it is.
Trying to do patch over here
app\code\Custom\Module\Controller\Router.php
Which causes another error for redirection
if ($postsURLSuffix) {
$suffix = substr($urlKey, -strlen($postsURLSuffix) - 1);
if ($suffix != '.' . $postsURLSuffix) {
// START FOR POST MESSAGE FORM REDIRECT
if (strpos($request->getServer('REQUEST_URI'), 'postReplyForm') !== false) {
$redirectURL = "http://localhost".$_SERVER['REQUEST_URI'];
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$resultRedirect->setPath($redirectURL);
return $resultRedirect;
}
// END FOR POST MESSAGE FORM REDIRECT
return false;
}
$urlKey = substr($urlKey, 0, -strlen($postsURLSuffix) - 1);
}
Which gives fatal error
Fatal error: Call to undefined method
Magento\Framework\Controller\Result\Redirect\Interceptor::execute() in
D:\wamp\www\magento2\vendor\magento\framework\App\FrontController.php
on line 57
Best Answer
You have to keep below code