Posts Tagged ‘CakePHP’
Programming in CakePHP Way
Set default language
Add this code at the top of webroot/index.php:
define('DEFAULT_LANGUAGE', 'chi');
Note: add it to config/bootstrap.php will not working.
Database fields
CakePHP can do some work automatically on these fields:
id int(10) auto_increment
tablex_id int(10)
created datetime
modified datetime
HTML output
Display a HTML link:
<?php echo $html->link(sprintf(__('List %s', true), __('Table1', true)), array('action'=>'index')); ?>
CakePHP Layout/Template
Template for main page:
app/views/pages/home.ctp
Layout for main page:
app/view/layouts/default.ctp
CSS styles:
app/webroot/css
Make Web Site Using CakePHP
Despite CakePHP already provided the powerful Scaffolding feature and the Bake code generator, also we need to understand the following ways to finish some site make tasks:
Display a form
<?php echo $html->formTag('/datas/function') ?>
<lable>Field1:</label>
<?php echo $html->inputTag('Data/field1', array('size' => '40')) ?>
<?php echo $html->tagErrorMsg('Data/field1', 'field1 is required') ?>
<lable>Field2:</label>
<?php echo $html->inputTag('Data/field2', array('size' => '255')) ?>
<?php echo $html->tagErrorMsg('Data/field2', 'field2 is invalid') ?>
<?php echo $html->submitTag('function') ?>
</form>
Data validation
var $validate = array (
'field1' => VALID_NOT_EMPTY,
'field2' => '/^.{6,40}$/',
'field3' => VALID_EMAIL
);
Save form data to database
if (!empty($this->data)) {
if ($this->Data->validates($this->data)) {
$this->Data->save($this->data);
$this->flash('Success messages');
} else {
$this->flash('Error messages');
}
}
Display a table
<table>
<?php
echo $html->tableHeaders(array_keys($datas[0]['Data']));
foreach ($datas as $data) {
echo $html->tableCells($data['Data'], array(’bgcolor’ => ‘#ddd’));
}
?>
</table>
Convert Joomla Template into CakePHP Layout
Copy Template Files
1. Copy Joomla Template’s css/* file to your CakePHP’s webroot/css directory.
2. Copy Joomla Template’s images directory to your CakePHP’s webroot directory
3. Copy Joomla Template’s index.php file to your CakePHP’s views/layout/default.ctp
Modify the default.ctp to meet CakePHP’s requirements
1. Replace:
<?php mosShowHead(); ?>
With:
<title><?php echo $title_for_layout;?></title>
2. Replace $mosConfig_live_site with $this->webroot
3. Replace mosLoadModules with $this->renderElement
4. Replace:
mosMainBody();
With:
echo $content_for_layout;