cancel
Showing results for 
Search instead for 
Did you mean: 

How to run php bin/magento regenerate:product:url command programmatically

How to run php bin/magento regenerate:product:url command programmatically

Hi Guys,

 

Any one let me know how can I run below command programmatically

 

 php bin/magento regenerate:product:url 

 

Thanks

1 REPLY 1

Re: How to run php bin/magento regenerate:product:url command programmatically

1. Create constructor File

/**

* @var \Magento\UrlRewrite\Model\ResourceModel\UrlRewriteFactory

*/

protected $_urlRewriteFactory;

/**

* @param Context $context

* @param \Magento\UrlRewrite\Model\ResourceModel\UrlRewriteFactory $urlRewriteFactory

*/

ublic function __construct(

    Context $context,

    \Magento\UrlRewrite\Model\ResourceModel\UrlRewriteFactory $urlRewriteFactory

) {

    $this->_eavAttributeFactory = $eavAttributeFactory;

    parent::__construct(

        $context

    );

}

 

2. Construct custom URL rewrites in execute method

 

$urlRewriteModel = $this->_urlRewriteFactory->create ()

/* set current store id */

$urlRewriteModel->setStoreId(1);

/* the following url is not formed by system so set as 0 */

$urlRewriteModel->setIsSystem(0);

/* unique identifier - place random unique value to id path */

$urlRewriteModel->setIdPath(rand(1, 100000));

/* place actual url path to target path field */

$urlRewriteModel->setTargetPath("www.website.com/yourModule/yourController/yourAction");

/* set requested path which you desire to form */

$urlRewriteModel->setRequestPath("www.website.com/xyz");

/* set current store id */

$urlRewriteModel->save();