Can't create simple CakePHP plugin - cakephp

I would like to create a plugin for CakePHP 3.1.4. The documentation is straight forward, but the example doesn't work (http://book.cakephp.org/3.0/en/plugins.html#creating-your-own-plugins)
The steps are:
composer create-project --prefer-dist cakephp/app sampleapp
Create the database. Connect to the database. Create a table "contacts". Navigate in the directory and run:
bin/cake bake plugin ContactManager
Create the controller:
bin/cake bake controller --plugin ContactManager Contacts
Re-generate the autoloader:
composer dumpautoload
Add this line to the /config/bootstrap.php file:
Plugin::load('ContactManager', ['routes' => true]);
But now, the documentation sais
"If you want to access what we’ve got going thus far, visit
/contact-manager/contacts. You should get a “Missing Model” error
because we don’t have a Contact model defined yet."
But this doesn't work. Instead I get an error:
Missing Controller.
Cake\Routing\Exception\MissingControllerException.
Cake\Routing\Dispatcher->dispatch ROOT/webroot/index.php, line 37
Error: ContactManagerController could not be found. Error: Create the
class ContactManagerController below in file:
src/Controller/ContactManagerController.php
This means the plugin could not be loaded otherwise it would not suggest this. When opening the DebugKit under "Include" the plugin is not in the plugins array.
I checked the composer.json files and in both the plugin is listed correctly. The bake command ran through without errors. I tried the above steps with multiple new projects with different names.
What is the problem here? Thank you very much.

Finally, I found the solution.
What the docs say should be in /plugins/ContactManager/config/routes.php AND what bake plugin creates:
Router::plugin('ContactManager', function ($routes) {
$routes->fallbacks('DashedRoute');
});
But what really needs to be in the file instead of the above snippet is:
Router::scope('/contactmanager', ['plugin' => 'ContactManager'], function ($routes) {
$routes->fallbacks();
});

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?

CakePHP 3 Debugkit.toolbar will not load

Just setup CakePHP 3 and get this error after I added the plugin hooks to the code in the AppController and bootstrap.php
Error: Class 'App' not found File /var/www/fastcom-portal-one/app/plugins/DebugKit/Controller/Component/ToolbarComponent.php
Line: 16
DebugKit has not yet been updated for CakePHP 3.
Remember to run
bin/cake plugin assets symlink
So DebugKit is able to load its assets correctly.
It's already supported, install it using composer:
"require": {
"cakephp/debug_kit": "3.0.*-dev"
},
From now on you don't need to set DebugKit in your AppController, just add this to your bootstrap.php file:
Plugin::load('DebugKit', ['bootstrap' => true]);

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 to use DebugKit toolbar in cakephp 2.1.3 or cakephp 2.0

I have tried to use cakephp Debugkit toolbar for cakephp 2.1.3... but it is not working fine..
I have implemented debugkit on cakephp 1.3 it is working fine..
i downloaded debugkit for cakephp 2.1.3. I rechecked that..
How i am implementing it??
app/plugin/DebugKit ///this is my path for debugkit
I checked my debug mode is 2..
Configure::write('debug', 2);
how i am loading my debug kit in Appcontroller..'
public $components = array('DebugKit.Toolbar');
It is showing error
Parse error: syntax error, unexpected T_FUNCTION in /var/www/guest1/cakephp-2.1.3/app/Plugin/debug_kit/Controller/Component/ToolbarComponent.php on line 165
I download debugkit from different source but same result.
Thanks !
it is showing error in this function and line
public function implementedEvents() {
$before = function ($name) {
return function () use ($name) {
DebugTimer::start($name, __d('debug_kit', $name));
};
};
$after = function ($name) {
return function () use ($name) {
DebugTimer::stop($name);
};
};
I loaded file in bootstrap.php at the end
CakePlugin::loadAll();
Error:-
Error: DebugKit.ToolbarComponent could not be found.
Error: Create the class ToolbarComponent below in file: /var/www/guest1/cakephp-2.1.3/app/Plugin/DebugKit/Controller/Component/ToolbarComponent.php
You have downloaded the latest version of the DebugKit plugin, which uses closures. As you can see there, you need at least PHP 5.3 for this to work.
Simply download an earlier version of DebugKit which is compatible with your version of PHP.
I have same problem. I guess if you are using Cakphp 2.0 and higher, you cannot use Debugkit version lower than 2 because it has a different Plugin name convention. Files have a different tree structure.
So all you have to do is use a lower version and rename the files or update PHP or use lower version of Cakephp.
I had the same problem. DebugKit worked on my local server, but crashed with the same error online. The problem was that i didn't have a database connection. After i succesfully connected to the database, debugkit worked fine.

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