load model from another module - cakephp

I'm new in CakePHP. Currently I got a problem in my task.
I had assigned to create a Plugin name UserManagement and Login in app/Plugin/
The Plugin UserManagement already has completed. But now I having a problem in Plugin Login. Actually I need to use User Model from Plugin UserManagement in Plugin Login. However, I don't know how to write a scripts and what steps first I should taken?
I try before, create LoginsController in Controller. But It display an error mentioned that I need to create Model first. Infact that, I actually plan to use Model from UserManagement.
Please assists. Thank you.
Sorry..my English is bad. TT_TT

You have to add
$this->loadModel(PluginName.ModelName);
in our controller and then
$data = $this->ModelName->find('WhatEver');
Hope this will give you some help.

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 add new controller path

Background: I'm new to CakePHP and trying to modify an preexisting project.
When I try to add a new UploadsController it is supposed to handle xxxx/uploads requests right?
However, when entering the url neither AppController or UploadsController, allow index.php to do a dispatch. Files are being called. It goes straight to /uploads which is a directory.
I realize this is a bad design to begin with but trying to fix things one step at a time. Need to authenticate before going to /uploads, and than take action.
What am I doing wrong? I tried to modify routes.php to specify controller but that does not work either.
So as I said I am new and fixing someone else's project. I found that there is a security issue where uploads directory is exposed. So I figured I would add Controller to take care of this. uploads folder was under webroot as a result it was going to uploads folder and was not directing to controller. Thanks and hopes this helps someone in the future.. although kind of doubt it since it was a really bad way to do this to begin with.
With the info you gave this is what I can suggest:
Check to see if the UploadsController is in the Controller folder.
Make sure that there's at least a index.ctp file in Views/Uploads/
Check to see if the UploadsController has a default index() method. This is what www.example.com/uploads/ will hit.

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

Cakephp twitter autopost

I need to post a product details in twitter whenever it is added in my website.I am using Cakephp, and i use twitter datasource available in book.cakephp.org . It returns a 'Basic authentication is not supported ' message
when i searched it, i get that twitter is now using OAuth, how can i impliment this in my data source?. i did it referring the below url
http://book.cakephp.org/view/1077/An-Example
Anybody please help.
The Cake Book link seems to be out of date and relevant to the time before Twitter implemented OAuth.
I have accomplished what you're trying to do using http://code.42dh.com/oauth/ but this is basically a controller and a component, not a datasource as you asked.
There's a CakePHP OAuth Datasource for CakePHP which might work (I haven't tried it) but you'll need to add the PHP Oauth library too. If you choose to do this, let me know if it works as interacting with Twitter as a datasource makes a lot more sense, especially as Cake are depreciating components.
I have created this - it isnt a datasource but it provides a model you can use to accomplish what you need to do:
https://github.com/voycey/CakePHP-Twitter-API-v1.1-Full
You can either use a Shell to have it post automatically or you can just call it in your controller when you add your entity.

Can not get session in cakePHP

I have problem with session in cakephp.I have one file chat.php that is in webroot folder but when I run that file with ajax I could not find session which is created from chat.ctp file.
so anyone has idea how to get session in third party file in cakePHP.
can I write session any .ctp file ?
Thanks.please help me.
Yes, it's totally possible.
In APP/config/core.php, find the "Session.cookie" config value:
Configure::write('Session.cookie', 'CAKEPHP');
Then, in your external file, just set the same session name when you start your session:
session_name('CAKEPHP');
session_start();
print_r($_SESSION);
I have heard that this is a spotty thing but for me, the solution above has worked in two projects so far.
I'm assuming you're using the Session component to write the session variable and then trying to read it from a regular 'ol php file outside of cake's scope using the $_SESSION variable.
As you have figure out, this is not guaranteed to work properly. The best way to handle this would be to integrate your chat.php file into cake, using a controller, action and view.
If this for some strange reason isn't possible, you would need to import all the files responsible for setting up cake and instantiate the session component and use that to read from your session. I'd strongly recommend going with way #1.
I don't think it's a good idea to use the third party session in CakePHP since Cake has already done that very well.And I believe the best place to make some sessions is controller,not view.See Session in Cakephp's cookbook and Ajax helper of Cake.Probably they would help.

Resources