To get tthe category url by using the category name

To get tthe category url by using the category name

July 16, 2026 · Illustrated author avatarBy Stefan Bogdanescu
To get tthe category url by using the category name

Retrieving Magento Category URLs by Name

In Magento, you may find yourself in situations where you need to access category information based on the name rather than the ID. This can be particularly useful for generating SEO-friendly URLs or for handling legacy data migrations. In this post, we’ll explore how to get a category URL using only the category name.

Understanding Category Structure

Before diving into the code, it’s essential to understand Magento’s category hierarchy and naming conventions:

  • Categories are represented as nodes in a tree structure.
  • Each node has an ID (category_id), a name (name), and a path to its parent (path).
  • The URL key is used to generate the category’s URL.

Getting Category URLs by Name

To fetch a category URL using only the name, you’ll need to create a custom model or controller that utilizes Magento’s category collection. Here’s an example of how to write this functionality as a model:

// app/code/Vendor/Extension/Model/CategoryLoader.php

namespace Vendor\Extension\Model;

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Model\AbstractModel;

class CategoryLoader extends AbstractModel
{
    protected $categoryFactory;

    public function __construct(
        \Magento\Catalog\Model\CategoryFactory $categoryFactory
    ) {
        $this->categoryFactory = $categoryFactory;
    }

    public function loadByName($name)
    {
        $collection = $this->categoryFactory->create()->getCollection();
        $collection->addAttributeToSelect('entity_id');
        $collection->addFieldToFilter('name', ['like' => "%$name%"]);

        if ($collection->count() === 0) {
            throw new NoSuchEntityException(__("The category with name '%1' doesn't exist.", $name));
        }

        return $this->categoryFactory->create()->load($collection->getFirstItem()->getEntityId());
    }
}

In this model, we create a collection of all categories and filter it by the provided name. We then load the first matching category using its ID.

Using the CategoryLoader

To use this CategoryLoader, you can inject it into your controller or service:

// app/code/Vendor/Extension/Controller/Index/Index.php

namespace Vendor\Extension\Controller\Index;

use Magento\Framework\App\ActionInterface;
use Magento\Framework\Controller\ResultFactory;

class Index implements ActionInterface
{
    protected $categoryLoader;

    public function __construct(
        \Vendor\Extension\Model\CategoryLoader $categoryLoader
    ) {
        $this->categoryLoader = $categoryLoader;
    }

    public function execute()
    {
        try {
            $category = $this->categoryLoader->loadByName('Example Category');
            $url = $category->getUrl();
        } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
            // Handle exception (e.g., set a custom message or redirect)
            return $this->getResultFactory()->create(ResultFactory::TYPE_ERROR);
        }

        // Use the category URL as needed
    }
}

Best Practices and Limitations

  • Always validate that the category exists before trying to load it.
  • Be aware of performance implications; loading all categories can be resource-intensive in large stores. Consider using more specific filters or pagination.
  • For SEO purposes, ensure your category names are unique and lowercase.

For a deeper understanding of Magento’s category system and URL generation process, I recommend checking out the official documentation on Category URLs.

Remember that while Magento provides many built-in APIs, sometimes you need to create custom solutions like this CategoryLoader model to handle specific business logic. The key is to understand the underlying data structures and API endpoints available.

Happy coding!

Catalog Extensions SEO Errors

Our network