How to get backend Url in Magento 2?

It’s quite easy to fetch Base URL for an Admin panel/Backend in Magento 2 using Backend module Url Class.

Back end Url have base store Url plus admin URI name. like {BASE_URL}/admin/

You can get Magento2 backend URL by just below way,

public function __construct(
    \Magento\Backend\Model\Url $backendUrlManager
) {
    $this->backendUrlManager  = $backendUrlManager;
}

public function getBackendUrl()
{
    return $this->backendUrlManager->getUrl('sales/order/view', ['param1' => 'param1']);
}

We have passed Sales/order/view action with a query string as param1 in URL.

Get URL from an adminhtml template or PHP file,
echo $this->getBackendUrl();

How to get product available qty in Magento 2?

Get Product qty or stock information by Product Object is pretty easy in Magento 2.

We can simply get product qty from the product id and get the first product object. We can get product data from either by id or by SKU.

Please check below code snippet for getting product qty in Magento 2,

<?php
namespace Rbj\Training\Block;

class Product extends \Magento\Framework\View\Element\Template
{
    protected $stockItemRepository;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\CatalogInventory\Model\Stock\StockItemRepository $stockItemRepository,
        array $data = []
    ) {
        $this->stockItemRepository = $stockItemRepository;
        parent::__construct($context, $data);
    }

    public function getProductQty($id)
    {
        return $this->stockItemRepository->get($id);
    }
}

Call template file by below code,

$product_id = 1; //product id
$stock = $block->getProductQty($product_id);
echo $stock->getQty(); // product qty
echo $stock->getMinQty();
echo $stock->getMaxQty();
echo $stock->getIsInStock(); // is in stock return as boolean

You can fetch the Product Stock related data using the above Stock object.

How to override Customer form/login.phtml file to custom module in magento 2?

We can override form/login.phtml file by just copy login.phtml file to our theme level easily without any code customization. login.phtml file contains the code of login form of site,They contain email and password field.

For custom module requirement, we need to overwrite login.phtml file in module level so we need to create customer_account_login.xml file.
Create XML file at app/code/Rbj/Customer/view/frontend/layout/customer_account_login.xml

<?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">
    <body>
        <referenceBlock name="customer_form_login">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Rbj_Customer::form/login.phtml</argument>
            </action>
        </referenceBlock>
    </body>
</page>

Now keep our login.phtml file at app/code/Rbj/Customer/view/frontend/templates/form/login.phtml
Keep content from core login.phtml file to our custom file.
Run command,
php bin/magento cache:clean

Open link, {YOUR_SITE_URL}/customer/account/login
You can get your override file changes.