首页 > 编程语言 >magento block 程序中获取各种url及绝对路径的方法

magento block 程序中获取各种url及绝对路径的方法

时间:2023-03-28 22:32:14浏览次数:51  
标签:getBaseDir url 绝对路径 Magento dir directory magento folder Mage


magento block 程序中获取各种url的方法



 



Mage::getBaseUrl('media') //可获得 media 带 http 的url 地址。

//同理也可以获得skin 和js 目录的地址:
Mage::getBaseUrl('skin');
Mage::getBaseUrl('js');

//而获取带http 的网站根地址为:
Mage::getBaseUrl('web');

//在cms中调用的URL的方法(content 中):
{{store direct_url="Home"}}   //跳网id 是Home 的cms页面。
{{skin url='images/media/main_page_banner.jpg'}}  //定位一副图片的位置。

$this->getUrl('catalogsearch/advanced/result');
//在block 可以获得一页页面的URL.

$this->getSkinUrl('/images/logo.jpg')
//可以直接获取图片地址

$this->getSkinUrl('css/style.css');
//$this->getSkinUrl();一看应该就知道得到的是skin下面的路径。

$this->getUrl('contacts');
//要是要获得的是具体页面的url, 如果$this不能用就可以用Mage:: 来调用函数

 

 

//To Retrieve URL path in STATIC BLOCK

//To get SKIN URL
{{skin url='images/sampleimage.jpg '}}

//To get Media URL
{{media url='/sampleimage.jpg'}}

//To get Store URL
{{store url='mypage.html'}}

//To get Base URL
{{base url='yourstore/mypage.html'}}

//TO Retrieve URL path in PHTML
//Note: In editing PHTML don’t forget to enclode the following code With PHP tag

//Not secure Skin URL:
<?php echo $this->getSkinUrl('images/sampleimage.jpg') ?>

//Secure Skin URL
<?php echo $this->getSkinUrl('images/ sampleimage.gif',array('_secure'=>true)) ?>

//Get Current URL
$current_url = Mage::helper('core/url')->getCurrentUrl();

//Get Home URL
$home_url = Mage::helper('core/url')->getHomeUrl();

//Get Magento Media Url
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);

//Get Magento Media Url
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);

//Get Magento Skin Url
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN);

//Get Magento Store Url
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);

//Get Magento Js Url
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_JS);

 



Get Directory paths



Mage::getBaseDir()
 //output : /var/www/html/magento
Mage::getBaseDir('app')
 //output : /var/www/html/magento/app
Mage::getBaseDir('media')
 //output : /var/www/html/magento/media



Same way you can get more directory path,
Mage::getBaseDir(‘design’) => Get design directory path
Mage::getBaseDir(‘code’) => Gives code directory file path
Mage::getBaseDir(‘lib’) => Gives lib directory file path
Mage::getBaseDir(‘skin’) => Gives skin directory file path
Mage::getBaseDir(‘var’) => Gives var directory file path
Mage::getBaseDir(‘cache’) => Gives cache directory file path
Mage::getBaseDir(‘log’) => Gives log directory file path



 



Magento Base Directories

Add the following code an empty controller action or other area of Magento where you can run arbitrary code.

$base_path = Mage::getBaseDir('base');
var_dump($base_path);
 
$etc_path = Mage::getBaseDir('etc');
var_dump($etc_path);

 

You should see output something like the following

string '/Users/username/Sites/magento1point4.1.dev' (length=43)
string '/Users/username/Sites/magento1point4.1.dev/app/etc' (length=51)

 

The first folder is the base directory for the entire Magento installation. The second folder is the base directory for Magento’s etc folder, which holds configuration files.

In the latest Community Edition release of Magento there are 14 different directories that are considered important enough to be retrievable by this method. The static calls to Mage::getBaseDir are a wrapper/public-api to the getDir method on a Mage_Core_Model_Config_Options object. If you take a look at the Magento Model pseudo-constructor in

