cancel
Showing results for 
Search instead for 
Did you mean: 

URL Rewrite SourceModel error.

SOLVED

URL Rewrite SourceModel error.

Hi,

 

I created console command to add some URL rewrites for custom pages.

I found code snippet - https://www.mageplaza.com/magento-2-url-rewrite-programmatically.html

But it didn't work for me.

I got following error message

 

PHP Fatal error:  Uncaught Error: Call to undefined method Magento\UrlRewrite\Model\ResourceModel\UrlRewrite::setStoreId() in /data/magento_ia/magento2/magento_ia/app/code/IA/ProductDescrGen/Console/Command/IaBrandFeatureListUrlRewrite.php:37
Stack trace:
#0 /data/magento_ia/magento2/magento_ia/vendor/symfony/console/Symfony/Component/Console/Command/Command.php(257): IA\ProductDescrGen\Console\Command\IaBrandFeatureListUrlRewrite->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#1 /data/magento_ia/magento2/magento_ia/vendor/symfony/console/Symfony/Component/Console/Application.php(874): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#2 /data/magento_ia/magento2/magento_ia/vendor/symfony/console/Symfony/Component/Console/Application.php(195): Symfony\Component\Console\Application->doRunCommand(Object(IA\ProductDescrGen\Console\Command\IaBrandFeatureListUrl in /data/magento_ia/magento2/magento_ia/app/code/IA/ProductDescrGen/Console/Command/IaBrandFeatureListUrlRewrite.php on line 37

I try same code in front-end controller and get same error "Call to undefined method".

 

I use Magento 2.1.10

Store was migrated from M1.

 

What am I missing?

 

1 ACCEPTED SOLUTION

Accepted Solutions

Re: URL Rewrite SourceModel error.

You need to set below way your code, Remove extra ResourceModel from class,

Use 

\Magento\UrlRewrite\Model\UrlRewriteFactory $urlRewriteFactory

 Instead of 

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

Final code would be,

<?php

namespace IA\ProductDescrGen\Console\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputInterface;

class IaBrandFeatureListUrlRewrite extends Command
{

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

	public function __construct(
		\Magento\UrlRewrite\Model\UrlRewriteFactory $urlRewriteFactory
	)
	{
		$this->_urlRewriteFactory = $urlRewriteFactory;
		parent::__construct();
	}

	protected function configure()
	{
		$this->setName('ia:updateurl')->setDescription('Update URL rewrite rules for Brand Feature list pages.');
	}

	protected function execute(InputInterface $input, OutputInterface $output)
	{
		$output->writeln('Hello World!');

		$urlRewriteModel = $this->_urlRewriteFactory->create();
		/* set current store id */
		$urlRewriteModel->setStoreId(2);
		/* this url is not created by system so set as 0 */
		$urlRewriteModel->setIsSystem(0);
		/* unique identifier - set random unique value to id path */
		$urlRewriteModel->setIdPath(rand(1, 100000));
		/* set actual url path to target path field */
		$urlRewriteModel->setTargetPath("www.example.com/customModule/customController/customAction");
		/* set requested path which you want to create */
		$urlRewriteModel->setRequestPath("www.example.com/xyz");
		/* set current store id */
		$urlRewriteModel->save();
	}

}

Remove generated folder from root.

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

View solution in original post

7 REPLIES 7

Re: URL Rewrite SourceModel error.

@olukyanenko - okay , so as you are using Factory method , so might be issue with the compilation.

 

do one thing like remove var/generation directory by running command rm -rf var/generation

 

Then after run php bin/magento setup:di:compile and then clear and flush cache by running this command php bin/magento cache:clean and php bin/magento cache:flush

 

Then try it will works .

if issue solved,Click Kudos & Accept as Solution

Re: URL Rewrite SourceModel error.

@Manthan Dave - thanks for advice but it don't help.

Store in developer mode.

I cleared cache, page_cache and generation folder, before ask here.

 

The only step, I didn't run di:compile.

I try it now, and it doesn't help Smiley Sad

Re: URL Rewrite SourceModel error.

@olukyanenko  - Okay i understand !

 

So post your full module code  here - post all files.

 

like Model.php file , di.xml , controller file and whatever files you have created  so it helps us to troubleshoot the issue.

if issue solved,Click Kudos & Accept as Solution

Re: URL Rewrite SourceModel error.

Command file. 

<?php

namespace IA\ProductDescrGen\Console\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputInterface;

class IaBrandFeatureListUrlRewrite extends Command
{

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

	public function __construct(
		\Magento\UrlRewrite\Model\ResourceModel\UrlRewriteFactory $urlRewriteFactory
	)
	{
		$this->_urlRewriteFactory = $urlRewriteFactory;
		parent::__construct();
	}

