Reputation: 293
Something that should be trivial in IMHO.
I have a public function in my webapp AppController
- call it getDbConfig()
I have a plugin and I want to reference the getDbConfig() function that was defined in the system-wide AppController.
Obviously $this->getDbConfig()
does not work - since $this refers to the local context - but for the life of me I cannot figure out how to specify the path to public functions in the system AppController
in Plugin AppControllers
Upvotes: 0
Views: 157
Reputation: 3106
You should pass the function to your plugin/component as a configuration parameter, or the results from getDbConfig() (I assume it won't change much).
So:
class MyComponent extends Component
{
private $dbconfig = null;
public function initialize (array $config)
{
parent::initialize ($config);
if (isset ($config['dbconfig']))
{
$this->dbconfig = $config['dbconfig'];
}
}
}
Then, in your controller:
class MyController extends AppController
{
public function initialize()
{
parent::initialize();
$this->loadComponent ('My', ['dbconfig' => $this->getDbConfig()]);
}
}
If you really must reference a function and call it, use variable functions.
Upvotes: 0
Reputation: 25698
but for the life of me I cannot figure out how to specify the path to public functions in the system AppController in Plugin AppControllers
Read about php OOP namespace basics. You need to import and alias the class. And inherit from your apps AppController. This would make the plugin rely on having the App
namespace available, however, at least I have never seen a CakePHP app using another namespace. If it's another one the developer can still fork and use that fork and make the change to it.
Alternatively make the code in question a component or utility class and check in your plugins AppController if that class exists and then instantiate the utility lib it or load the component.
Upvotes: 1
Reputation: 9398
Doesn't make sense IMHO. A plugin is made to live outside you application. Ideally you could make you plugin public so that other developers could use it inside their application.
If you reference a function that you (and only you) have in your AppControll your plugin is no more a plugin.
I think that this is the typical case where you should create a Component. Put the getDbConfig()
inside a Component so that you can call that component both from the Plugin and the AppController
Upvotes: 1