Wie bekomme ich den root-Verzeichnispfad in einem phtml-Magento 2?


16
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');

$mediaPath  =   $fileSystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)->getAbsolutePath();

Es gibt den Medienpfad zurück. Wie bekomme ich den Root-Pfad eines Projekts in einer HTML-Seite?

Antworten:


38

Class \ Magento \ Framework \ Filesystem \ DirectoryList wird verwendet, um Pfade wie root, media, var etc. Abzurufen

Dadurch wird das Stammverzeichnis Ihres Projekts wie folgt abgerufen

/ var / www / html / myproject

Mit dem ObjectManager

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$directory = $objectManager->get('\Magento\Framework\Filesystem\DirectoryList');

echo $rootPath  =  $directory->getRoot();

Durch Abhängigkeitsinjektion

protected $_dir;
...

public function __construct(
    ...
    \Magento\Framework\Filesystem\DirectoryList $dir,
    ...        
) {
    ...
    $this->_dir = $dir;
    ...
}

Holen Sie sich andere Verzeichnispfade wie

$this->_dir->getRoot(); // Output: /var/www/html/myproject

$this->_dir->getPath('media'); // Output: /var/www/html/myproject/pub/media

$this->_dir->getPath('pub'); // Output: /var/www/html/myproject/pub

$this->_dir->getPath('static'); // Output: /var/www/html/myproject/pub/static

$this->_dir->getPath('var'); // Output: /var/www/html/myproject/var

$this->_dir->getPath('app'); // Output: /var/www/html/myproject/app

$this->_dir->getPath('etc'); // Output: /var/www/html/myproject/app/etc

$this->_dir->getPath('lib_internal'); // Output: /var/www/html/myproject/lib/internal

$this->_dir->getPath('lib_web'); // Output: /var/www/html/myproject/lib/web

$this->_dir->getPath('tmp'); // Output: /var/www/html/myproject/var/tmp

$this->_dir->getPath('cache'); // Output: /var/www/html/myproject/var/cache

$this->_dir->getPath('log'); // Output: /var/www/html/myproject/var/log

$this->_dir->getPath('session'); // Output: /var/www/html/myproject/var/session

$this->_dir->getPath('setup'); // Output: /var/www/html/myproject/setup/src

$this->_dir->getPath('di'); // Output: /var/www/html/myproject/var/di

$this->_dir->getPath('upload'); // Output: /var/www/html/myproject/pub/media/upload

$this->_dir->getPath('generation'); // Output: /var/www/html/myproject/var/generation

$this->_dir->getPath('view_preprocessed'); // Output: /var/www/html/myproject/var/view_preprocessed

$this->_dir->getPath('composer_home'); // Output: /var/www/html/myproject/var/composer_home

$this->_dir->getPath('html'); // Output: /var/www/html/myproject/var/view_preprocessed/html

HINWEIS: Sie sollten \Magento\Framework\App\ObjectManager::getInstance() It niemals verwenden, um den Zweck der Abhängigkeitsinjektion zu umgehen.


Fabrik Methode funktioniert nicht ...
Sarfaraj Sipai

13

Sie können die Standardvariable magento 2 verwenden, um den absoluten Pfad des Magento-Verzeichnisses abzurufen

echo BP;

1

Sie müssen ROOT verwenden, um das Stammverzeichnis zu erhalten

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$fileSystem = $objectManager->get('Magento\Framework\Filesystem');
$mediaPath  =   $fileSystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::ROOT)->getAbsolutePat‌​h()

Bitte lassen Sie mich wissen, wenn Sie ein Problem haben
Rakesh Jesadiya

Um das Ergebnis zu erhalten, muss -> getAbsolutePath () hinzugefügt werden. zum letzten Code $ mediaPath = $ fileSystem-> getDirectoryRead (\ Magento \ Framework \ App \ Filesystem \ DirectoryList :: ROOT) -> getAbsolutePath ();
Rita Jose

0

Ich bevorzuge Alan Storms Herangehensweise .

$object_manager = MagentoCoreModelObjectManager::getInstance();
$dir = $object_manager->get('MagentoAppDir');            
$base = $dir->getDir();
$media = $dir->getUrl(MagentoAppDir::MEDIA);

Eine vollständige Liste der Konstanten finden Sie unter lib/Magento/App/Dir.php.

#File: lib/Magento/App/Dir.php
/**
 * Code base root
 */
const ROOT = 'base';

/**
 * Most of entire application
 */
const APP = 'app';

/**
 * Modules
 */
const MODULES = 'code';

/**
 * Themes
 */
const THEMES = 'design';

/**
 * Initial configuration of the application
 */
const CONFIG = 'etc';

/**
 * Libraries or third-party components
 */
const LIB = 'lib';

/**
 * Files with translation of system labels and messages from en_US to other languages
 */
const LOCALE = 'i18n';

/**
 * Directory within document root of a web-server to access static view files publicly
 */
const PUB = 'pub';

/**
 * Libraries/components that need to be accessible publicly through web-server (such as various DHTML components)
 */
const PUB_LIB = 'pub_lib';

/**
 * Storage of files entered or generated by the end-user
 */
const MEDIA = 'media';

/**
 * Storage of static view files that are needed on HTML-pages, emails or similar content
 */
const STATIC_VIEW = 'static';

/**
 * Public view files, stored to avoid repetitive run-time calculation, and can be re-generated any time
 */
const PUB_VIEW_CACHE = 'view_cache';

/**
 * Various files generated by the system in runtime
 */
const VAR_DIR = 'var';

/**
 * Temporary files
 */
const TMP = 'tmp';

/**
 * File system caching directory (if file system caching is used)
 */
const CACHE = 'cache';

/**
 * Logs of system messages and errors
 */
const LOG = 'log';

/**
 * File system session directory (if file system session storage is used)
 */
const SESSION = 'session';

/**
 * Dependency injection related file directory
 *
 */
const DI = 'di';

/**
 * Relative directory key for generated code
 */
const GENERATION = 'generation';

/**
 * Temporary directory for uploading files by end-user
 */
const UPLOAD = 'upload';
Durch die Nutzung unserer Website bestätigen Sie, dass Sie unsere Cookie-Richtlinie und Datenschutzrichtlinie gelesen und verstanden haben.
Licensed under cc by-sa 3.0 with attribution required.