	protected function configure()
	{
		$this->setName('ia:updateurl')->setDescription('Update URL rewrite rules for Brand Feature list pages.');
	}

	protected function execute(InputInterface $input, OutputInterface $output)
	{
		$output->writeln('Hello World!');

		$urlRewriteModel = $this->_urlRewriteFactory->create();
		/* set current store id */
		$urlRewriteModel->setStoreId(2);
		/* this url is not created by system so set as 0 */
		$urlRewriteModel->setIsSystem(0);
		/* unique identifier - set random unique value to id path */
		$urlRewriteModel->setIdPath(rand(1, 100000));
		/* set actual url path to target path field */
		$urlRewriteModel->setTargetPath("www.example.com/customModule/customController/customAction");
		/* set requested path which you want to create */
		$urlRewriteModel->setRequestPath("www.example.com/xyz");
		/* set current store id */
		$urlRewriteModel->save();
	}

}

di.xml file

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
	<type name="Magento\Framework\Console\CommandList">
		<arguments>
			<argument name="commands" xsi:type="array">
				<item name="ia_brand_feature_list" xsi:type="object">IA\ProductDescrGen\Console\Command\IaBrandFeatureListUrlRewrite</item>
			</argument>
		</arguments>
	</type>
</config>

That's it.

 

Command file includes same code snippet as I found in Google. I also see the same code in another articles.

I think the issue may be related to artifacts from M1.

Re: URL Rewrite SourceModel error.

@olukyanenko - okay i understand.

 

Instead of using Resource Model Factory -

 

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

 

Use Model UrlRewriteFactory as you are using method called setStoreId.

 

Like Below : 

\Magento\UrlRewrite\Model\UrlRewriteFactory $urlRewriteFactory

 

Try below function code in command file replace with your code :

 

<?php

namespace IA\ProductDescrGen\Console\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputInterface;

class IaBrandFeatureListUrlRewrite extends Command
{

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

	public function __construct(
		\Magento\UrlRewrite\Model\UrlRewriteFactory $urlRewriteFactory
	)
	{
		$this->_urlRewriteFactory = $urlRewriteFactory;
		parent::__construct();
	}

	protected function configure()
	{
		$this->setName('ia:updateurl')->setDescription('Update URL rewrite rules for Brand Feature list pages.');
	}

	protected function execute(InputInterface $input, OutputInterface $output)
	{
		$output->writeln('Hello World!');

		$urlRewriteModel = $this->_urlRewriteFactory->create();
		/* set current store id */
		$urlRewriteModel->setStoreId(2);
		/* this url is not created by system so set as 0 */
		$urlRewriteModel->setIsSystem(0);
		/* unique identifier - set random unique value to id path */
		$urlRewriteModel->setIdPath(rand(1, 100000));
		/* set actual url path to target path field */
		$urlRewriteModel->setTargetPath("www.example.com/customModule/customController/customAction");
		/* set requested path which you want to create */
		$urlRewriteModel->setRequestPath("www.example.com/xyz");
		/* set current store id */
		$urlRewriteModel->save();
	}

} 

Let me know if it works for you or not.

 

if issue solved,Click Kudos & Accept as Solution

Re: URL Rewrite SourceModel error.

You need to set below way your code, Remove extra ResourceModel from class,

Use 

\Magento\UrlRewrite\Model\UrlRewriteFactory $urlRewriteFactory

 Instead of 

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

Final code would be,

<?php

namespace IA\ProductDescrGen\Console\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputInterface;

class IaBrandFeatureListUrlRewrite extends Command
{

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

	public function __construct(
		\Magento\UrlRewrite\Model\UrlRewriteFactory $urlRewriteFactory
	)
	{
		$this->_urlRewriteFactory = $urlRewriteFactory;
		parent::__construct();
	}

	protected function configure()
	{
		$this->setName('ia:updateurl')->setDescription('Update URL rewrite rules for Brand Feature list pages.');
	}

	protected function execute(InputInterface $input, OutputInterface $output)
	{
		$output->writeln('Hello World!');

		$urlRewriteModel = $this->_urlRewriteFactory->create();
		/* set current store id */
		$urlRewriteModel->setStoreId(2);
		/* this url is not created by system so set as 0 */
		$urlRewriteModel->setIsSystem(0);
		/* unique identifier - set random unique value to id path */
		$urlRewriteModel->setIdPath(rand(1, 100000));
		/* set actual url path to target path field */
		$urlRewriteModel->setTargetPath("www.example.com/customModule/customController/customAction");
		/* set requested path which you want to create */
		$urlRewriteModel->setRequestPath("www.example.com/xyz");
		/* set current store id */
		$urlRewriteModel->save();
	}

}

Remove generated folder from root.

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

Re: URL Rewrite SourceModel error.