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;
}
}