Getting below error while clicking on edit action from Grid.
Fatal error: Uncaught Error: Call to a member function getUrl() on null in /var/www/html/myproject/machpack/app/code/Vendor/Module/Block/Adminhtml/Module/Edit/Button/Generic.php:60 Stack trace: #0 /var/www/html/myproject/machpack/app/code/Vendor/Module/Block/Adminhtml/Module/Edit/Button/Delete.php(38): Vendor\Module\Block\Adminhtml\Module\Edit\Button\Generic->getUrl('//delete', Array) #1 /var/www/html/myproject/machpack/app/code/Vendor/Module/Block/Adminhtml/Module/Edit/Button/Delete.php(28): Vendor\Module\Block\Adminhtml\Module\Edit\Button\Delete->getDeleteUrl() #2 /var/www/html/myproject/magento236/vendor/magento/framework/View/Element/UiComponent/Context.php(285): Vendor\Module\Block\Adminhtml\Module\Edit\Button\Delete->getButtonData() #3 /var/www/html/myproject/machpack/vendor/magento/module-ui/Component/AbstractComponent.php(116): Magento\Framework\View\Element\UiComponent\Context->addButtons(Array, Object(Magento\Ui\Component\Form)) #4 /var/www/html/myproject/mac in /var/www/html/myproject/machpack/app/code/Vendor/Module/Block/Adminhtml/Module/Edit/Button/Generic.php on line 60
Generic.php
<?php
namespace Vendor\Module\Block\Adminhtml\Module\Edit\Button;
use Magento\Search\Controller\RegistryConstants;
/**
 * Class Generic
 */
class Generic
{
 /**
 * Url Builder
 *
 * @var \Magento\Framework\UrlInterface
 */
 protected $urlBuilder;
 /**
 * Registry
 *
 * @var \Magento\Framework\Registry
 */
 protected $registry;
 /**
 * Constructor
 *
 * @param \Magento\Backend\Block\Widget\Context $context
 * @param \Magento\Framework\Registry $registry
 */
 public function __construct(
 \Magento\Backend\Block\Widget\Context $context,
 \Magento\Framework\Registry $registry
 ) {
 $this->urlBuilder = $context->getUrlBuilder();
 $this->registry = $registry;
 }
 /**
 * Return the synonyms group Id.
 *
 * @return int|null
 */
 public function getId()
 {
 $contact = $this->registry->registry('Module');
 return $contact ? $contact->getId() : null;
 }
 /**
 * Generate url by route and parameters
 *
 * @param string $route
 * @param array $params
 * @return string
 */
 public function getUrl($route = '', $params = [])
 {
 return $this->urlBuilder->getUrl($route, $params);
 }
}
- 
 Show code for Generic.phpAbdul Samad Abbasi– Abdul Samad Abbasi2021年01月05日 11:17:57 +00:00Commented Jan 5, 2021 at 11:17
1 Answer 1
Try this in your Generic.php file
<?php
namespace Vendor\Module\Block\Adminhtml\ControllerName\Edit;
use Magento\Backend\Block\Widget\Context;
use Magento\Cms\Api\PageRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
/**
 * Class Generic
 */
class Generic
{
 /**
 * @var Context
 */
 protected $context;
 /**
 * @var PageRepositoryInterface
 */
 protected $pageRepository;
 /**
 * @param Context $context
 */
 public function __construct(
 Context $context,
 PageRepositoryInterface $pageRepository
 ) {
 $this->context = $context;
 $this->pageRepository = $pageRepository;
 }
 /**
 * Return CMS block ID
 *
 * @return int|null
 */
 
 /**
 * Generate url by route and parameters
 *
 * @param string $route
 * @param array $params
 * @return string
 */
 public function getUrl($route = '', $params = [])
 {
 return $this->context->getUrlBuilder()->getUrl($route, $params);
 }
 
}
- 
 your code worked correctly. but i still not get where i was making mistake?quickshiftin– quickshiftin2021年01月05日 11:28:31 +00:00Commented Jan 5, 2021 at 11:28
- 
 ComparegetUrlmethod of bothAbdul Samad Abbasi– Abdul Samad Abbasi2021年01月05日 11:47:01 +00:00Commented Jan 5, 2021 at 11:47
Explore related questions
See similar questions with these tags.