Home > cakePHP, PHP > PHP – cakephp cheatsheet tips & tricks nuts & bolts

PHP – cakephp cheatsheet tips & tricks nuts & bolts

CakePHP smart cheats

Default/Front page:
===================

1. app/views/layouts/default.ctp == you put the page layout

include 'app/webroot/files/slide.ctp';
echo $content_for_layout;

2. app/views/pages/home.ctp == $content_for_layout
body …. texts…. end of body

Controller:
=================

// direcotry: app/controllers/contact_controller.php
class ContactController extends AppController {

// no module will be used, ignore that
var $uses = null; // null means no   app/models/filename.php will used
#var $uses = array(‘Model1′,’Model2’); // if we uncomment this it will look inside app/models/model1.php and model2.php

// —–
//url: http://localhost/contact/
//direcotry: app/views/contact/index.
// —–
function index(){

# $this->Model1->myFunction(); // we can access the models
}

// —–
//visit: http://localhost/contact/login
//directory: app/views/contact/login.ctp
// —–
function login(){
}

}

Elements
======
app/views/elements/aname.ctp
<form action=’/var/html…’>
</form>

to use this  elements inside the views > echo $this->element(‘aname’);

1. How to access sessions?

to use from controllers/model: $this->Session->write(‘language’, $language); $reader = $this->Session->read(“Reader”);

to use from views: pr($session); pr($session->read());

2. How to redirect?

$this->redirect(‘/china_or_japan’);

exit;

3. How to access module/custom funciton/ schema?

// app/models/request.ctp

class Request extends AppModel {

var $name = ‘Request’;
var $useTable = false;  // false means no database we will be using, true or name will mean use that table.

function myName($value){

return $value . ‘ went to model, and model did the processing, and he said that everything is fine.’;

}

}

from controller/views  we can access this

$go_and_check_model = $this->Request->myName(‘Shamun’);
$this->set(‘requests’, $go_and_check_model ); // so we used a model ? got it!

4. How to route?

# routes file to use
# app/config/routes.php

# visitor came:  http://www.mydomain.com/nopage/                          == default

# visitor routed: http://www.mydomain.com/existing/page                  == i want

# Router::connect( pattern_to_search, array( app/controller/existing_controller.php , function_name, prefix ) );

Router::connect(‘/nopage/’, array(‘controller’ => ‘existing’, ‘action’ => ‘page’));

5.

Advertisements
Categories: cakePHP, PHP
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: