1

In magento one Mage_Core_Model_Url_Rewrite->rewrite method

where I will find same method in magento2?

1

Hope, you want custom rewrite

So, in this case, you have to use two class:

1.Magento\UrlRewrite\Model\UrlRewrite

2.Magento\UrlRewrite\Helper\UrlRewrite

Model\UrlRewrite for saving the record and Helper\UrlRewrite use for validated the rewrite before save.

Example:

<?php

/**
 * Amit Bera
 */

namespace StackExchange\Magentodemo\Model;

/**
 * Description of CustomUrl
 *
 * @author Amit Bera
 */
class CustomUrl {

    const ENTITY_TYPE_CUSTOM = 'custom';
    /**
     * @var \Magento\UrlRewrite\Helper\UrlRewrite
     */
    protected $helperUrlRewrite;

    /**
     * @var \Magento\UrlRewrite\Model\UrlRewriteFactory
     */
    protected $urlRewriteFactory;

    public function __construct(
            \Magento\UrlRewrite\Helper\UrlRewrite $helperUrlRewrite,
            \Magento\UrlRewrite\Model\UrlRewriteFactory $urlRewriteFactory
            ) {

                $this->urlRewriteFactory = $urlRewriteFactory;
                $this->helperUrlRewrite = $helperUrlRewrite;
    }
    public function testCustomUrl()
    {
        $requestPath ='devamitbera';
        $storeId = 0; // if default then 0
        $targetPath = 'contact';
        $description ='Redirect devamitbera to contact us page';
        // 301 = Permanent redirection (301),
        // 302 = Temporary redirection (302)
        //  0 = for No redirection
         $redirect_type =  301;

        $urlRewrite = $this->urlRewriteFactory->create();
        $validated = $this->helperUrlRewrite->validateRequestPath($requestPath);
        if( $validated){
            $urlRewrite->setEntityType(self::ENTITY_TYPE_CUSTOM)
                    ->setRequestPath($requestPath)
                    ->setTargetPath($targetPath)
                    ->setRedirectType($redirect_type)
                    ->setStoreId( $storeId)
                    ->setDescription($description);
        }

    }
}

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.