cakephp: how do you load a behavior from a plugin? - cakephp

edit
I used cake bake to make a new plugin and behavior (using --plugin) and it worked. Not sure what I missed.
I'm trying to load the behavior of a plugin. I'm trying to do this with a plugin I wrote for my own use, but I get the same problem when trying to load someone else's.
For example loading https://github.com/Xety/Cake3-Upload
I know you should normally use composer, but this is the way I would load my own plugin.
copy the plugin to \app\plugins\Cake3Upload
load the plugin in bootstrap.php using
Plugin::load('Cake3Upload', ['autoload' => true]);
access the behavior in my model's MyModelTable.php initialize function
$this->addBehavior('Cake3Upload.Upload');
I then get the following error:
Missing Behavior
Cake3Upload.UploadBehavior could not be found.
Make sure your plugin was loaded from config\bootstrap.php and
Composer is able to autoload its classes, see Loading a plugin and
Plugins - autoloading plugin classes
Error: Create the class UploadBehavior below in file:
\app\plugins\Cake3Upload\src\Model\Behavior\UploadBehavior.php
use Cake\ORM\Behavior;
class UploadBehavior extends Behavior
{
}

And what about namespaces ? If u wont use composer u must build the same files tree.
I think it's just about the uploadBehavior have this namespace:
namespace Xety\Cake3Upload\Model\Behavior;
then u only have to follow the plugin doc:
Plugin::load('Xety/Cake3Upload');
and
$this->addBehavior('Xety/Cake3Upload.Upload');
Add 'Xety/' maybe

Related

CakePHP 3 Audit Log, working with admin routes?

I'm trying to work with this CakePHP 3 plugin which I think does exactly what I need it to do, but am struggling big time! I'm new to CakePHP 3, composer and fiddling with routes etc (previously CakePHP 2.x and manual installation and never played with routes).
The plugin does not appear to be maintained any longer and the package does not exist when trying to install via composer, so I forked it and cloned the files to /plugins/AuditLog/
I've run updated my app composer.json file, loaded the plugin in my bootstrap.php, and run composer install on /plugins/AuditLog/ to load the dependencies from the plugin composer.json file (not sure if this is the right thing to do?)
In plugins/AuditLog/config/routes.php there is the following code
Router::plugin('AuditLog', function ($routes) {
$routes->prefix('admin', function ($routes) {
$routes->fallbacks('DashedRoute');
});
$routes->fallbacks('DashedRoute');
});
So a bit of searching in the CookBook and I read for the first time about 'prefix routing', which from what I can tell means I should be able to reach the pages via /admin/audit-log/audits (for example).
Not surprisingly, I get a missing controller error here
Error: Create the class AdminController below in file: src/Controller/AdminController.php
The instructions for the plugin don't mention anything about creating an admin controller, and there are no controller files anywhere in the plugin at all actually, despite there being views and models.
I'm really out of my depth here, can anyone help me untangle this? Where/how to add which controllers and get them pointing to the right place?

Include sitemap in cakephp

I generated a normal sitemap with an online Sitemalgenerator:
http://xml-sitemaps.com
Now I tried to include it like every other Sitemap on a Server.
The problem is, on this Server seems to be a CakePHP Framework and I dont know, how it works cause
I dont have any experience in PHP.
The error:
Missing Controller
Error: Sitemap.xmlController could not be found.
Error: Create the class Sitemap.xmlController below in file: app/Controller/Sitemap.xmlController.php
<?php
class Sitemap.xmlController extends AppController {
}
Can you tell me, how to include a XML Sitemap in the easiest way? I'm a beginner in Webprogramming!
(I dont want to create a dynamic Sitemap)
Thank you!
Place your file in your webroot folder:
app/webroot/Sitemap.xml
As long as the file is found in the webroot CakePHP will load it, if the file is not found it will run through its normal application request cycle based on your defined routes.

CakePHP Twitter plugin

Appologies if this is something obvious I've missed.
I'm trying to use this Cake plugin to add a twitter feed to my cake app (just display my latest tweet). I've followed the docs in the readme but I get this:
Missing Controller
Error: Twitter.UsersController could not be found.
Error: Create the class UsersController below in file: C:\wamp\www\mysite\app\Plugin\Twitter\Controller\UsersController.php
Heres what I've done so far (as the readme instructs):
Cloned all the files into app/Plugin/Twitter/
Rename Config/twitter.default.php to Config/twitter.php
Updated the config file with my twitter app keys
Visited mysite/twitter/twitter/connect - which gives me the above error.
The readme mentions that the plugin uses - http_socket_oauth, another cake plugin, I trust that this is included in this plugin and that I dont have to install that plugin aswel?
Could someone point me in the right direction?
Thank you

How do I install a CakePHP plugin?

More easy questions - this time, how do I install a CakePHP plugin? I get that I take the plugin folder and put it in the, well, the plugins directory.
so
/app
/plugins
/what is this?
And I ask this, because I just downloaded a plugin - here:
http://milesj.me/blog/read/changelog-forum-2.3
And the name of the folder that everything is in is milesj-cake-forum-29a0699
This doesn't appear to be a good plugin name... is there some place that lists or describes what the plugin should be called? I know i know, I'm going to try it out without the 29stuff, but I like things to be silky smooth, not "trial and error".
Yes, I am a terrible programmer.
Based on the plugin's documentation, they refer to models as Forum.Profile for example which suggests that the plugins should be installed in APP/plugins/forum as that's where the CakePHP autoloader will look unless instructed otherwise (section 2).
for example fb plugin ; Load the plugin in your app/Config/bootstrap.php file:
//app/Config/bootstrap.php
CakePlugin::load('Facebook');
You can use all or some of the Facebook plugin as you see fit. At the very least you will probably want to use the Facebook Helper
public $helpers = array('Facebook.Facebook');

CakePHP 2.0 Plugin URL

Trying to get CakePHP work with subfolders for Controllers, Views, or Models is not really working and from what I've read I need to use "Plugins". Right now I have the following folder structure:
/app/Plugin/Manager/
/Controller
CandyController.php
/Models
/View
/Candy
viewCandy.ctp
ManagerAppController.php
ManagerAppModel.php
When I try and set my url to: http://localhost/Manager/Candy/viewCandy/123. I get the error message: "ManagerController does not exist". Why is CakePHP not picking up that it should look in the Manager plugin folder?
Now that 2.0 has been released the docs are fleshed out a bit more. This is described in the Plugin section of the cookbook.
They suggest putting this in bootstrap.php instead of routes.php:
CakePlugin::loadAll(); // Loads all plugins at once
CakePlugin::load('ContactManager'); //Loads a single plugin
Here is the solution:
Go to /app/Config/routes.php and add the line CakePlugin::load(array('YourPluginName')); after the line CakePlugin::routes();. In my case it was line 40.
Basically CakePHP 2.0 doesn't automatically load plugins. I think that's fine and dandy, but there isn't really any documentation for this.

Resources