How can I create cached files within CakePHP Plugin? - cakephp

I have created a plugin for CakePHP that I would like to generate cached files within its own plugin folder at /app/Plugin/MyPlugin/tmp/cache.
I have already cerated the MyPlugin/tmp/cache directory manually.
I have created a bootstrap file at /app/Plugin/MyPlugin/Config/bootstrap.php with the following content:
<?php
Cache::config('short', array(
'engine' => 'File',
'duration' => '+60 minutes',
'path' => 'Plugin/MyPlugin/tmp/cache',
'prefix' => 'cake_short_',
'mask' => 0666,
));
I have created a Shell script at /app/Plugin/MyPlugin/Console/Command/MyPluginShell.php with the following function:
<?php
...
public function get_listings() {
$listings = $this->Listing->find('all');
Cache::write('listings', $listings, 'short');
$this->out('Task Completed');
}
I can run get_listings from within the Cake console just fine and the Task completes, however there is no Cache file being created at /app/Plugin/MyPlugin/tmp/cache like I would expect.
On a side note, I have tried replacing 'path' => 'Plugin/MyPlugin/tmp/cache' with 'path' => CACHE just to see if it will appear in app/tmp/cache but haven't had any luck.
I have also created the /app/Plugin/MyPlugin/tmp/cache within the plugin.
Any help would be appreciated.

I figured it out.
I wasn't loading the plugin correctly within app/Config/bootstrap.php.
I was doing this (incorrect):
CakePlugin::load('MyPlugin', array('routes' => true));
Instead of this (correct):
CakePlugin::load('MyPlugin', array('routes' => true, 'bootstrap' => true));

Related

Drupal hook not being called

I'm new to Drupal making a plugin that hooks into the ckeditor widget. I absolutely can't figure out why my implementation of a hook that is defined in ckeditor is never called.
Here are some details
my module is enabled
I'm able to use more basic hooks like exceltohtml_plugin instead of exceltohtml_ckeditor_plugin and reach my test statement.
I'm can't think of any more troubleshooting ideas to reveal the issue so any help would be greatly appreciated.
exceltohtml.module
<?php
error_log("TEST: this will print to log");
// implementation of hook_ckeditor_plugin()
function exceltohtml_ckeditor_plugin()
{
error_log("TEST: but this will never run");
return array(
'exceltohtml' => array(
'name' => 'exceltohtml',
'desc' => t('Excel sheet upload'),
'path' => drupal_get_path('module', 'exceltohtml') .'/plugins/exceltohtml',
'buttons' => array(
'excel_to_html' => array('label' => 'Insert spoiler','icon' => '/images/image.gif' ),
)
)
);
}
ckeditor.api.php (the file in ckeditor that Im basing my hook on)
/**
* Hook to register the CKEditor plugin
*/
function hook_ckeditor_plugin() {
return array(
'plugin_name' => array(
// Name of the plugin used to write it.
'name' => 'plugin_name',
// Description of the plugin - it would be displayed in the plugins management section of profile settings.
'desc' => t('Plugin description'),
// The full path to the CKEditor plugins directory, with the trailing slash.
'path' => drupal_get_path('module', 'my_module') . '/plugin_dir/',
'buttons' => array(
'button_name' => array(
'icon' => 'path to button icon',
'label' => 'Button Label',
)
)
)
);
}
If the function in your module is new, then the slightly older version of your module code might be cached.
Visit the module listing page in Drupal, that should reload the modules PHP code:
admin/modules

CakePHP 3 Plugin Routes don't seem to be loading

