CATATAN: Anda sebaiknya tidak pernah menggunakannya. \Magento\Framework\App\ObjectManager::getInstance()
Ini mengalahkan tujuan injeksi ketergantungan.
untuk mendapatkan hasil perlu menambahkan -> getAbsolutePath (); ke kode terakhir $ mediaPath = $ fileSystem-> getDirectoryRead (\ Magento \ Framework \ App \ Filesystem \ DirectoryList :: ROOT) -> getAbsolutePath ();
Dan Anda dapat menemukan daftar konstanta lengkap di bawah 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';
Anda dapat menggunakan variabel default magento 2 untuk mendapatkan path absolut dari direktori Magento
sumber
Anda harus menggunakan ROOT untuk mendapatkan direktori root
sumber
Saya lebih suka pendekatan Alan Storm .
Dan Anda dapat menemukan daftar konstanta lengkap di bawah
lib/Magento/App/Dir.php
.sumber