1

I am creating a module in Magento 2 where I have created a model and now I'm implmenting a simple CRUD functionality.

I have shown a list of items

Item name   edit
1. item1    link to edit form
2. item2    link to edit form
3. item3    link to edit form

I want to place this <a href="{{ link to edit form with id}}" />, but I don't know how to give route names in Magento 2 . Can anyone help?

Below is my code:

Controller File

<?php

namespace Smartshore\Subscription\Controller\Index;

use \Magento\Framework\App\Action\Action;

class Index extends Action
{   
    /** @var  \Magento\Framework\View\Result\Page */
    protected $resultPageFactory;

    /**
     * @param \Magento\Framework\App\Action\Context $context
     */
    public function __construct(\Magento\Framework\App\Action\Context $context,
                                \Magento\Framework\View\Result\PageFactory $resultPageFactory)
    {
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }

    /**
     * Subscription Index, shows a list of subscriptions
     *
     * @return \Magento\Framework\View\Result\PageFactory
     */
    public function execute()
    {
        return $this->resultPageFactory->create();
    }
}

Block File

<?php

namespace Smartshore\Subscription\Block;

use Smartshore\Subscription\Model\Api\Data\SubscriptionInterface;
use Smartshore\Subscription\Model\ResourceModel\Subscription\Collection as SubscriptionCollection;

class SubscriptionList extends \Magento\Framework\View\Element\Template implements
    \Magento\Framework\DataObject\IdentityInterface
{
    /**
     * @var \Smartshore\Subscription\Model\ResourceModel\Subscription\CollectionFactory
     */
    protected $_subscriptionCollectionFactory;

    /**
     * Construct
     *
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Smartshore\Subscription\Model\ResourceModel\Subscription\CollectionFactory $subscriptionCollectionFactory,
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Smartshore\Subscription\Model\ResourceModel\Subscription\CollectionFactory $subscriptionCollectionFactory,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->_subscriptionCollectionFactory = $subscriptionCollectionFactory;
    }

    /**
     * @return \Ashsmith\Blog\Model\ResourceModel\Post\Collection
     */
    public function getSubscriptions()
    {
        // Check if subscriptions has already been defined
        // makes our block nice and re-usable! We could
        // pass the 'subscriptions' data to this block, with a collection
        // that has been filtered differently!
        if (!$this->hasData('subscriptions')) {

            $subscriptions = $this->_subscriptionCollectionFactory
                ->create()
                ->addOrder(
                    SubscriptionInterface::CREATION_TIME,
                    SubscriptionCollection::SORT_ORDER_DESC
                );

            $this->setData('subscriptions', $subscriptions);
        }

        return $this->getData('subscriptions');
    }

    /**
     * Return identifiers for produced content
     *
     * @return array
     */
    public function getIdentities()
    {
        return [\Smartshore\Subscription\Model\Subscription::CACHE_TAG . '_' . 'list'];
    }

}

This is the layout

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column"
    xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <title>Subscriptions</title>
    </head>
    <body>
        <referenceContainer name="content">
            <block class="Smartshore\Subscription\Block\SubscriptionList" name="subscription.list" template="Smartshore_Subscription::list.phtml" />
        </referenceContainer>
    </body>
</page>

This is the template

<?php /** @var $block \Smartshore\Subscription\Block\SubscriptionList */ ?>

<h1><?php echo __('Latest Subscriptions') ?></h1>

<ul class="subscription-list">

<?php foreach ($block->getSubscriptions() as $singleSubscription): ?>
    <li class="subscription-list-item">

        <div class="subscription-item-content">
            <?php echo $singleSubscription->getPlan(); ?>
        </div>

        <div class="blog-post-item-meta">
            <strong><?php echo __('Created at:') ?></strong> <?php echo $singleSubscription->getCreationTime() ?>
        </div>
    </li>
<?php endforeach; ?>

</ul>

Controller Form.php

<?php

namespace Smartshore\Subscription\Controller\Index;


use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;

class Form extends Action
{
    protected $resultPageFactory;

    public function __construct(Context $context, PageFactory $pageFactory)
    {
        $this->resultPageFactory = $pageFactory;
        parent::__construct($context);
    }

    public function execute()
    {
        $resultPage = $this->resultPageFactory->create();

        return $resultPage;
    }
}
2
  • 1
    can you show the code your have for your items grid?
    – Marius
    Dec 7, 2016 at 12:50
  • I've added files in the question @Marius
    – anujeet
    Dec 7, 2016 at 12:57

1 Answer 1

1

you can add a method in the class \Smartshore\Subscription\Block\SubscriptionList that looks like this:

public function getSubscriptionEditUrl($subscription)
{
    return $this->_urlBuilder->getUrl('[module_url_key_here]/index/form', [id => $subscription->getId()])
}

then just add in your template the url like this:

<a href="<?php echo $block->getSubscriptionEditUrl($singleSubscription)?>"><?php echo __('Edit')?></a>

But I want to say that you are doing it wrong. For CRUD modules you should use ui-components to generate the grid. Check how the core modules are done. For example the cms pages section.

5
  • Can you please refer a link where I can see how to use ui-components. I am a newbie in Magento
    – anujeet
    Dec 7, 2016 at 13:04
  • Can we create urls' like this /subscription/**id**/edit in Magento 2?
    – anujeet
    Dec 7, 2016 at 13:11
  • theoretically yes, but it's too much of a hassle to do so. The standard urls look like {module}/{controller}/{action}/{param1_name}/{param1_value}.
    – Marius
    Dec 7, 2016 at 13:13
  • Thanks, Please see my question, I've added a file Form.php in Controller, I want to use same form for add/edit but in case of edit if I have "id" in url, I want the form fields to be filled. can you give me any idea related to that?
    – anujeet
    Dec 7, 2016 at 13:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.