Archive

Posts Tagged ‘Cakephp’

Javascript in CakePHP 1.3

July 8th, 2010

The newly updated version of CakePHP in version 1.3 packs a punch! It is now more easier to code as the Helper methods are better organized now.

For instance, linking external javascript in CakePHP 1.2 is quite an hassle as Javascript itself has Javascript Helper. However, The Javascript Helper is deprecated in 1.3 and will be removed in future versions of CakePHP. Henceforth, lets look at how HTML Helper load javascript.

In CakePHP 1.2, to load an external javascript, we do this:

<?php echo $javascript->link("jquery", true); ?>

Now in CakePHP 1.3, we can make use of HTML Helper, we do the following:

<?php echo $html->script("jquery"); ?>

Nevertheless, if you have multiple external js file to load, you can load it into an array:

<?php echo $html->script(array('jquery','wysiwyg','scripts')); ?>

Easy and more organized now eh? External CSS files were all along being load using HTML Helper. Now with the loading of Js files through HTML helper instead of invoking Javascript Helper, it is much more convenient! Moreover, you will only need to load one helper which is HTML Helper instead of loading 2. :D

Willie Programming , , ,

CakePHP 1.2 favicon

February 23rd, 2009

In CakePHP 1.2 default layout, favicon are linked through:

1
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">

The problem with this is that when you navigate to other views (except for the default controller and view it loads), the favicon will disappear. This is because the path to the favicon is lost.

There is also a mistake in CakePHP 1.2 Manual at here

The $html->meta has the wrong values fed into it and the parameters doesn’t match what the API desribe at here.

Hence, following the manual in CakePHP 1.2 will not works in displaying the favicon correctly. If we were to use $html->meta parameters as what it was describe in the API like below:

1
<?= $html->meta('icon', /favicon.ico'); ?>

We will get:

1
<link href="/favicon.ico" type="image/x-icon" rel="icon" />

The correct path is still not being generated !

To resolve this, we made use of $html->url from CakePHP 1.1 :

1
<?= $html->meta('icon', $html->url('/favicon.ico')); ?>

Now, the path of the favicon will be generated correctly and displayed in all views when using CakePHP 1.2

Willie Programming , ,

Controller without Model in CakePHP

February 11th, 2009

Sometimes, we may have a controller that doesn’t required a model in CakePHP. However if there is no Model with a singular name correspond to the controller, CakePHP will return a error of no Model being found.

To resolve this, define the controller variable of uses:

var $uses = null; //declare that this controller has no model

In this way, the error will not be shown because we have specify that the controller does not have any model.

An overview example are as follow:

1
2
3
4
5
6
7
8
9
10
11
12
<?php
class CompaniesController extends AppController {
   var $name = 'Companies';
   var $uses = null; //declare that this controller has no model
   var $layout = 'default';
   var $helpers = array('Html', 'Javascript', 'Time', 'Ajax');
 
   function about_us() {
      $this->pageTitle = "About Us";//will load the view, about_us.ctp
   }
}
?>

Hope this helps!

Willie Programming , ,