How to load custom plugins in CakePHP? - cakephp

I'm writing a poll plugin for a website based on CakePHP. The plugin works good if I access it from its own URL (eg. myapp.com/plugin/controller) but I need to call it from different pages. I would like to include it as a widget in every page.
I'm looking for a method like $myplugin->renderPoll($pollId); but I really didn't find any information about how to instantiate the Polls class. I tried with App::import and ClassRegistry::init with no luck.
Any suggestion ?
Thank you

Looks like you are trying to create some sort of Helper to create poll cross views? I would suggest creating a Helper for that particular class. Simply create a helper in plugins/plugin_name/views/helpers/foo.php, and in each controller (or in app_controller.php) that you need it, include the helpers as $helpers = array("PluginName.Foo"); and inside your view, you should be able to use the methods defined in foo.php by calling $foo->renderPoll($pollId).
//app/plugins/plugin_name/views/helpers/foo.php
class FooHelper extends AppHelper {
var $name = "Foo";
function renderPoll($id=0) {
//...
}
}

Use Elements! They're small blocks of presentation code that need to be repeated from page to page, sometimes in different places in the layout.
Check this link out: http://book.cakephp.org/view/1081/Elements
I guess this link explains everything you need.

Related

Include utility class all over the application cakephp 3

I have custom utility class that contains some methods for general use and it is located in src/Utility/UtilityClass.
I can include the Utility class in controller .e.g.
use App\Utility\ArrayUtil;
and then call the class in my controllers e.g.
ArrayUtil::myMethod();
And it works. I need to include the UtilityClass in bootstrap so it applies all over the application so I can reuse it in models, templates and other controllers as well.
I tried to load it in config/bootstrap.php but I get this error:
Error: Class 'ArrayUtil' not found
Any idea
You can add this line at the top of the page, be it Model, view or controller.
use App\Utility\ArrayUtil;
If you're using this utility in multiple views, then I'd suggest you to write this line in Template/Layout/default.ctp, since all templates would be a part of this.
Hope this helps.
Peace! xD

Allowing the user to change config settings in CakePHP

Using CakePHP 2.6.7
I have created a plugin and there are 2 variables which for the most part are effectively constants - but the user should be able to change their values (they are paths to header and footer images).
I had been trying to use Configure::read() and Configure::write() but now realise that isn't what Configure is intended for and doesn't actually work in that manner at all.
How should these two variables be stored so that the values can be changed by a user and these changes would be permanent (until they make another change)?
Initial Solution
I've now solved the problem by serializing the data in an array to a text file. It would be great if someone had a more elegant solution though.
Simplest solution would be to store these values in the database as settings and then load them in.
We often do this using a Setting model to store the name-value pairs then attach a component (often to AppController) that loads in the data. For example, create a component like this:-
App::uses('Component', 'Controller');
class SettingsComponent extends Component {
public function initialize(Controller $Controller) {
$Controller->loadModel('Setting');
$settings = $Controller->Setting->find('all');
foreach($settings as $setting) {
Configure::write('Setting.' . $setting['Setting']['name'], $setting['Setting']['value']);
}
return;
}
}
Then load this for any controller that needs these settings:-
public $components = array('Settings');
You can then access the values in your code like:-
Configure::read('Setting.app_name', 'My Cake App');
You can easily extend the functionality of the component and what is stored in the settings table to make this approach as flexible as you need.

Cakephp using Cache in Component

I am using Cakephp 2.3 , i want to perform lots of common calculation in my several controllers. so i write those functions in my appcontroller and i cached some datas..
but what happens is my appController will become fatty.. so i create some component to perform these actions.. i dont know my approach is right or not..? please suggest..
i want to use cache in my component, i tried this code. but nothing is cached..
public $helpers =array('Cache');
and
$result = Cache::read('fm_data', 'long');
if (!$result) {
$result =
$this->TFmStation->find('all',array('contain'=>array('TLocation',
'TLanguage','TMediaOrg','TContactPerson',
'TAddress','TFmProgram'=>array('TTargetGroup'))));
Cache::write('fm_data', $result, 'long');
}
return $result;
please help me to how to use cache in component
how to effectively use component class in cakephp in the case of more common functions in the application.. when i write these functions in appController it load all the functions so according to memory prespective, how to effectively use component
Model TFmStation is best place to have the above logic. Components are there for generic functionality like UploadComponent, EmailComponent, RecaptchaComponent etc. If your logic part have something to do with model, then it should go into that model.
A Model can be called from AppController.php in a similar fashion as you calling your Component.

How to preserve layout through different views of same controller in cakephp

I'm using cakephp and I have set a simple site, when rendering index() it works fine
but when programming other methods of the same controller, the views for them do not show the
background, it's like it cannot find the images, I thought the layout would be preserved for all views.
Your View/Layouts/default.ctp layout file will always be used unless specified otherwise. If it's not showing a background, it's like you're using an incorrect path for the image, css...etc.
If you want to apply a layout to all methods of a particular controller (but not all other controllers) then use a theme.
Controller Code:
class MyThingController extends AppController {
public $theme = 'MyTheme';
....
}
Next you have to put your layout file in:
/app/View/Themed/MyTheme/Layouts/default.ctp
Then all methods in your controller will use this layout by default.
See here for more info: enter link description here
(note: this answer applies to Cake version 2.1+)

CakePHP - Render a view that is actually plugin's view from Component

Morning guys,
So this is my first time developing a plugin for CakePHP. Here's what I am doing in startUp of the component.
//component
function startUp(&$controller){
//....
if($render){
$controller->render("return", "ajax");
}
}
By default render will look at app/views/<controllers>/return.ctp and app/views/layouts/ajax for this render call.
Is there anyway that I can give a directive to render from app/my_plugin/views/awesome_stuffs/return.ctp and app/my_plugin/views/layout/ajax.ctp instead?
I believe the third param of Controller::render($file, $layout, $file) could do the job, but is there any better Cake way of doing things?
Plus, is that considered a good practice to take over controller's rendering function like that?
One way is to call the PLUGIN controller/action URL in your AJAX call, instead of the main app controller/action URL.
ex:
instead of:
http://domain.com/controller/action
you call:
http://domain.com/my_plugin/controller/action
When you do it this way, the plugin views & layouts are called automagically. See:
http://book.cakephp.org/view/1118/Plugin-Tips
http://book.cakephp.org/view/1115/Plugin-Views
Otherwise, the only way I know of is manually setting paths as you mentioned or controller-wide via:
var $viewPath = 'path/to/plugin/views/';
var $layoutPath = 'path/to/plugin/layouts/';
You might want to try setting $this->view to the plugin dotted view file you want to render.
add to your source
$controller->plugin = "pluginname";

Resources