All posts in CakePHP

Remove the default interface CakePHP links in default Cake apps?

Reformatting the default CakePHP layout (default.ctp)

Modify app/views/layouts/default.ctp

If no view exists in that location, check cake/libs/views/layout/default.ctp

Then copy the default.ctp like your layout.

Bellow is a sampel modification which we do for testing . This will remove the title and footer and default css of cake php template. You can modify it according to your need.


[smProductImageAdd src="http://learneveryday.net/codecanyon/adverticement/add_codecnayon_smart-social-share-php.png" alt= "Smart Social Share" href="http://codecanyon.net/item/php-smart-social-share/202558" title="Smart Social Share (Php Plugin)" description="Smart Social share is a php plugin . Which helps you to give user to share your content with social book mark site. There are 4 plugin in this package." ref="marifdu"] 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<?php echo $this->Html->charset(); ?>

	<?php
		echo $this->Html->meta('icon');

		echo $scripts_for_layout;
	?>
</head>
<body>
	<div id="container">

		<div id="content">

			<?php echo $this->Session->flash(); ?>

			<?php echo $content_for_layout; ?>

		</div>
	
	</div>
</body>
</html>

[smProductImageAdd src="http://learneveryday.net/codecanyon/adverticement/add_codecnayon_smart-social-share-php.png" alt= "Smart Social Share" href="http://codecanyon.net/item/php-smart-social-share/202558" title="Smart Social Share (Php Plugin)" description="Smart Social share is a php plugin . Which helps you to give user to share your content with social book mark site. There are 4 plugin in this package." ref="marifdu"]

Undefined variable: javascript in cakePHP with scriptaculous

Today i am trying to use ajax call in cakephp.

<?php  echo $ajax->submit('Submit', array('url'=> array('controller'=>'users', 'action'=>'add'), 'update' => 'testdiv')); ?>

I found this error:

Undefined variable: Javascript

After a couple of searching I found the following things can cause this problem:

1. You do not have the html, javascript and ajax helper defined in your own controller (eg. user_controller.php)

var $helpers = array('Html','Javascript','Ajax'); 