#File: app/code/core/Mage/Core/Model/Config/Options.php
class Mage_Core_Model_Config_Options extends Varien_Object
{
    /* ... */
    protected function _construct()
    {
        $appRoot= Mage::getRoot();
        $root   = dirname($appRoot);
 
        $this->_data['app_dir']     = $appRoot;
        $this->_data['base_dir']    = $root;
        $this->_data['code_dir']    = $appRoot.DS.'code';
        $this->_data['design_dir']  = $appRoot.DS.'design';
        $this->_data['etc_dir']     = $appRoot.DS.'etc';
        $this->_data['lib_dir']     = $root.DS.'lib';
        $this->_data['locale_dir']  = $appRoot.DS.'locale';
        $this->_data['media_dir']   = $root.DS.'media';
        $this->_data['skin_dir']    = $root.DS.'skin';
        $this->_data['var_dir']     = $this->getVarDir();
        $this->_data['tmp_dir']     = $this->_data['var_dir'].DS.'tmp';
        $this->_data['cache_dir']   = $this->_data['var_dir'].DS.'cache';
        $this->_data['log_dir']     = $this->_data['var_dir'].DS.'log';
        $this->_data['session_dir'] = $this->_data['var_dir'].DS.'session';
        $this->_data['upload_dir']  = $this->_data['media_dir'].DS.'upload';
        $this->_data['export_dir']  = $this->_data['var_dir'].DS.'export';
    }

 

you can see where these directory paths are stored.

Let’s quickly review each one

 

Mage::getBaseDir(‘base’);

This is your main Magento directory. In a default root level application instal, this is the equivalent of the document root.

 

Mage::getBaseDir(‘app’);

This is your Magento application directory. This is the directory where the final class Mage... application file (Mage.php) is stored.

The default directory (from the Magento base folder) is

/app/

 

Mage::getBaseDir(‘code’);

This is your Magento code directory. This is base directory for the three Magento code pools (core, community, local).

The default directory (from the Magento base folder) is

/app/code

 

Mage::getBaseDir(‘design’);

This is your Magento design package directory. This is the base folder that contains the “design packages” for each Area of Magento, (frontend, adminhtml, and install)

The default directory (from the Magento base folder) is

/app/design

 

Mage::getBaseDir(‘etc’);

The etc folder is where Magento stores system level (as opposed to module level) configuration files. The name etc is borrowed from the *nix family of operating systems, and Magento’s configuration files are all XML based.

The default directory (from the Magento base folder) is

/app/etc

 

Mage::getBaseDir(‘lib’);

Magento’s library folder is where non-module based Magento code lives. This include a large amount of the system code which allows Magento to run, as well as a number of third party libraries (including the Zend Framework). The library is also the last code pool Magento will search when attempting to autoload a file.

The default directory (from the Magento base folder) is

/lib

 

Mage::getBaseDir(‘locale’);

The locale folder contains the translation files for the core Magento modules. Magento uses a system similar to GNU gettext to provide translated text. Unique strings are stored as key value pairs in the translation files. They “key” is used to lookup which text should actually be displayed. This means for the english locale you’ll see redundant looking strings like

"Add Option","Add Option"

It’s only when you look at the non-english speaking translation files that the system become obvious

The default directory (from the Magento base folder) is

/app/locale

 

Mage::getBaseDir(‘media’);

Magento’s media folder is where media files (images, movies, etc.) related to data (products) is stored.

The default directory (from the Magento base folder) is

/media

 

Mage::getBaseDir(‘skin’);

If the word module is the most used yet least well defined programming term, then the words skin and theme are the most abused in the designer’s world. In Magento, the skin folder contains images, CSS and Javascript files used by your themes.

This is not the only folder where you’ll find images, CSS or javascript though. This folder is meant for files that are customized per theme.

The default directory (from the Magento base folder) is

/skin

 

Mage::getBaseDir(‘var’);

The var folder is another one borrowed from the *nix world. The var stands for Variable files, and is intended to store files which are expected to change during normal system operations.

The default directory (from the Magento base folder) is

/var

 

Mage::getBaseDir(‘tmp’);

The tmp dir is a temporary directory for safely outputting files into for immediate processing. The operating assumption of the tmp folder is that any developer can write to it and expect their file to stay around for a few minutes, without any expectation that it will be there tomorrow.

The default directory (from the Magento base folder) is

/var/tmp

 

Mage::getBaseDir(‘cache’);

Magento, rather famously, makes heavy use of caching for activities that might bog down the system if they had to be performed every-time a page loads. For example, Layout XML files are merged once, and then the tree is cached so they don’t need to be merged again. The cache folder is one place where Magento will store these cached results.

The default directory (from the Magento base folder) is

/var/cache

 

Mage::getBaseDir(‘log’);

Magento’s log folder is where is stores the system and exception logs. These logs can be turned on from the Admin Panel’s

System -> Configuration -> Developer -> Log Settings

section. The apache/web-server user will need write permission on this folder and the files therein.

The default directory (from the Magento base folder) is

/var/log

 

Mage::getBaseDir(‘session’);

During installation you have the option of storing user sessions on disk, or in the database. The session folder is where the user sessions are written out to and read from if you choose to store them in the filesystem

The default directory (from the Magento base folder) is

/var/session

 

Mage::getBaseDir(‘upload’);

There are a number of Admin Panel features which allow you to upload media files (default logos, etc.). The upload folder is where Magento stores these files.

The default directory (from the Magento base folder) is

/media/upload

 

Mage::getBaseDir(‘export’);

The export folder is where Magento will write out files meant to be viewed and used by system owners. For example, the Data Flow section of the Admin uses this folder to write out its export files.

The default directory (from the Magento base folder) is

/var/export

Module Base Directories

So, that covers the system folders that Magento gives you access to. There is, however, one other important static method on the Mage application class class that we should discuss. Give the following a try

var_dump( Mage::getModuleDir('', 'Mage_Core') );
var_dump( Mage::getModuleDir('etc', 'Mage_Core') );
var_dump( Mage::getModuleDir('controllers', 'Mage_Core') );
var_dump( Mage::getModuleDir('sql', 'Mage_Core') );
var_dump( Mage::getModuleDir('locale', 'Mage_Core') );

 

That static method Mage::getModuleDir method allows you to retrieve directory path information that’s specific to any module loaded into the system. Like the getBaseDir method, this call is a public-api to code deeper in the Magento system

#File: app/code/core/Mage/Core/Model/Config.php
class Mage_Core_Model_Config extends Mage_Core_Model_Config_Base
{
    /* ... */
    public function getModuleDir($type, $moduleName)
    {
        $codePool = (string)$this->getModuleConfig($moduleName)->codePool;
        $dir = $this->getOptions()->getCodeDir().DS.$codePool.DS.uc_words($moduleName, DS);
 
        switch ($type) {
            case 'etc':
                $dir .= DS.'etc';
                break;
 
            case 'controllers':
                $dir .= DS.'controllers';
                break;
 
            case 'sql':
                $dir .= DS.'sql';
                break;
 
            case 'locale':
                $dir .= DS.'locale';
                break;
        }
 
        $dir = str_replace('/', DS, $dir);
        return $dir;
    }

 

Let’s quickly cover these four folders.

 

Mage::getModuleDir(‘etc’, ‘Packagename_Modulename’);

Your module’s etc folder. The etc folder is a concept borrowed from the *nix, and contains you’re module’s XML configuration files.

 

Mage::getModuleDir(‘controllers’, ‘Packagename_Modulename’);

Your module’s controllers folder. Magento’s controllers are not included in the __autoload implementation. If you ever need to manually include or require a controller, use this method to ensure you get the right path.

 

Mage::getModuleDir(‘sql’, ‘Packagename_Modulename’);

Your module’s sql folder. The sql folder contains installer and upgrade files for your Model resources, which are used to install entities and migrate database information during installations.

 

Mage::getModuleDir(‘locale’, ‘Packagename_Modulename’);

Your module’s locale folder. The locale folder contains translation files that are used by Magento when your module is viewed under a different locales.

 


 

 

 

标签:getBaseDir,url,绝对路径,Magento,dir,directory,magento,folder,Mage
From: https://blog.51cto.com/u_8895844/6155514

相关文章