In Kohana 3, how can I override/extend a module class?
E.g. I want to add functionality to the Auth module that is specific to my application. In this case I want to extend the abstract Auth class located in the classes folder of the Auth module.
What naming convention should I use for my Auth class and where in the file system do I place my class file?
To solve this issue it’s important to understand the hierarchical nature of the Kohana 3 framework. When it comes to overriding or extending modules you need to do the following.
Let’s extend the Auth module. When you look at the Auth module file system structure you notice that in the
classesdirectory there is a file calledauth.php. When you open this file you see the following:Here an abstract class named
Authis defined which is extending theKohana_Authclass. When you use any references to theAuthclass in your application you’re referring to this abstract class. The actual implementation ofAuthis actually kept in theKohana_Authclass which is located in theKohanafolder which part of the module directory structure.To extend the
Authmodule, i.e. add your own functionality, you simply place anauth.phpfile in theclassesfolder of your application directory. In yourauth.phpfile you extend your version of theAuthmodule by extending theKohana_Authclass. Like so:Because of the hierarchical nature of the framework, the abstract class
Authdefined as part of the module will never be loaded because the framework loads yourAuthclass first because it takes precedence. The class you extend,Kohana_Auth, provides all the auth original functionality you can no extent and/or override.For more information on the behavior checkout this part of the documentation.