0

In my controller BVZ\Customer\Controller\Index I'm doing a redirect using

return $this->_redirect('customer/index/firstAccess');

When the code execute, the request are made (see the print below) below enter image description here

but the url in browser stay in the same place.

how can i make the page redirect to 'customer/index/firstAccess' and not just make the request?

all code of my controller

<?php

namespace BVZ\Customer\Controller\Index;

class CustomerExists extends \Magento\Framework\App\Action\Action {

  public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
        \Magento\Customer\Api\AccountManagementInterface $customerAccountManagement
  ) {
        parent::__construct($context);
            $this->resultJsonFactory = $resultJsonFactory;
            $this->customerAccountManagement = $customerAccountManagement;
  }

  public function execute() {
    $cpfcpnj = $this->getRequest()->getParam('cpfcpnj');

    $vowels = array("-", ".","/");
    $cpfcpnj = str_replace($vowels, "", $cpfcpnj);

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $customerObj = $objectManager->create('Magento\Customer\Model\ResourceModel\Customer\Collection');
    $collection = $customerObj
                  ->addAttributeToSelect('*')
                  ->addAttributeToFilter('taxvat',$cpfcpnj)
                  ->load()
                  ->getFirstItem();

  if($collection->getId()) {

      if($collection->getData('firstAccess')!=0){

          //email reset
          return $this->_redirect('customer/account/ResetPasswordPost');
          $return = "first";
      } else {
          $return = "exists";
      }

  } else {
      $return = "new";
  }

  $result = $this->resultJsonFactory->create();
  return $result->setData($return);
2
  • Hi, Please provide more information about your code and files to make it easier for everyone to understand the problem better.
    – Sarvagya
    Commented Dec 19, 2018 at 12:03
  • Hello, i have updated the question. Please check if became clearer. Commented Dec 19, 2018 at 12:15

2 Answers 2

0
public function execute()
{
    $resultRedirect = $this->resultRedirectFactory->create();
    $resultRedirect->setPath('routername/controllerpath/controllername');
    return $resultRedirect;
}
1
  • This way it worked. I just needed to improve the AJAX, how the request was made via AJAX, the process redirect worked in php, but JS continued the process without counting this redirect. So I needed to set where I want to go in php and use another call in JS to redirect. Commented Dec 21, 2018 at 10:44
0

Controller Result

Review this file: vendor/magento/framework/Controller/ResultFactory.php

Here is a sample of the ResultFactory usage:

// The use statement below is for constants. $this->resultFactory is inherited from parent class
use Magento\Framework\Controller\ResultFactory;

class Sample extends \Magento\Framework\App\Action\Action
{

    $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);

    $resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);   

    /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
    $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
    return $resultRedirect->setPath('*/*/');

}

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.