2. Your render action points to a view where no view file is existing for. ( so the view file HAS to be created!

$this->render('the_method','ajax'); 

Hope it helps somebody who is experiencing the same problem I had.

jquery UI tab in cake php

At first we have to copy all the JavaScript file inside D:\xampp\htdocs\cakephp\app\webroot\js
Here we use jquery and jquery UI javascript file and css file.
We now copy all the css file inside D:\xampp\htdocs\cakephp\app\webroot\css
Leter we will load this css and java script file.

Now Create a controller articles_controller.php inside D:\xampp\htdocs\cakephp\app\controllers
And write bellow code

<?php
class ArticlesController extends AppController {
	
var $uses=null;
var $name = 'Articles';
var $helpers = array('Html','Form','Javascript'); 


 function index() {
		   
          $this->set('page_heading', 'Jquery Tab');       

       }
}
?>

To load JavaScript helper we write this code

var $helpers = array('Html','Form','Javascript'); 

Create a folder articles (D:\xampp\htdocs\cakephp\app\views\articles) and inside it create a file index.ctp write this bellow code inside index.ctp

<?php
$javascript->link('jquery-1.4.2.min.js', false);
$javascript->link('ui/jquery.ui.core.js', false);
$javascript->link('ui/jquery.ui.widget.js', false);
$javascript->link('ui/jquery.ui.tabs.js', false);

?>
<link rel="stylesheet" href="<?php echo $this->webroot . 'css/'; ?>/jquery-ui.css" type="text/css" media="screen" />

 <script type="text/javascript">
           $(function() {
               $("#tabs").tabs();
           });
	</script>

<div id="tabs" class="ui-tabs-nav">


	<ul>
		<li><a href="#tabs-1">tabs-1</a></li>
		<li><a href="#tabs-2">tabs-2</a></li>
		<li><a href="#tabs-3">tabs-3</a></li>
	</ul>
	<div id="tabs-1">

        <p>Tab 1. This is demo text. This is demo text.This is demo text.This is demo text.This is demo text.This is demo text.This is demo text.This is demo text.This is demo text.</p>
	</div>
	<div id="tabs-2">
        <p>Tab 2. This is demo text.This is demo text.This is demo text.This is demo text.This is demo text.This is demo text.This is demo text.This is demo text.This is demo text.</p>
	</div>
	<div id="tabs-3">

        <p>Tab 3.This is demo text.This is demo text.This is demo text.This is demo text.This is demo text.This is demo text.This is demo text.This is demo text.This is demo text.</p>
	</div>
</div>

we write bellow code to load the css from webroot/css. May be there is other technique to load css but this is easy to load css in cake php.

<link rel="stylesheet" href="<?php echo $this->webroot . 'css/'; ?>/jquery-ui.css" type="text/css" media="screen" />

[smProductImageAdd src="http://learneveryday.net/codecanyon/adverticement/add_codecnayon_smart-social-share-php.png" alt= "Smart Social Share" href="http://codecanyon.net/item/php-smart-social-share/202558" title="Smart Social Share (Php Plugin)" description="Smart Social share is a php plugin . Which helps you to give user to share your content with social book mark site. There are 4 plugin in this package." ref="marifdu"]

Now the you run http://localhost:85/cakephp/articles you will see the bellow out put.

You can download the code use in this tutorial.

Cakephp: Creating and Sending Parameters Between Controller and View

Cakephp Step By Step Tutorial: We still wok with our first application at cakephp. But we will build controller and view better. Create parameters in controller and send it to view. It will make our application more flexible.

Create a new file books_controller.php within D:\xampp\htdocs\cakephp\app\controllers. Overwrite become like this:

<?php
    class BooksController extends AppController {
		
		var $Books = 'Books';
		var $uses=null;


       function index() {
		   
          $this->set('page_heading', 'Packt Book Store');       
          $book  = array (
                   'book_title'   => 'Object Oriented Programming  
                                                        with PHP5',
                   'author'     => 'Hasin Hayder',
                   'isbn'        => '1847192564',
                   'release_date' => 'December 2007'
                  );
		  
          $this->set($book);       
          $this->pageTitle = 'Welcome to the Packt Book Store!';
       }
    }
?>


Now create a folder books in side D:\xampp\htdocs\cakephp\app\views
And inside books create a file index.ctp. Rewrite with following code:

 <h2><?php echo $page_heading; ?></h2>
    <dl>
    <lh><?php echo $book_title; ?></lh>
    <dt>Author:</dt><dd><?php echo $author; ?></dd>
    <dt>ISBN:</dt><dd><?php echo $isbn; ?></dd>
    <dt>Release Date:</dt><dd><?php echo $release_date; ?></dd>
    </dl>

Point your browser to http://localhost:85/cakephp/books.

Hello, CakePHP! in Cake Php

CakePHP is an MVC framework. Model, view, controller are the main components. Generally speaking, the model is an object and is used to operate database. You should employ it to communicate with database, like inserting, deleting, querying. The view is as its name. It is used to display the result. Usually it contains lots of HTML but few PHP code. The controller is employed to handle HTTP requests and do some logic. The controller is central in an MVC pattern. It calls the model to get data from database, then do some work, and sends the result to a view to display.

Let’s have a look at CakePHP. I will use CakePHP to write a “’Hello, CakePHP! From index.’” program. My CakePHP is located in D:\xampp\htdocs\cakephp\. In it there are four directories: app, cake, docs, vendors. “app” is the working directory where we should save our code. Now let’s write some codes. Save this file as testers_controller.php in the folder ex:
D:\xampp\htdocs\cakephp\app\controllers\hellos_controller.php
Note that when we run this file from browser then we run this file as http://localhost/cakephp/hellos this will run normally the index function. IF we want to run the sayhello function then we have to write as http://localhost/cakephp/hellos/sayHello

<?php

class HellosController extends Controller{

	var $Hellos;
 	var $uses=null;
	
	var $autoRender=false;	

 function index(){

    echo 'Hello, CakePHP! From index.';

  }
  function sayHello(){

    echo 'Hello, CakePHP!';

  }

}

?>

As you see, the name of the controller class is HellosController. i.e. The controller’s name is Hello. According to the CakePHP convention, the controller’s name should be plural form. You must save it as “hellos_controller” in app\controllers so that CakePHP can find the controller class.

Now run the index function

Now run the sayHello function