All information about magento,tips,extensions and many things about magento.step by step process for beginners
Tuesday, October 11, 2011
Friday, September 16, 2011
Magento Development Environment
Turn on Logging
Mage::Log($variable);
$variable
to a log. Arrays and Objects are auto-expanded and pretty printed, allowing you quickly zoom in on the portion of the data structure you're interested in. I've found using this in combination with Mac OS X's Console.app to be a much smoother experience than dumping objects to the browser, particularly given how often Magento objects store references to a good portion of the system (resulting is large object data dumps).- In the Magento Admin, go to System->Configuration
- In the left column, click on Developer (under Advanced)
- If it's not expanded, Click on Log Settings
- Select Yes from the "Enabled" drop down
- Click on Save Config
var/log/system.log var/log/exception.log
system.log
andexception.log
. Make sure Apache has write access to both the directory and the files. Check with your system administrator on the best way to do this.Mage::Log
will be dropped in system.log. Exceptions will be logged to exception.logTurn on Developer Mode
#Mage::setIsDeveloperMode(true); #ini_set('display_errors', 1);
<pre>
formatting.Logging Magento’s Controller Dispatch
A Few Caveats
- These files are not a true Magento Module. While powerful, creating something like this with Magento's override system would be an order of magnitude harder
- Because of the above, I can't make any promises about using this with a anything but Magento version 1.3.2.3
- Standard caveats about production servers apply. Use at your own risk, no warranties, etc.
Installing
1.3.2.3/Mage
) and place it inapp/code/local
local/Mage/Core/Controller/Varien/Action.php local/Mage/Core/Controller/Varien/Front.php local/Mage/Core/Controller/Varien/Router/Standard.php local/Mage/Core/Model/App.php
2009-09-20T16:55:48+00:00 DEBUG (7): Matching router [standard] = Mage_Core_Controller_Varien_Router_Standard vs. request 2009-09-20T16:55:48+00:00 DEBUG (7): Module: helloworld 2009-09-20T16:55:48+00:00 DEBUG (7): Found Modules: Array ( [0] => Alanstormdotcom_Helloworld ) 2009-09-20T16:55:48+00:00 DEBUG (7): Setting Route Name [helloworld] on request 2009-09-20T16:55:48+00:00 DEBUG (7): Passing request into controller Alanstormdotcom_Helloworld_IndexController 2009-09-20T16:55:48+00:00 DEBUG (7): Setting Module Name to: helloworld at Mage_Core_Controller_Varien_Router_Standard::match(248) 2009-09-20T16:55:48+00:00 DEBUG (7): Setting Controller Name to: index at Mage_Core_Controller_Varien_Router_Standard::match(250) 2009-09-20T16:55:48+00:00 DEBUG (7): Setting Action Name to: index at Mage_Core_Controller_Varien_Router_Standard::match(252) 2009-09-20T16:55:48+00:00 DEBUG (7): Calling dispatch method on Alanstormdotcom_Helloworld_IndexController 2009-09-20T16:55:48+00:00 DEBUG (7): Action Controller: Alanstormdotcom_Helloworld_IndexController dispatching with action [index]
Magento’s Class Instantiation Abstraction and Autoload
$customer_group = new Mage_Customer_Model_Group();
Mage_Customer_Model_Group
class. However, if you were to search the Magento codebase forMage_Customer_Model_Group
, you'd never find a single expression that looks anything like the one above. You would, however, see a lot code that looks something like this$customer_group = Mage::getModel('customer/address');
PHP Autoload
include('/path/to/file.php');
function __autoload($class_name){ require('lib/'.strToLower($class_name).'.class.php'); }
lib/customer.class.php lib/email.class.php
Magento Autoload
Mage_Customer_Model_Group Namespace_ModuleName_ClassType_Name
- Namespace
- A classes's namespace lets you know who's responsible for creation and maintain of the class, and helps prevent name collisions between modules. All Magento core modules use the Mage namespace, and the recommended convention is to use a version of your company name for your own modules.
- ModuleName
- All customization of Magento is done through modules, which are a collection of source code and config files that can be loaded into the Magento system. In the example above, the ModuleName is Customer.
- Class Type
- Without getting into too much detail, there are several broad categories of classes in Magento, including Model, Singleton, Helper, and Block.
- Name
- Finally, each class should have a unique name that describes its intended use or function. In the above example, this is Group.
clearing the Magento Cache
(System->Cache Management) which works well, unless you happen to get a bad configuration cached that crashes the system or renders the admin inaccessible.
rm -rf var/cache/mage-*
Magento Configuration Lint
The System you Have
What's a Lint?
lint
program did was scan C code for things that weren't technically wrong (they'd compile), but were likely not what the original programmer had in mind. The combination of C's terse semantics and raw power led to countless situations where a simple typo or failure to think through a function could lead to subtle memory errors that took days to track down. The lint
program scans your code and looks for common errors that still compile. This is sometimes called static analysis.for
, etc.) that can lead to subtle and hard to track down bugs. JSLint calls these out right away. Again, JSLint will call out code that is valid and will run, but it's code that may lead to confusing bugs or behavior.How Configlint Works
Running The Configlint
http://example.com/configlint/
IndexController
, which contains all the code you need to run your Lint Cases.Generated Magento Model Code
function someAction() { header('Content-Type: text/plain'); $coupon = Mage::getModel('salesrule/rule')->load(1); function outputValue($value) { switch(gettype($value)) { case 'string': echo "'".str_replace("'","\\'",$value)."'"; break; case 'array': echo "array("; foreach($value as $v) { outputValue($v); echo ","; } echo ")"; break; case 'NULL': echo 'NULL'; break; default: echo "'can\'t handle ".gettype($value)."'"; } } echo '$model'; foreach($coupon->getData() as $key=>$value) { echo '->set'; echo str_replace(' ', '',ucwords(str_replace('_', ' ', $key))); echo '('; outputValue($value); echo ')'; echo "\n"; } exit; }
salesrule/rule
Model (assuming you have a Model with an id of 1)$model->setRuleId('1') ->setName('test') ->setDescription('') ->setFromDate('2010-05-09') ->setToDate(NULL) ->setCouponCode('') ->setUsesPerCoupon('0') ->setUsesPerCustomer('0') ->setCustomerGroupIds(array('1',)) ->setIsActive('1') ->setConditionsSerialized('a:6:{s:4:"type";s:32:"salesrule/rule_condition_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";}') ->setActionsSerialized('a:6:{s:4:"type";s:40:"salesrule/rule_condition_product_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";}') ->setStopRulesProcessing('0') ->setIsAdvanced('1') ->setProductIds('') ->setSortOrder('0') ->setSimpleAction('by_percent') ->setDiscountAmount('10.0000') ->setDiscountQty(NULL) ->setDiscountStep('0') ->setSimpleFreeShipping('0') ->setApplyToShipping('0') ->setTimesUsed('0') ->setIsRss('1') ->setWebsiteIds(array('1',))
Magento Connect Role Directories
System -> Magento Connect -> Package Extensions
Magento Base Directories
As platform, PHP has a long history of relying on the base computer's underlying file system as a storage mechanism for various application and system functions. This ties in closely with PHP's practical history of closely modeling the nacent web's file serving architecture for it's own.
In an ideal world, as an end-user or application developer using the Magento system, you wouldn't need to worry about the file system at all. When you're handling user sessions, you don't care if they're stored on disk or in a database, or when you declare a Model class you don't need to worry about require
ing the source file into the system. That's because there's an abstraction that sits on top these systems and allows you to not worry about how they're implemented.
Unfortunately, we don't live in an ideal world. There are times when, either out of deadline pressure or lack of an interface, we need to programmatically deal directly with the raw file system in Magento. This article will teach you how to programmatically find the paths of important Magento systems files, which will allow you to build less fragile applications that can withstand system configuration and installation changes.
Finding The Path
If you're worked much with PHP, you know that finding the full file path to any particular file can be a bit of a black art. When you include
or require
a PHP file, or open a raw file with functions like file
orfile_get_contents
using a relative file path, there's a set of rules that PHP will use to determine which directories need to be searched.
Because this list of directories (called the include_path
is malleable at runtime, if two files with the same name exist in the include path, it's often hard to tell which one is loaded. Consider the following
1 2 3 4 5 | include ( 'foo.php' ); #files system has # /lib/foo.php # / use /web/phplib/foo.php #which one loads? |
Because of this, it's a common pattern in the PHP to use the magic constants __FILE__
or __DIR__
to find the absolute path to the calling file, and then navigate to the file you want relative to that calling file
1 2 | $info = file_get_contents (dirname( __FILE__ ) . '/../data/info.json' ); $info = json_decode( $info ); |
This is far from ideal.
First, there's the problem of symlinks. Depending on your web server system, it's not always clear if __FILE__
is returning the file's real path, or its path in the context of a symlink. Because of this, you'll often see the use of the realpath
function
1 2 | $info = file_get_contents ( realpath (dirname( __FILE__ )) . '../data/info.json' ); $info = json_decode( $info ); |
While this works, it's not without its problems. As you can see, this will often lead to code that's a little hard to read. Additionally, calls to realpath
mean that PHP needs to stat the filesystem to retrieve that information. As traffic to your application grows, these kind of file stats can start to create a performance bottleneck.
Finally, although it solves the problem of ambiguity in PHP's include path, this isn't a 100% portable solution. If we move the code from one PHP file (say, a Model in a module) to another PHP file (say, a Magento phtml template), the number of ..
's we'll need is different, since one file is deeper in the hierarchy than the other.
Sometimes __FILE__
is the only way to get where you want to go. Fortunately, Magento offers another option for commonly accessed folders.
Magento Base Directories
Add the following code an empty controller action or other area of Magento where you can run arbitrary code.
1 2 3 4 5 | $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
1 2 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #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 nameetc
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 themeare 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.). Theupload
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
1 2 3 4 5 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #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.
Wrapup
In this article we explored using the Magento provided methods for retrieving important system file paths. While not necessary, using these methods will help ensure that your Magento solutions and custom modules will be able to weather system updates and help minimize the system administration effort when upgrading the platform.