2

I have a controller for adminhtml area like this:

namespace Namespace\Module\Controller\Adminhtml\Order;

use Magento\Framework\Controller\ResultFactory;
use Magento\Sales\Model\Order;

class Complete extends \Magento\Backend\App\Action
{
    protected $resultPageFactory;

    public function __construct(
        \Magento\Backend\App\Action\Context $context,
    \Magento\Sales\Api\OrderRepositoryInterface $order
    )
    {
    $this->order = $order;
        parent::__construct($context);
    }

    public function execute()
    {
    $orderId = $this->request->getParam('order_id');
    $order = $this->order->get($orderId);
    if(!empty($order->getId())){
      try {
        $orderState = Order::STATE_COMPLETE;
        $order->setState($orderState)->setStatus(Order::STATE_COMPLETE);
        $this->messageManager->addSuccess(__("Successfully completed the order"));
      } catch (Exception $e) {
        $this->messageManager->addError(__("There's something wrong, please try again!"));
      }
    } else {
      $this->messageManager->addError(__("Invalid Order"));
    }
    $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
    $resultRedirect->setUrl($this->_redirect->getRefererUrl());
    return $resultRedirect;
    }
}

i want to generate the url to redirect to this controller with magento GET parameter order_id, how to do it in block file, so i can get the url from my phtml?

edit: i tried to do it like this:

$url = $block->getUrl('mymodulerouter/order/complete', ['param' => array("order_id"=>$order->getId())]);

echo $url;

but the result only generate the url to the controller without request get parameter, like this:

http://baseurl.com/adminhtml/mymodulerouter/order/complete/key/3t45n892808da9ea52mij56b87vv87f37f9c7y79886209d27caf9df3e7dc7c9c07/

1
  • what is your module etc/adminhtml routes.xml file frontname? Commented Dec 27, 2017 at 6:31

1 Answer 1

1

For multiple querystring variable

<?php
$params = [
    'order_id' => $block->getOrderId(),
    'website_id' => $block->getWebsiteId(),
];
?>
$url = $block->getUrl('mymodulerouter/order/complete', $params)
echo $url;

For single variable,

$url = $block->getUrl('mymodulerouter/order/complete', ['order_id' => $order->getId()])
echo $url;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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