Question

I am currently developing a Lithium application and have come across a function that I have written that I would like to use across multiple Controllers.

I obviously don't want to have the function in each controller. What is the standard way of creating a re-usable component in Lithium?

The lack of search facility on their documentation is making it difficult to find any specifics.

Was it helpful?

Solution

You could try extending controller. Extending controllers is not that bad according to core devs. If that is not and option you could extract your code into a plugin, still some code in controller though.

OTHER TIPS

All you have to do is create a extensions/action/Controller.php and have your controllers extend that.

In your extensions/action/Controller.php

<?php
namespace app\extensions\action;

class Controller extends \lithium\action\Controller {

    protected function _init() {
        parent::_init();

        //add your functionality here
    }
}

?>

And then, your controller has to extend the above mentioned base controller: class MyController extends \app\extensions\action\Controller {

I think this is not a Lithium-specific thing. You could either inherit from the Controller and make your own base controller, but you can also create arbitrary classes that hold your functionality. Don't let a framework inhibit you =)

Regarding the documentation: I usually google in the sense of "<keywords> site:lithify.me"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top