ใน Magento 1 Mage::getBaseUrl();แต่ใน Magento 2 ฉันต้องผ่านประเภทออบเจกต์คลาสที่รับผิดชอบในตัวสร้าง
ฉันไม่มีความคิดที่ฉันจะต้องผ่านชั้นเรียน?
ใน Magento 1 Mage::getBaseUrl();แต่ใน Magento 2 ฉันต้องผ่านประเภทออบเจกต์คลาสที่รับผิดชอบในตัวสร้าง
ฉันไม่มีความคิดที่ฉันจะต้องผ่านชั้นเรียน?
คำตอบ:
ในวีโอไอพี 2
หากคุณต้องการได้รับ URL พื้นฐานคุณสามารถลองใช้รหัสด้านล่าง:
/** * @var \Magento\Store\Model\StoreManagerInterface $this->_storeManager */ $this->_storeManager->getStore()->getBaseUrl();
ที่ไหน$this->_storeManagerตัวอย่างของ\Magento\Store\Model\StoreManagerInterface 
รหัสด้านบนนี้จะให้ผลลัพธ์แก่คุณ
http://www.example.com ( หากเปิดใช้งานการเขียน Seo ซ้ำ )
และ http://www.example.com/index.php ( หากไม่ได้เปิดใช้งานการเขียน Seo ซ้ำ )
หากคุณต้องการ URL ฐานโดยไม่ต้อง index.php
$this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB)
ดูรายละเอียดได้ที่ magento2 get base url and media url and static url
การใช้ Object Manager
URL ฐาน:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface'); $storeManager->getStore()->getBaseUrl();
URL ฐานโดยไม่มี index.php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface'); $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);
สำหรับการรับ URL ฐานสื่อ:
$this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
สำหรับการรับลิงค์ URL:
$this->_storeManager->getStore() ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_LINK);
สำหรับการได้รับ   $this->_storeManager  คุณควรโทรฉีด\Magento\Store\Model\StoreManagerInterface $storeManager
ที่   __construct( )ฟังก์ชั่นที่คลาสบล็อก
เหมือนกับ :
public $_storeManager; public function __construct( \Magento\Store\Model\StoreManagerInterface $storeManager, ..... ) { ... $this->_storeManager=$storeManager; }
นอกจากนี้คุณยังจะได้รับURL พื้นฐาน โดยตรงที่ใช้โทรโดยตรงของphtmlobject Manager
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface'); $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);
 Directly call of object manager is not good ideaหมายเหตุ: หากคุณต้องการ URL พื้นฐานที่ phtml จากนั้นฉีดStoreManagerInterfaceที่บล็อก
\Magento\Framework\View\Element\Templateเพียงแค่ใช้คำสั่งนี้กับคุณกำลังใช้คลาสที่ขยาย
$this->getBaseUrl()ถ้าไม่ใช้สิ่งนี้:
$this->_storeManager->getStore()->getBaseUrl()หรือถ้าคุณใช้มันในเทมเพลต PHTML ให้ใช้:
$block->getBaseUrl()\Magento\Framework\View\Element\Templateคุณกำลังสมมติว่าพวกเขาจะอยู่ในระดับที่ขยาย อย่างไรก็ตามคำถามของ OP ไม่ได้อ้างอิงถึงจุดที่เขากำลังเข้ารหัสอยู่ เช่น Model, Helper, Controller ฯลฯ
                    ใน Magneto2: นี่เป็นวิธีรับลิงก์ Url ในไฟล์ PHTML:
echo $this->getUrl('about-us')
ฉันหวังว่ามันจะทำงานให้คุณ
หากคุณต้องการรับ URL จากไดเรกทอรีรูทของ Magento install คุณสามารถใช้ getUrl ได้ มันสืบทอดมาจากคลาส AbstractBlock (Magento \ Framework \ View \ Element \ AbstractBlock) เพื่อให้คุณสามารถใช้บล็อกของคุณได้ นี่คือตัวอย่าง
$this->getUrl('pub/media/video/', ['_secure' => $this->getRequest()->isSecure()]).$fileNameพารามิเตอร์แรกคือเส้นทางที่คุณต้องการและตัวที่สองตั้งค่าตัวเลือก _secure หากผู้ใช้เรียกดูผ่าน https คุณสามารถเพิ่มลงในพา ธ ได้โดยการต่อชื่อไฟล์เฉพาะเข้ากับการเรียก getUrl หรือคุณสามารถเพิ่มลงในพารามิเตอร์แรกได้ พา ธ นั้นสัมพันธ์กับไดเรกทอรีรูทของการติดตั้ง Magento ของคุณ
ฉีดผู้จัดการร้านค้าและรับ URL พื้นฐาน
public $_storeManager;
  public function __construct(
      \Magento\Store\Model\StoreManagerInterface $storeManager,
       .....
    ) {
       ...
  $this->_storeManager=$storeManager;
    }
$this->_storeManager->getStore()->getBaseUrl();
หมายเหตุ: อย่าสร้างตัวจัดการออบเจ็กต์เสมอ
ถ้าเป็น Magento 2.0.0 CE เวอร์ชันที่เสถียรและวัตถุประเภท "บริบท" ใด ๆ ถูกโหลดแล้วในคลาสบล็อกเช่นMagento\Backend\Block\Widget\Contextนั้นเพียงแค่เรียกgetStoreManager()->getStore()->getBaseUrl()ใช้ฟังก์ชันดังต่อไปนี้:
$context->getStoreManager()->getStore()->getBaseUrl()ภายในนวกรรมิกนอกจากนี้คุณสามารถส่งผ่านข้อโต้แย้งเช่น\Magento\Framework\UrlInterface::URL_TYPE_MEDIAภายในgetBaseUrl()ฟังก์ชั่นนี้
หวังว่านี่จะช่วยได้
วิธีที่ถูกต้องในการรับสื่อใน PHTML คือ:
$block->getViewFileUrl('images/myimage.png');เพียง: <?php $block->getBaseUrl() ?>
ในวีโอไอพีรูทของคุณให้สร้างไฟล์ Test.php
use Magento\Framework\App\Bootstrap;
include('app/bootstrap.php');
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface'); 
$baseUrl= $storeManager->getStore()->getBaseUrl();
ในไฟล์คลาสบล็อกของคุณเพิ่มฟังก์ชั่นต่อไปนี้:
public function getImageUrl($link_url = '')
    {
        if(!empty($link_url))
        {
            $media_url = $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
            return $media_url.'/'.$link_url;
        }
        else
        {
            return '#';
        }
    }
และเรียกสิ่งนี้จากไฟล์เทมเพลต. html ของคุณโดยทำดังนี้:
$block->getImageUrl('<relative image path>')คุณสามารถรับ Magento2 Base url ได้โดยใช้สิ่งนี้:
$this->_storeManager->getStore()->getBaseUrl()ในวีโอไอพี 2
หากคุณต้องการได้รับ URL พื้นฐานคุณสามารถใช้รหัสด้านล่าง:
$this->_storeManager->getStore()->getBaseUrl()โดยใช้ objectManager คุณสามารถใช้รหัสต่อไปนี้
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager>get('\Magento\Store\Model\StoreManagerInterface');
$storeManager->getStore()->getBaseUrl();
นี่คือรายละเอียดการสอนที่ฉันพบเพื่อรับ URL พื้นฐานและอื่น ๆ ใน Magento2 http://www.webmull.com/magento-2-getbase-url/
public function getBaseUrl()
{
    return $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
}