How to use correctly a CakePHP plugin - cakephp

I've developed a bug tracker using CakePHP 2.4.4. I made this as a standalone cakephp app but now I wanna transfer it to a plugin, in order to reuse it in other projects.
As I already read at the docs (http://book.cakephp.org/2.0/en/plugins.html), I have followed the instructions from there and created the correct folder and files structure. This is what i've done so far: https://github.com/lubbleup/BugCake/tree/plugin
But now,when i try to use the plugin in a separate cakephp installation, I cannot understand how to make of the plugin and, for example, use it's controllers and functionality etc.
can anyone help me out here?
ps:this is my first time trying to create a cakephp plugin
Thank you in advance!

You have to load the plugins your parent app in APP/Config/bootstrap.php
CakePlugin::loadAll();
You do not need AppModel or AppController in your plugin. Your plugin has an own AppController/-Model named PluginNameAppController/PluginNameAppModel.
You can call your plugin at http://host/plugin_name/controller/action/[...]. In your case http://host/bug_cake/issues/view/1 for instance.
But you can also use custom routes in your plugin with plenty of options.
Hope that answers your question—if not, comment.

Related

CakePHP 3.x: how to implement event listener in plugin / plugins src-directory doesn't get loaded

In order to build an extendable blogging system with CakePHP I’d like to realize something like extension hooks in Wordpress. My plan is to have a base SQL to load the articles in the main application. This SQL should be possible to extend by a variable set of plugins (one plugin for each special article type).
I already know that CakePHP’s way to realize something like that is the events system. I have some trouble registering listeners residing in my plugins, though.
The official documentation (3.x Book; http://book.cakephp.org/3.0/en/core-libraries/events.html#registering-listeners) says, that a listener can be registered this way:
// Attach the UserStatistic object to the Order's event manager
$statistics = new UserStatistic();
$this->Orders->eventManager()->on($statistics);
Unfortunately it doesn’t tell where to place this code in order to register the listener automatically while bootstrapping. Placing the following lines in my plugin’s bootstrap.php like so:
$tester = new Lib\Tester();
$this->Orders->eventManager()->on($tester);
… (having a class Tester in plugins/MyPlugin/src/Lib of course) results in errors.
In the first line the listener class cannot be found (“Error: Class 'Lib\Tester' not found”). And in the second line $this must not be used in the context of bootstrap.php (“Error: Using $this when not in object context”).
In an old thread (Where to register event listeners) someone else asked for the same thing and got a pretty detailed answer. It was for CakePHP 2.x, though, and doesn’t work with CakePHP 3.x anymore. For example, the function App::uses doesn’t exist in CakePHP 3.x and I couldn’t find a replacement.
So: can someone please help me find the right way to set up a working listener within a plugin in CakePHP 3.x?
[UPDATE #1]
I've found a workaround that works for me (but is quite ugly). In the bootstrap.php of the plugin I was able to instantiate an object of my event listener class like this:
require __DIR__ . '/../src/Lib/Tester.php';
$tester = new MyPlugin\Lib\Tester();
After that I could register the listener on the Articles model (also in bootstrap.php of course):
Cake\ORM\TableRegistry::get('Articles')->eventManager()->on($tester);
That way I'm now able to build up a base request on the articles table and send it to several plugins. Then the plugins can enrich the request to join special tables or something like that.
So, one question remains: how can I avoid the use of the __DIR__ in bootstrap.php? How can the listener be instantiated properly?
[UPDATE #2]
It seems as if the complete src subdirectory of my plugin isn't part of the import path. None of the classes I put there gets loaded. That means that CakePHP doesn't find a simple Controller when I place one there.
Am I doing something wrong in general? Or might that be a bug in CakePHP?
Unfortunately I neither can find plugins for CakePHP 3.x whose source code could help me understand what I might do wrong.
... as ndm assumed in a comment above, there was something wrong with my manually inserted parts in the composer.json file. After fixing that and running dumpautoload again all classes get loaded, now.

use croogo as plugin in main cakephp app

Im working with cakephp for few months and recently I came across croogo, a cakephp cms system. I've tried it and I should say its an awesome system.
Is it possible to use it as a plugin in my main site. I want to use it just for the admin part and rest of my application unattached to it. Ive tried loading its bootstrap file from my main app and also by linking routes to it. I always get errors.
Can someone have any idea if croogo is meant to be used like a plugin or does it have to be used seperately?
"Beginning version 1.6.x, Croogo has been updated to be installed as a vendor package"
=> So yes, it is possible to use it in your code - just not as plugin, but vendor.
See
https://github.com/croogo/croogo/tree/3.0#installation-using-git

import Only CakePHP Html Helpers in Custom MVC project

I have an unusual task at hand, I just need to import a specific portion of CakePHP framework in my custom MVC project, i.e HTML helpers.
Can anyone tell me what code libraries from CakePHP do I need to transfer into my project , to do this.
Please note I need only HTML helpers from the CakePHP framework.
Thanks.
Just go to the CakePHP Github repository Helper directory and download them: https://github.com/cakephp/cakephp/tree/master/lib/Cake/View/Helper

CakePHP Plugins - Best way to implement a config file for the plugin?

I am developing a plugin and I'm trying to find the best way to have a set of default config options for it which are automatically used when the plugin is loaded, but also have the ability to customise them for a specific app.
I am struggling to find any good documentation about this for Cake 2.0+
Most of the solutions seem to involve configuring something in the main app bootstrap or making a config file in the main app, which seems like a bad idea because if you forget to do any of those things or don't do them correctly, the plugin won't work and it's relying on the 'outside' app, which doesn't seem right.
At the same time, it also seems like a bad idea to have the user edit some sort of config file within the plugin, as they are then tampering with the plugin itself.
What is the best way to do this (or what does everyone normally do)?
Take a look at this
http://api.cakephp.org/2.4/class-Configure.html
I was trying to figure out the same thing, then I saw it in a Twitter plugin. They have their own configure file in the plugin folder
You need to put a $config variable in the file. it doesn't do anything, but if you don't, Cake will complain.
This setup works the for plugin I am developing at the moment.
Example:
/Plugin/CakeCommerce/config/config.php
$config = '';
Configure::write('CC.default_email' , array('admin#server.com'=>'Server Admin'));
To load the config file. Do that in your __construct call in your plugin AppController.
class CakeCommerceAppController extends AppController {
public function __construct($request = null, $response = null)
{
parent::__construct($request, $response);
Configure::load('CakeCommerce.config' , 'default' , false);
}
}
What you should do is put your configuration settings in the plugins bootstrap file in Plugins/PluginDirectory/Config/bootstrap.php.
Then when loading the plugin the user has the option of whether to use the plugin's bootstrap file (See the CookBook). This appears to be all or nothing though, but you do have the advantage that your default configuration settings could always be used and so the user wouldn't have to create any config settings themselves.
I have found a plugin development article on the bakery and found this note:
Notice: be careful with configuration settings because plugin config files will override matching app settings, it is better to name settings with a plugin prefix
This to me suggests that you can't overwrite individual plugin settings from the application's bootstrap file. The user would have to modify the plugin's bootstrap file instead.
(The article is quite old and appears to be for an older version of cake so I don't know if things changed in more recent versions. Like you I am having a hard time finding much information on this subject)

Integrate js helper from cakephp 1.3.7 to cakephp 1.2.5

I am working on a project based on cakePHP 1.2.5. Now I need to use new JS helper defined in cakePHP 1.3.7.
I want to use some methods of JS helper like $this->Js->buffer("some code"),
$this->Js->writeBuffer()...
Is it possible to include this JS helper only to cakePHP 1.2.5? and How?
Thanks
I don't think you can because the basic way of calling the class has changed. In 1.2.5 the helper classes were not attached to the $this object in your view.
I think the better question would be to see if you can upgrade from 1.2 to 1.3. What are the requirements keeping you from upgrading, if any.
I would update your project to the current version of cakephp. A lot of bugs were fixed and other things were optimized.
But you can use the normal php or javascript functions...or build your own methods to solve your problem...
You could try to copy the JsHelper and its dependencies (HtmlHelper, FormHelper, and the engine helper for the Javascript framework you use) to the helpers folder of your application, though I don't know whether this will work...
However, even if this should work it is a hack, and I would consider to upgrade to CakePHP 1.3.x or to write your own helper providing the desired functionality.

Resources