cakephp 3 controllers cakedc plugin - cakephp

I'm trying to use the user plugin cakeDC with cakephp 3. I followed the documention and I can log a user, list users and that's all. For other actions I have an error :
Error: CakeDC/Users.EditController could not be found.
Error: Create the class EditController below in file: C:\wamp\www\cake/vendor/cakedc/users/src\Controller\EditController.php
My functions edit, delete... are defined in src/Controller/Users/UsersController.php but it doesn't recognize them except index().
What should I try?
Thanks.

The problem was in the routes.php. I tried the routes like https://github.com/CakeDC/users/blob/master/Config/routes.php and modified them. It's not optimized but it works.

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.

CakePHP: Can only load index.php, all pages 404 after Cake Bake

I'm trying to bake my first CakePHP application and am unable to get any page to particularly load for me right now. I've updated my config settings for salt,database, etc. and the index.php page tells me that I have configured everything.
So far I've used cake bake all on just one database table so far to make sure it loads properly. I created the Model, Controller, and View for the standard add/index/view/edit pages. When I try to access URL/organizations/index.php I'm hitting a 404 error however.
Is there any troubleshooting someone might have advice for how to solve this one? It is confusing to me that the index.php loads (so it redirects properly when loading the webroot) but trying to view any View pages yields no results. Is there any debug commands I can do to view what the valid pages would be? Or any additional information I can provide?
If you try URL/index.php/organisations or something similar to this and it works, then there is an issue with URL re-writing on the server which you'll need to correct.
I believe if you have things set up correctly you would want to visit /organizations in order to access the index() method of the organizations controller.
In general the ".php" is left off of the URL, as your index.php file initiates all the bootstrapping and routing. This also requires a correct .htaccess setup. Hard to say exactly what the problem is without seeing the app or an error message.
Try in url
URL/organizations/index.php
To
URL/organizations/index
or
URL/organizations/index.ctp
Cakephp using .ctp extension, that means cakephp Template. Please see this link. And also you can see your app\view\Organizations folder. Here all file is with .ctp extension. Isn't it ?

how to access static variable in cakephp 2

I am using the following code to check the logged in user's group (cakephp 1.3)
Session->read('Auth.User.user_group_id') != User::USERLEVEL_USER) :?>
(this is in .ctp file)
and now am migrating to cakephp 2. and this code is not working.
cant find any results when I googled .
Could anyone shed some light on this
in cakephp 2.x you're sending variables to view, so you don't have access to User class. you should get instance of this class in controller and send it as a variable to view.
please use this plugin to improve your debugging skills:
https://github.com/cakephp/debug_kit

How to use correctly a CakePHP plugin

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.

Plugin default controllers in CakePHP 2.0

I'm struggling to get a custom plugin to work with a default controller:
e.g. in CakePHP 1.3 I could create a users plugin and create a users_controller in it which automatically becomes the plugins controller,
I could access the methods of the user controller in the users plugin via:
/users/add
/users/edit/1
If I do the same in CakePHP 2.0 I get the following errors:
Error: Users.AddController could not be found.
Error: Create the class AddController below in file: /home/richarda/www/test/cake_zero/www/app/Plugin/Users/Controller/AddController.php
I can access them at the following urls:
/users/users/add
/users/users/edit/1
Oddly, the default index action works as expected, ie. I can go to
/users
and can see the index view from the users controllers in the users plugin.
There is no mention of default controllers for plugins in the 2.0 docs, has this functionality been removed?
Turns out default routing for plugins has been disabled in CakePHP2.0
Here's the ticket I posted: http://cakephp.lighthouseapp.com/projects/42648/tickets/2237-20-plugins-dont-have-a-default-controller#ticket-2237-3
The solution is to create a custom route:
Router::connect('/users/:action', array('controller'=>'users', 'plugin'=>'users');
And you're good to go.
Hope this helps someone.
I use this in cake 2.2.0 and it works for my plugin called admin.
Hope you can apply it to your situation.
Router::connect('/admin/', array('plugin'=>'admin','controller'=>'groups','action'=>'index'));

Resources