1

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

  • Could you post the code in your routes.xml file please ? – Raphael at Digital Pianism Sep 15 '16 at 10:25
  • try module/posts/postReplyFor with module/posts/postreplyfor – Rakesh Jesadiya Sep 15 '16 at 10:25
  • @RaphaelatDigitalPianism already mentioned routes.xml in previous question link – Ankit Shah Sep 15 '16 at 10:26
  • Sorry it's typo mistake in question i have updated question. – Ankit Shah Sep 15 '16 at 10:32
0

You have to keep below code

public function getSaveUrl() {
    return $this->_urlBuilder->getUrl('mymodule/posts/postreplyform');
}
  • Sorry it's typo mistake in question i have updated question. – Ankit Shah Sep 15 '16 at 10:33
  • plz show your block file to more debug – Rakesh Jesadiya Sep 15 '16 at 10:34
  • Updated in question – Ankit Shah Sep 15 '16 at 10:39
  • I think there is an issue with Router.php @Rakesh as i'm referring to that module for my development. – Ankit Shah Sep 16 '16 at 1:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.