Overriding Magento’s Mage_Core

This was a bit fidgety, but its possible to override core Magento files! Actually there are two ways, but one involves copy pasting the class and tweaking. That will break if the class ever changes on a Magento update, so I won’t even tell you how to do that.

Instead, we’ll create our own module. My module’s called Madskull, and I already have a folder in /app/code/local/madskull

In this example, I overrode Mage_Core_Model_Url. I had to have a custom getUrl() method.

I created (in the aforementioned folder) Url/Url.php, which extends Mage_Core_Model_Url :

<?php

/**
 * Class Madskull_Core_Model_Url
 *
 *  @author delboy1978uk
 */
class Madskull_Url_Url extends Mage_Core_Model_Url
{
    /**
     * Do a quick string replace to ditch apostrophes
     * @return string
     */
    public function getUrl($routePath = null, $params = null)
    {
        return str_replace("'",'',parent::getUrl($routePath,$params));
    }
}

Okay, next in the Url directory, create an etc folder, and create config.xml :

<config>
    <modules>
        <Madskull_Url>
            <version>1.0.1</version>
        </Madskull_Url>
    </modules>
    <global>
        <models>
            <core>
                <rewrite>
                    <url>Madskull_Url_Url</url>
                </rewrite>
            </core>
        </models>
    </global>
</config>

Finally, we activate the module by adding it to a new file /app/etc/modules/Madskull_Url_Url.xml :

<?xml version="1.0"?>
<config>
    <modules>
        <Madskull_Url>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Core />
            </depends>
        </Madskull_Url>
    </modules>
</config>

Job done! Now, without creating second copies of the core classes, you can extend away, and updates will not affect your code!

Advertisements

Upgrade your ZF1 classes to PSR-0 Autoload

As a Zend Framework 1 user I loved the simplicity of the class naming convention, being Folder_Subfolder_ClassName. However as you probably know, these class names get really quite long! The latest PHP as you already know uses namespaces and allows for shorter classnames that wont clash with each other. Now I have added an API to my website using the incredible Apigility (http://apigility.org) which was built in Zend Framework 2, I thought it would be nice to upgrade my existing classes to autoload PSR-0 style, so I can eventually migrate easily across.

First thing then, you need composer installed. If you’ve been following my blog, or using any other vendors packages, you’ll already have it in your project.

In ZF1, the library folder was where you would keep your different modules/packages/classes. I have a library called TTB. So in the TTB folder, create an src folder, and another TTB folder in there (this is a quirk of PSR-0, but trust me). In that folder, recreate your classes. Changes aren’t very difficult:

<?php
namespace TTB\Form;
use TTB\Form\Element;

class Contact extends \Zend_Form
{
    //etc
}

The line TTB_Form_Contact extends Zend_Form is shortened by way of the namespace line at the top to just become Contact, and the Zend_Form gets a backslash in front of it as it is in the global namespace. You also specify use  to import any other classes into the namespace. Now we can call Textbox instead of Element\Textbox or TTB\Form\Element\Textbox.

You probably know all this stuff anyway! The point is, to get it autoloading in your project!

So in your index file of your ZF1 project, require once vendor/autoload.php. And in your composer.json, add the following:

"autoload": {
     "psr-0": {
         "TTB" : "application/library/TTB/src/"
     }
 }

Finally, run composer dump-autoload in the terminal from your site root, and this will generate the classmap. You are now ready for PSR-0 compliance! Now you just need to spend all day refactoring! It’ll be worth it when you take your old project to a new framework!  😉

Autoloading custom classes in Codeigniter

Edit: This is old. Install Composer instead, and require the vendor/autoload.php

I’ve been messing around with CodeIgniter recently. It’s not bad at all, and for non enterprise applications or small sites, it’s a good choice! However I wanted to bring across my own classes that I’d already wrote, so what we need to do is write a helper script to set up SPL Autoloading. All my classes were written using the conventions of Zend Framework, so rather than write my own autoload feature, I thought ‘Hmm, I could also use the validators, filters, form elements, and a couple of other awesome non MVC related stuff from ZF. ‘ So here’s how:

Create a file called zend_framework_helper.php

ini_set("include_path", ini_get("include_path").PATH_SEPARATOR.BASEPATH."../application/third_party/");
 require_once 'Zend/Loader/Autoloader.php';
 spl_autoload_register(array('Zend_Loader_Autoloader', 'autoload'));

Copy your Zend Folder to /application/third_party/ . Last thing you need to do is autoload the helper. Open config/autoload.php, find the helpers array, and add ‘zend_framework’ to it.

To get your own ZF_Style_Class to autoload, (my custom classes in this instance are namespaced DD), add the namespace to the Zend Autoloader. You can put this in its own helper or just put it after the spl_autoload_register command in the zf helper.

Zend_Loader_Autoloader::getInstance()->registerNamespace('DD_');

So now in my Codeigniter controllers, I can call my own classes, and that of Zend Framework! Remember to setView(new Zend View()) in your Form objects! Check it out:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Nofun extends MX_Controller {

 public function index()
 {
   $button = new Zend_Form_Element_Button('Frankenstein');
   $form = new Zend_Form('formname'); 
   $form->setView(new Zend_View()); 
   $form->addElement($button); 
   echo 'this rocks!<br />'.$form; 
   $test = new DD_Test(); 
   echo $test->pullLever(); 
 } 
}

Screen Shot 2013-05-23 at 21.12.50

Now we have the power of the non MVC Zend Stuff at out disposal!

If you’re interested in Codeigniter check out the awesome series of videos by David Connelly of DC Radio Network (www.dcradionetwork.com), introduction video below!