magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.57k stars 9.32k forks source link

'magento' adding in the URL when Use Web Server Rewrites set to NO using Console Command #25976

Open sandip5678 opened 4 years ago

sandip5678 commented 4 years ago

Summary (*)

This is a Developer experience issue. I have created one simple console file that will generate product name, URL etc.

  1. Magento 2.3.x with sample data
  2. Use Web Server Rewrites field set to No
  3. Clear cache if prompted

Examples (*)

  1. app/code/Custom/Pro/etc/di.xml
<?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\CommandListInterface">
        <arguments>
            <argument name="commands" xsi:type="array">
                <item name="checkProductURL" xsi:type="object">Custom\Pro\Console\Command\ProductURLFetch</item>
            </argument>
        </arguments>
    </type>
</config>
  1. app/code/Custom/Pro/Console/Command/ProductURLFetch.php
<?php

namespace Custom\Pro\Console\Command;

use Magento\Catalog\Api\ProductRepositoryInterfaceFactory;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Framework\App\ObjectManagerFactory;
use Magento\Framework\App\State;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ProductURLFetch extends \Symfony\Component\Console\Command\Command
{
    /**
     * @var \Magento\Catalog\Api\ProductAttributeRepositoryInterface
     */
    protected $productAttributeRepository;

    /**
     * @var \Magento\Framework\Api\SearchCriteriaBuilder
     */
    protected $searchCriteriaBuilder;

    /**
     * @var \Magento\Catalog\Model\ResourceModel\Attribute
     */
    protected $attributeResource;

    /**
     * @var State
     */
    protected $appState;

    protected $_productRepositoryFactory;

    /**
     * @var \Magento\Framework\EntityManager\EntityMetadata
     */
    protected $metadata;

    /**
     * Object manager factory
     *
     * @var ObjectManagerFactory
     */
    private $objectManagerFactory;
    /**
     * @var CollectionFactory
     */
    private $collectionFactory;

    /**
     * @param ObjectManagerFactory $objectManagerFactory
     * @param CollectionFactory $collectionFactory
     * @param State $appState
     * @param ProductRepositoryInterfaceFactory $productRepositoryFactory
     */
    public function __construct(
        ObjectManagerFactory $objectManagerFactory,
        CollectionFactory $collectionFactory,
        State $appState,
        ProductRepositoryInterfaceFactory $productRepositoryFactory
    ) {
        $this->objectManagerFactory = $objectManagerFactory;
        $this->appState = $appState;
        $this->collectionFactory = $collectionFactory;
        $this->_productRepositoryFactory = $productRepositoryFactory;
        parent::__construct();
    }

    /**
     * {@inheritdoc}
     */
    protected function configure()
    {
        $this->setName('catalog:product:showurl');
        $this->setDescription('It will just show Product URLs.');
    }

    /**
     * {@inheritdoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->appState->setAreaCode(\Magento\Framework\App\Area::AREA_FRONTEND);

        $productCollection = $this->collectionFactory->create();
        $output->setDecorated(true);
        $productCollection->addAttributeToSelect('name','sku','url');        
        try {
            foreach ($productCollection as $product){
                $output->writeln( $product->getProductURL()  );
            }

            $output->writeln("<info>Product URLs has been generated..</info>");

            return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
        } catch (\Exception $exception) {

            $output->writeln("");
            $output->writeln("<error>{$exception->getMessage()}</error>");
            // we must have an exit code higher than zero to indicate something was wrong
            return \Magento\Framework\Console\Cli::RETURN_FAILURE;
        }
    }

}

Execute below console. php bin/magento catalog:product:showurl

Output URL in the output will be as follows: http://www.example.com/magento/kir-1100.html

It should not contain 'magento' as in the URL. http://www.example.com/kir-1100.html

Proposed solution

Looking into the file /vendor/magento/module-store/Model/Store.php and function named _updatePathUseRewrites returns base URL with 'magento'.

m2-assistant[bot] commented 4 years ago

Hi @sandip5678. Thank you for your report. To help us process this issue please make sure that you provided the following information:

Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, please, review the Magento Contributor Assistant documentation.

@sandip5678 do you confirm that you were able to reproduce the issue on vanilla Magento instance following steps to reproduce?


m2-assistant[bot] commented 4 years ago

Hi @engcom-Charlie. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

magento-engcom-team commented 4 years ago

:white_check_mark: Confirmed by @engcom-Charlie Thank you for verifying the issue. Based on the provided information internal tickets MC-29753 were created

Issue Available: @engcom-Charlie, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

rmsundar1 commented 3 years ago

@magento I am working on this