HtmlHelper not loaded in tutorial example - cakephp

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.

Related

CakePHP - Class ** cannot be found

I have a strange problem with CakePHP
CakePHP gives an error on the following line:
View/Designer/cards:
$this->JsBridge->set('Card.DISPLAY_TYPE_FOLDER_GREETING', Card::DISPLAY_TYPE_FOLDER_GREETING);
Class 'card' can not be found.
However in DesignersController I load the model Card via de following line:
public $uses = array('Designer', 'Card');
If I add the following line in the top of DesignersController
App::uses('Card', 'Model');
The page loads, but the following line does not work:
$this->paginate = $this->Card->getPagination($filter);
I have put the code for the model Card.php here : http://pastebin.com/U7zxKHCx
Can you tell me what is going wrong?
Thank you!
Are you including the CardModel in your Controller?
$uses = array('....','Card',....);
Controller properties, classes attached etc. are NOT directly accessible in a View. You will need to set this constant Card::DISPLAY_TYPE_FOLDER_GREETING to a variable:
$this->set('variableName1', Card::DISPLAY_TYPE_FOLDER_GREETING);
Then use it in the View:
$this->JsBridge->set('Card.DISPLAY_TYPE_FOLDER_GREETING', $variableName1);

CakePHP 2 : Implement CakePDF plugin by Ceeram

I'm trying to implement the CakePDF plugin designed by Ceeram.
I'm using CakePHP 2 and I work locally using wamp on windows vista. I followed everything from the readme file but I got stucked at some point.
What I'd firstly like to do is converting an HTML link to a PDF using the WkHtmlToPdf engine. I see many people having issues to make it work so I'm gonna detail all the way through in the following different steps.
STEP 1: CakePdf plugin by Ceeram
I downloaded the plugin at https://github.com/ceeram/CakePdf
I extracted the contained folder into app/Plugin/CakePdf
STEP 2: Bootstrap
I added the following lines - app/Config/bootstrap.php :
CakePlugin::load('CakePdf', array('bootstrap' => true, 'routes' => true));
Configure::write('CakePdf', array(
'engine' => 'CakePdf.WkHtmlToPdf'
),
'orientation' => 'portrait',
'download' => true
));
STEP 3: Controller
I've created my controller "InvoicesController.php" - app/Controller/InvoicesController.php:
class InvoicesController extends AppController {
public $components = array('RequestHandler');
public function view($id = null) {
$this->Invoice->id = $id;
if (!$this->Invoice->exists()) {
throw new NotFoundException(__('Invalid invoice'));
}
$this->pdfConfig = array(
'orientation' => 'portrait',
'filename' => 'Invoice_' . $id
);
$this->set('invoice', $this->Invoice->read(null, $id));
}
}
STEP 4: View
I've created a pdf folder in my view folder and created view.ctp in app/View/Invoices/pdf/view.ctp.
STEP 5: Layout
I've created a pdf folder in my layout folder and created app/View/Layouts/pdf/default.ctp
That's about it. In my view I couldn't make a PDF file from an URL.
Although I have to mention I'm new in OOP and CakePHP so I would be very grateful if you could show me how to have this done. I'm sure it will help others because there many newbies like me who want to do this but because it's all meant for advanced programmers we cannot figure out how to put the pieces together.
Thank you very much for your understanding and your help!
[THIS POST IS MODIFIED EACH TIME THERE IS A NEW ANSWER WHICH IMPROVES IT]
You need to add RequestHandler component, and browse to localhost/invoices/view/1.pdf
Looks like i had forgotten to mention to add RequestHandler component in the readme.
Also for WkHtmlToPdf you need to tell it where it can find it, and since you are on windows the default location surely wont work for you. You can set the location with Configure::write('CakePdf.binary', 'C:\Program Files\wkhtmltopdf\wkhtmltopdf.exe') after having installed it on windows
You are missing this code in app/config/routers.php
Router::parseExtensions();
Router::setExtensions(array('json', 'xml', 'rss', 'pdf'));
details available on:
http://www.dereuromark.de/2014/04/08/generating-pdfs-with-cakephp/

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');

CakePHP 2.0: Error on loading an image with the html-helper

I've switched to the newest Version of CakePHP, but when I was about to build up my layout, I got this error:
Fatal error: Call to a member function image() on a non-object in C:\xampp\htdocs\propfe\app\View\Layouts\default.ctp on line 91
of course I've already included the helper in lib/Cake/Controller/AppController.php:
public $helpers = array('Session','Html','Js');
and although it's improbable, the error is in this line, here's no 91 of the layout:
<?php echo $this->html->image('aesculap.jpg'); ?>
You have to use $this->Html instead of $this->html.
You have the helper defined in AppController, but does your controller that is loading this view extend AppController or Controller.

Setting up magic routes for plugins in CakePHP 1.3?

I'm working on upgrading my project from CakePHP 1.2 to 1.3. In the process, it seems that the "magic" routing for plugins by which a controller name (e.g.: "ForumsController") matching the plugin name (e.g.: "forums") no longer automatically routes to the root of the plugin URL (e.g.: "www.example.com/forums" pointing to plugin "forums", controller "forums", action "index").
The error message given is as follows:
Error: ForumsController could not be found.
Error: Create the class ForumsController below in file: app/controllers/forums_controller.php
<?php
class ForumsController extends AppController {
var $name = 'Forums';
}
?>
In fact, even if I navigate to "www.example.com/forums/forums" or "www.example.com/forums/forums/index", I get the same exact error.
Do I need to explicitly set up routes to every single plugin I use? This seems to destroy a lot of the magic I like about CakePHP. I've only found that doing the following works:
Router::connect('/forums/:action/*', array('plugin' => 'forums', 'controller' => 'forums'));
Router::connect('/forums', array('plugin' => 'forums', 'controller' => 'forums', 'action' => 'index'));
Setting up 2 routes for every single plugin seems like overkill, does it not? Is there a better solution that will cover all my plugins, or at least reduce the number of routes I need to set up for each plugin?
I guess, that topic Configuration-and-application-bootstrapping covers that:
App::build(array(
'plugins' => array('/full/path/to/plugins/', '/next/full/path/to/plugins/')
));
Also take a look at this ticket: http://cakephp.lighthouseapp.com/projects/42648/tickets/750-plugin-route-problem-when-acl-and-auth-components-used#ticket-750-5 (Cake 1.3 had removed magic plugin routes).
You don't have myplugin_app_controller.php in your /app/plugins/myplugin directory.
Just create a file containing following:
<?php
class MypluginAppController extends AppController {
}
?>
And you will have all your plugin's features. :)

Resources