GaryDevenay
GaryDevenay

Reputation: 2415

Creating a re-usable controller component in Lithium

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.

Upvotes: 1

Views: 465

Answers (3)

Housni
Housni

Reputation: 983

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 {

Upvotes: 2

Tomen
Tomen

Reputation: 4854

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"

Upvotes: 0

Marius
Marius

Reputation: 185

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.

Upvotes: 2

Related Questions