What has happened to javascript helper in cakePHP 2? - cakephp

I have used this item and get this error :
Missing Helper
Error: JavascriptHelper could not be found.
Error: Create the class JavascriptHelper below in file: app/View/Helper/JavascriptHelper.php
<?php
class JavascriptHelper extends AppHelper {
}
Well indeed, this file does not exists, and I tried to use 'Js' in my helper array.
class myClassController expend AppController {
var $helpers = array('Html', 'Js'); // and not 'Javascript');
In the code, the method $this->Javascript->codeBlock is called to add a javascript method (in the middle of the content instead of the header) but there is no $this->Js->codeBlockcodeBlock either.
$output .= $this->Js->codeBlock("datepick('" . $htmlAttributes['id'] . "','01/01/" . $options['minYear'] . "','31/12/" . $options['maxYear'] . "');");
Could you explain me what happened to the old Javascript helper or how to get the code working?
Is there an other helper which could work with CakePHP-2.0?
Cordially,

Have you read the migration guide? If not do that now:
http://book.cakephp.org/2.0/en/appendices/2-0-migration-guide.html#xmlhelper-ajaxhelper-and-javascripthelper-removed
XmlHelper, AjaxHelper and JavascriptHelper removed The AjaxHelper and
JavascriptHelper have been removed as they were deprecated in version
1.3. The XmlHelper was removed, as it was made obsolete and redundant with the improvements to Xml. The Xml class should be used to replace
previous usage of XmlHelper.
The AjaxHelper, and JavascriptHelper are replaced with the JsHelper
and HtmlHelper.
JsHelper JsBaseEngineHelper is now abstract, you will need to
implement all the methods that previously generated errors.
So
$this->Js->codeBlock('...');
is now
$this->Html->codeBlock('...');

HtmlHelper::scriptBlock($code, $options = array())
//Parameters:
$code (string) – The code to go in the script tag.
$options (array) – An array of html attributes.

Related

How to load/execute a view template from a string in CakePHP

I've developed a CakePHP plugin that allows the site administrator to define custom reports as a list of SQL queries that are executed and the results displayed by a .ctp template.
Now I need to allow the administrator to edit the template, stored in the DB together with the report.
Therefore I need to render a template that is inside a string and not in a .ctp file and I could not find anything in the core that helps.
I considered initially the approach to write the templates in .ctp files and load them from there, but I suspect this solution is rigged with flaws re: the location of the files and related permissions.
A better solution seems to override the View class and add a method to do this.
Can anyone suggest a better approach ?
P.S. Security is not a concern here, since the administrator is basically a developer without access to the code.
In CakePHP 2.0:
The View::render() method imports the template file using
include
The include statement includes and evaluates the specified file.
The evaluated template is immediately executed in whatever scope it was included. To duplicate this functionality, you would need to use
eval()
Caution: The eval() language construct is very dangerous because it
allows execution of arbitrary PHP code. Its use thus is discouraged.
If you have carefully verified that there is no other option than to
use this construct, pay special attention not to pass any user
provided data into it without properly validating it beforehand.
(This warning is speaking to you, specifically)
...if you wish to continue... Here is a basic example of how you might achieve this:
$name = 'world';
$template = 'Hello <?php echo $name ?>... <br />';
echo $template;
// Output: Hello ...
eval(' ?>' . $template . '<?php ');
// Output: Hello world...
Which is (almost) exactly the same as:
$name = 'world';
$template = 'Hello <?php echo $name ?>... <br />';
file_put_contents('path/to/template.php', $template);
include 'path/to/template.php';
Except people won't yell at you for using eval()
In your CakePHP application:
app/View/EvaluatorView.php
class EvaluatorView extends View
{
public function renderRaw($template, $data = [])
{
if (empty($data)) {
$data = $this->viewVars;
}
extract($data, EXTR_SKIP);
ob_start();
eval(' ?>' . $template . '<?php ');
$output = ob_get_clean();
return $output;
}
}
app/Controller/ReportsController.php
class ReportsController extends AppController
{
public function report()
{
$this->set('name', 'John Galt');
$this->set('template', 'Who is <?php echo $name; ?>?');
$this->viewClass = 'Evaluator';
}
}
app/View/Reports/report.ctp
// Content ...
$this->renderRaw($template);
Alternatively, you may want to check out existing templating engines like: Mustache, Twig, and Smarty.
Hmmm.. Maybe create a variable that will store the generated code and just 'echo' this variable in ctp file.
I had similar problem (cakephp 3)
Controller method:
public function preview($id = null) {
$this->loadModel('Templates');
$tempate = $this
->Templates
->findById($id)
->first();
if(is_null($template)) {
$this->Flash->error(__('Template not found'));
return $this->redirect($this->referer());
}
$html = $template->html_content;
$this->set(compact('html'));
}
And preview.ctp is just:
<?= $html

View Error CakePHP UploadPack plugin

I am using CakePHP's UploadPlugin (https://github.com/szajbus/uploadpack). Storing images goes smooth as documented, the problem kicks in when trying to use the helper to view the images:
Fatal Error
Error: Call to a member function image() on a non-object
Code:
<?php echo $upload->image($entry['User']['id'], 'User.avatar') ?>
The helper is correctly loaded in the controllers. What could caseis issue? thanks
UPDATE:
My helper is included as follows:
public $helpers = array('Form', 'UploadPack.Upload');
I had the same issue. I was able to fix it by doing the following:
echo $this->upload->image($MODELNAME, 'MODEL.avatar');

How to use find('all') in Views - CakePHP

I searched a lot but I couldn't find on How to use the find('all') in Views as used in Rails, but here I'm getting the error "Undefined property: View::$Menu [APP\Lib\Cake\View\View.php, line 804]"
'Menu' is the model which I'm using to fetch data from the menus table.
I'm using the below code in views:
$this->set('test',$this->Menu->find('all'));
print_r($test);
Inside your Menu model create a method, something like getMenu(). In this method do your find() and get the results you want. Modify the results as you need and like to within the getMenu() method and return the data.
If you need that menu on every page in AppController::beforeFilter() or beforeRender() simply do
$this->set('menu', ClassRegistry::init('Menu')->getMenu());
If you do not need it everywhere you might go better with using requestAction getting the data using this method from the Menus controller that will call getMenu() from the model and return the data. Setting it where you need it would be still better, if you use requestAction you also want to cache it very likely.
TRY TO NOT RETRIEVE DATA WITHIN VIEW FILE. VIOLATION OF MVC RULE
try this in view file:
$menu = ClassRegistry::init('Menu');
pr($menu->find('all'));
In AppHelper ,
Make a below function
function getMenu()
{
App::import('Model', 'Menu');
$this->Menu= &new Menu();
$test = array();
$test = $this->Menu->find('all');
return $test;
}
Use above function in view like :
<?php
$menu = $html->getMenu();
print_r($menu);
?>
Cakephp not allow this .
First create the reference(object) of your model using ClassRegistry::init('Model');
And then call find function from using object
$obj = ClassRegistry::init('Menu');
$test = $obj->find('all');
echo ""; print_r($test); `
This will work.

CakePHP generate and save PDF

I am using CakePHP for an application that does auto generation of vouchers to a PDF file. But they work through user clicks. Now I wish to create it automatically and have the file written to the server hard disk. Then later on, these files will get zipped up and sent to an email of my choice.
For the PDFs I use Html2ps/Html2pdf component found in CakePHP. You can view it here http://bakery.cakephp.org/articles/Casmo/2010/06/26/creating-pdf-files-with-html2ps-html2pdf
One issue I have is the formatting doesn't look right. If I have links that look like this:
http://www.mydomain.com/this-is-my-perma-link
They will render this way in the generated PDF:
http://www.mydomain.com/this- is- my- perma- link
And that would be a broken link. I've tried to use other characters to replace the dash but it doesn't work. I am not sure why.
Another issue is, how can I write the generated PDF file to my server hard disk? Is there an option for me to do that and how do I define the destination. Any examples?
Maybe thanks in advance!
Hi you can to use FPDF library . and you can to save file in the server and then redirect to other function.
check the next code.
///Call to library
<?php
App::uses('AppController', 'Controller');
require_once APP . 'Vendor' . DS . 'fpdf17' . DS . 'fpdf.php';
class TestController extends AppController {
public function test(){
--Create fpdf.
$pdf = new FPDF();
$pdf->AddPage();
$pdf->Output('files/Report.pdf');
$pdf->text($x,$y,'Txt');
return $this->redirect(
array('controller' => 'Test', 'action' => 'test_new')
);
}
}

HtmlHelper not loaded in tutorial example

I followed the tutorial to create a blog for CakePHP 1.3 up to this step but keep getting error when running the app:
Notice (8): Undefined property: View::$Html [APP\views\posts\index.ctp, line 27]
Line 27:
echo $this->Html->link($post['Post']['title'], array('controller' => 'posts', 'action' => 'view', $post['Post']['id']));
Apparently CakePHP doesn't load the HtmlHelper class, I check over and over again in my controller, the Html should be loaded properly.
class PostsController extends AppController {
var $name = 'Posts';
var $helpers = array('Html', 'Form');
function index() {
$this->set('posts', $this->Post->find('all'));
}
}
When I added this line to the view (index.ctp), it works
$this->Html = &$this->loaded['html'];
But apparently I can't do that for every ctp file. I'm running Windows 7, WAMP 2, PHP 5.3.5, CakePHP 1.3.7 stable.
Anyone has a clue?
Like the comment by mtnorthrop above:
Is the FormHelper being loaded in your
views? What do you get if you do
pr($this->Html) in your view? How
about pr($html)? Until CakePHP 1.3,
helpers were accessed directly instead
of through the View object. In CakePHP
1.3 both methods should work. Does the plain $html->link() or $form->input()
methods work for you? – mtnorthrop 51
mins ago
From the book:
"The HtmlHelper is available in all
views by default. If you're getting an
error informing you that it isn't
there, it's usually due to its name
being missing from a manually
configured $helpers controller
variable."
You shouldn't need to specify it in your controllers. Perhaps somehow doing so is interfering with the core? BTW, you don't need to specify Form either.

Resources