简体   繁体   中英

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.

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.

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM