Monday, August 27, 2012

How to make CMS pages available only to logged in customers

Hello
Here is a possible solution on how to make cms mages available only to logged in customers.
Override the Mage_Cms_PageController. Here is a how to do it: http://www.extensionprogrammer.com/skin/frontend/default/dermodpro/pdf/magento-cheatsheet.pdf

Now add the following method in your new controller

public function preDispatch(){
		$restrictedIdentifiers = array('about-magento-demo-store');
		$pageId = $this->getRequest()
            ->getParam('page_id', $this->getRequest()->getParam('id', false));
        $page = Mage::getModel('cms/page')->load($pageId);
    	parent::preDispatch();
    	if (in_array($page->getIdentifier(), $restrictedIdentifiers)){
	    	if (!Mage::getSingleton('customer/session')->authenticate($this)) {
	            $this->setFlag('', 'no-dispatch', true);
	        }
    	}
    }
That's it. Enjoy. I know it's not 100% clean solution. If you want the clean solution, add a new field for the cms_page table (let's call it 'password_protect'), add the field in the admin form. After doing this the code above becomes:
public function preDispatch(){
		$pageId = $this->getRequest()
            ->getParam('page_id', $this->getRequest()->getParam('id', false));
        $page = Mage::getModel('cms/page')->load($pageId);
    	parent::preDispatch();
    	if ($page->getPasswordProtect()){
	    	if (!Mage::getSingleton('customer/session')->authenticate($this)) {
	            $this->setFlag('', 'no-dispatch', true);
	        }
    	}
    }
Marius.

Monday, August 13, 2012

Tips & tricks to speed up Magento

I've found on LikedIn a comment about tips and tricks for speeding up Mangeto.
It seams like a good list to consider when having troubles with the site speed.
The list was provided by Pieter Pabst.
Here it goes:


- Use KeepAlive in Apache.
- Make sure the query buffer on your MySQL instance is large enough.
- Hosting MySQL on another (V)Machine will help
- Migrate .htaccess to you apache config and turn off AllowOverride.
- Use some kind of opcode cashing, like APC of even better, Zend Server
- Use mod_deflate
- Use mod_expires
- Put var/cache in a /dev/ramdisk of tmpfs
- Use the CDN option, or at least something that is accessible via another DNS name (like content.yoursite of static.yoursite) as browsers are limited to X connections to a certain host at one time (use the URLs in System -> Configuration -> Web to accomplish this), but be aware of the fall-back mechanism for skins. Also beware that the upload script in the backend does not like it when it's hosted on another hostname (potential XSS). Your uploads will fail. To overcome this, make sure the backend uses local JS and the frontend uses CDN. To accomplish this use the configuration scope in the Web config section (default to local, storeviews to CDN).
- Move /media to another server
- After putting var/cache in ramdisk, put the rest somewhere with fast I/O. Like SAS disks or a SSD. Don't put it on something like iSCSI or SATA. Put media on cheap I/O like SATA, preferably on another server, or something assebeble via another hostname.
- If you have the option, don't use apache at all. Try ngnix.

Magento:
- Use flat catalog (System -> Configuration -> Catalog -> Frontend)
- Enable cache (System -> Cache management)
- Compile Magento when opcode caching is no option (System -> Tools -> - Compiling)

Hacking
- Disabling the fall-back mechanism for themes / skins and merging the base and your current into one will save you a lot of IO
- Try to code your modules in a way they can use cache
- Try full page caching, all tough you will have to buy a license for products like Zend Server, this will boost performance.
- Try cashing frequently used queries (in a request kind of context I mean) in f.e.a arrays. But beware of the memmory limit. Unset things of necessary.

Webdesign
- Use sprites
- Combine everything in one CSS file or use one of the extensions already mentioned.
- The same goes for javascript
- Use robots.txt to stop robots from crawling certain URLs with producs they probably already indexed trough other URLs. It will just cause additional load.

After you've done all that, you site needs to be immense popular to require load balancing of MySQL clustering.