I'm trying to load routes in a plugin in CakePHP 3.2. They work fine if I put the routes in the core routes.php file, but not in my plugin routes.php file.
The plugin name is: MFC/HDParser.
The path is: /vendor/mfc/hdparser. (The plugin was originally baked into the plugins directory, but transferred it to the vendors directory (and all the files updated) just in case that was the issue.)
In the core bootstrap file I have:
Plugin::load('MFC/HDParser', ['bootstrap' => true, 'routes' => true, 'autoload' => true]);
/vendor/cakephp-plugins contains the line:
'MFC/HDParser' => $baseDir . '/vendor/mfc/hdparser/'
/composer.json contains:
"autoload": {
"psr-4": {
"App\\": "src",
"mfc\\hdparser\\": "./vendor/mfc/hdparser/src",
}
},
My /vendor/mfc/hdparser/config/routes.php contains:
use Cake\Routing\Router;
Router::plugin(
'MFC/HDParser', ['path' => '/hdparser'], function ($routes) {
$routes->connect('/charactersheet', ['plugin' => 'MFC/HDParser', 'controller' => 'Charactersheet', 'action' => 'index']);
// $routes->connect('/:controller');
//
//$routes->resources('Charactersheet');
// $routes->fallbacks('DashedRoute');
//}
);
//Router::connect('/charactersheet', ['plugin' => 'MFC/HDParser', 'controller' => 'Charactersheet', 'action' => 'index']);
I've also tried using 'path' => '/mfc/hdparser'
When I try to access site.dev/charactersheet I get "Error: CharactersheetController could not be found."
If I try to use a route like /mfc/hdparser/charactersheet, /MFC/HDParser/charactersheet or /m-f-c..., /m_f_c..., /Mfc..., etc I get a missing controller ('Mfc') error.
If I put the line:
Router::connect('/charactersheet', ['plugin' => 'MFC/HDParser', 'controller' => 'Charactersheet', 'action' => 'index']);
in the core routes.php file (outside the scope, before Plugins:load(); or inside the scope using $routes->connect() ) it works fine.
I've tried inflecting just about everything using underscores, dashes and camelcase.
I've dug through the documentation (http://book.cakephp.org/3.0/en/plugins.html, http://book.cakephp.org/3.0/en/development/routing.html, http://api.cakephp.org/3.2/class-Cake.Routing.Router.html) and tried everything I could find there (and here), as well as using the CakeDC/Users plugin as a template, but I'm still missing something...
--MFC
I had the same issue with CakePHP 3.3, the routes.php file in my plugin was not loaded.
I fixed it by putting Plugin::routes(); after loading my plugins. This method loads all routes for plugins, where the configuration has 'routes' => true.
E.g.
Plugin::load('MyPlugin', ['routes' => true]);
Plugin::routes();

How to know webroot in core.php

I'm using CakePHP 2.3.1.
Our server has some independent applications in one server. So I want to change session.cookie_path setting following the Cookbook :
Configure::write('Session', array(
'defaults' => 'php',
'ini' => array(
'session.cookie_path' => '/app/dir'
)
));
I could change it successfully with this. But here is a problem. I need to set session.cookie_path value to webroot dynamically (without string literal value such as '/app/dir').
I've tried to use $this->webroot following this Q&A, but of course it does not work because there is no controller in the file app/Config/core.php.
Any ideas?
I realized a php variable is available : $_SERVER['REQUEST_URI'].
So I could solve the problem.
$requestURI = $_SERVER['REQUEST_URI'];
$webroot = preg_replace('/(^\/[^\/]+\/).*$/', '$1', $requestURI);
//echo $webroot;
Configure::write('Session', array(
'defaults' => 'cake',
'ini' => array(
'session.cookie_path' => $webroot // looks like '/app/'
)
));
But this solution does not have reusability enough : it would not work for apps located in deeper directories such as /apps/app1/.
I'm still awaiting a better solution.

CakePdf - Engine not loaded

I have installed CakePdf plugin for cakephp as outlined in the readme file on GitHub. I am getting an error saying the engine iisnt loaded. I have tried all 3 engines that come with the plugin. Anyone else have this issue and find a solution? Rhanks
http://www.slideshare.net/jellehenkens/building-php-documents-with-cakepdf-cakefest-2012
Here is a slideshow how to install the plugin,
To load the engine use this in bootstrap.php
Configure::write('CakePdf', array(
'engine' => 'CakePdf.DomPdf',
'pageSize'=>'A4',
'orientation' => 'landscape',
));
Go to your controller en use in your public function view or index this:
$this -> pdfConfig = array (
'orientation' => 'landscape',
'download' => true,
'filename' => 'Client_' . $id
);
i hope this will help you,

CakePHP page with no headers/footers

In a download page for a blob from a database, how would I make it so that no other output is sent? Right now it's sending the header, debug info, and a footer. How do I make it so that none of that is sent, just for that view?
you can create an clear layout (e.g. empty.ctp ) in you layouts folder, only with
<?php echo $content_for_layout ?>
and then in you action where you're getting your blob data use that layout
$this->layout = 'empty.ctp';
and also to disable debugging, in your controllers use
Configure::write('debug',0);
if you're unable to create new layout you could try this.
$this->layout = null;
$this->render("view_name");
If you're using this to download files, you should use the Media view in cakePHP
http://book.cakephp.org/view/1094/Media-Views
$this->view = 'Media';
$params = array(
'id' => 'example.zip',
'name' => 'example',
'download' => true,
'extension' => 'zip', // must be lower case
'path' => APP . 'files' . DS // don't forget terminal 'DS'
);
CakePhp 2.3 users :
use Sending files from the Book
CakePhp 2.x users :
use '$this->viewClass' instead of '$this->view'
copy-paste ready full solution, right in any controller file:
<?php
public function download($file) {
$fsTarget = APP.WEBROOT_DIR.DS.'files'.DS.$file; // files located in 'files' folder under webroot
if (false == file_exists($fsTarget)){
throw new NotFoundException(__('Invalid file'));
}
$pathinfo = pathinfo($fsTarget);
$this->viewClass = 'Media';
$params = array(
'id' => $file,
'name' => $pathinfo['filename'], // without extension
'download' => true,
'extension' => $pathinfo['extension'], // must be lower case
'path' => dirname($fsTarget) . DS // don't forget terminal 'DS'
);
$this->set($params);
}
Hope this helps!

Resources