CakePHP & DIEVOLUTION Blog
CakePHP Guestbook
Immer wieder Sonntags: Leere. Der einzige wirklich freie Tag der Woche, das Blog ist leer, der Kopf ist leer, und irgendwie fällt mir kein Thema ein, was euch interessieren könnte. Daher gibts heute etwas ganz einfach, nämlich eine kleine Guestbook Anwendung mit CakePHP.
Dieses Guestbook könnt ihr wenn ihr möchtet verwenden - ein Link zum Cakeblog ist natürlich gern gesehen
Features:
- Einfache Installation
- Captcha Prüfung
Benötigt werden zudem folgende Komponenten:
Zuerst die dafür nötige SQL Tabelle:
CREATE TABLE `guestbook_entries` (
`id` int(10) unsigned NOT NULL auto_increment,
`user_id` int(10) unsigned NOT NULL default '0',
`author` varchar(255) default NULL,
`entry` text,
`created` datetime default NULL,
`modified` datetime default NULL,
PRIMARY KEY (`id`)
)
Die Modeldatei guestbook_entry.php
<?php
class GuestbookEntry extends AppModel {
var $name = 'GuestbookEntry';
var $validate = array(
'name' => VALID_NOT_EMPTY,
'content' => VALID_NOT_EMPTY
);
}
?>
Die Controllerdatei guestbook_entries_controller.php
<?php
class GuestbookEntriesController extends AppController {
var $name = 'GuestbookEntries';
var $helpers = array('Html', 'Form', 'Deutsch', 'Pagination');
var $components = array('Captcha', 'Pagination');
function index() {
$criteria=NULL;
list($order,$limit,$page) = $this->Pagination->init($criteria);
$this->set('guestbookEntries', $this->GuestbookEntry->findAll($criteria,NULL,"GuestbookEntry.created DESC", $limit, $page));
}
function captcha()
{
$this->Captcha->render();
}
function view($id = null) {
if(!$id) {
$this->Session->setFlash('Invalid id for Guestbook Entry.');
$this->redirect('/guestbook_entries/index');
}
$this->set('guestbookEntry', $this->GuestbookEntry->read(null, $id));
}
function add() {
if(empty($this->data)) {
} else {
$captcha = $this->Session->read("captcha");
if ($captcha == $this->data['GuestbookEntry']['captcha']) {
$this->data['GuestbookEntry']['name'] = htmlentities($this->data['GuestbookEntry']['name']);
if($this->GuestbookEntry->save($this->data)) {
$this->Session->setFlash('The Guestbook Entry has been saved');
$this->redirect('/guestbook_entries/index');
} else {
$this->Session->setFlash('Please correct errors below.');
}
}
}
}
function edit($id = null) {
if(empty($this->data)) {
if(!$id) {
$this->Session->setFlash('Invalid id for Guestbook Entry');
$this->redirect('/guestbook_entries/index');
}
$this->data = $this->GuestbookEntry->read(null, $id);
$this->set('users', $this->GuestbookEntry->User->generateList());
} else {
$this->cleanUpFields();
if($this->GuestbookEntry->save($this->data)) {
$this->Session->setFlash('The GuestbookEntry has been saved');
$this->redirect('/guestbook_entries/index');
} else {
$this->Session->setFlash('Please correct errors below.');
$this->set('users', $this->GuestbookEntry->User->generateList());
}
}
}
function delete($id = null) {
if(!$id) {
$this->Session->setFlash('Invalid id for Guestbook Entry');
$this->redirect('/guestbook_entries/index');
}
if($this->GuestbookEntry->del($id)) {
$this->Session->setFlash('The Guestbook Entry deleted: id '.$id.'');
$this->redirect('/guestbook_entries/index');
}
}
}
?>
Nun folgen noch die views, gepackt in den Ordner views/guestbook_entries
index.thtml
<?php $pagination->setPaging($paging); ?>
<div class="guestbookEntries">
<h2>Guestbook</h2>
<h3>Spammers will be shot!</h3>
<?php echo $html->link($html->image("neu.gif"), '/guestbook_entries/add', NULL,NULL,FALSE); ?>
<? echo $this->renderElement('pagination');
$i=0;?>
<table cellpadding="0" cellspacing="0" class="gbook_table">
<?php foreach ($guestbookEntries as $guestbookEntry):
$gbook = ($i++%2 == 0) ? "gbook_class1" : "gbook_class2";
echo "<tr class=\"".$gbook."\">";
?>
<td rowspan="3" class="gbook_number"><?php echo $guestbookEntry['GuestbookEntry']['id']; ?></td>
<td colspan="2"><?php echo $guestbookEntry['GuestbookEntry']['name']; ?> schrieb am <?php echo $deutsch->de_date($guestbookEntry['GuestbookEntry']['created'],2); ?> </td>
<?php echo "</tr><tr class=\"".$gbook."\">";?>
<td colspan="2"><?php echo $guestbookEntry['GuestbookEntry']['content']; ?></td>
<?php echo "</tr><tr class=\"".$gbook."\">";?>
<td nowrap>
<?php echo $html->link('Bearbeiten','/guestbook_entries/edit/' . $guestbookEntry['GuestbookEntry']['id'])?>
<?php echo $html->link('Löschen','/guestbook_entries/delete/' . $guestbookEntry['GuestbookEntry']['id'], null, 'Are you sure you want to delete id ' . $guestbookEntry['GuestbookEntry']['id'])?>
</td>
</tr>
<?php endforeach; ?>
</table>
<? echo $this->renderElement('pagination');?>
</div>
edit.thtml
<h2>Edit Guestbook Entry</h2>
<form action="<?php echo $html->url('/guestbook_entries/edit/'.$html->tagValue('GuestbookEntry/id')); ?>" method="post">
<div class="required">
<?php echo $form->labelTag('GuestbookEntry/user_id', 'User');?>
<?php echo $html->selectTag('GuestbookEntry/user_id', $users, $html->tagValue('GuestbookEntry/user_id'), array(), array(), false);?>
<?php echo $html->tagErrorMsg('GuestbookEntry/user_id', 'Please select the User.') ?>
</div>
<div class="required">
<?php echo $form->labelTag('GuestbookEntry/author', 'Author');?>
<?php echo $html->input('GuestbookEntry/author', array('size' => '60'));?>
<?php echo $html->tagErrorMsg('GuestbookEntry/author', 'Please enter the Author.');?>
</div>
<div class="required">
<?php echo $form->labelTag( 'GuestbookEntry/entry', 'Entry' );?>
<?php echo $html->textarea('GuestbookEntry/entry', array('cols' => '60', 'rows' => '10'));?>
<?php echo $html->tagErrorMsg('GuestbookEntry/entry', 'Please enter the Entry.');?>
</div>
<?php echo $html->hidden('GuestbookEntry/id')?>
<div class="submit">
<?php echo $html->submit('Save');?>
</div>
</form>
add.thtml
<h2>Guestbook</h2>
<form action="<?php echo $html->url('/guestbook_entries/add'); ?>" method="post">
<fieldset><legend>Eintrag vornehmen</legend>
<p>Bitte einen Autor und einen Text eingeben. Danach noch das Captcha richtig schreiben und dein Eintrag wird gespeichert</p>
<?php echo $html->tagErrorMsg('GuestbookEntry/name', 'Autor eingeben');?>
<p>Autor:<br><?php echo $html->input('GuestbookEntry/name', array('size' => '40'));?></p>
<?php echo $html->tagErrorMsg('GuestbookEntry/content', 'Please enter the Entry.');?>
<p>Deine Nachricht:<br><?php echo $html->textarea('GuestbookEntry/content', array('cols' => '30', 'rows' => '10'));?></p>
<p>Wenn du das Captcha nicht lesen kannst, lade die Seite neu, dein Text bleibt gespeichert.</p>
<p><img src="<?php echo $html->url('/guestbook_entries/captcha'); ?>" /><br>
<?php echo $html->input("GuestbookEntry/captcha", array('style' => 'width: 116px'));?></p>
<div class="submit">
<?php echo $html->submit('Kommentar hinzufügen', array("class"=>"formsubmit"));?>
</div>
</fieldset>
</form>
<ul class="actions">
<li><?php echo $html->link('Zurück zum Guestbook', '/guestbook_entries/index')?></li>
</ul>
Dies sollte genügen, um ein funktionierendes Guestbook auf euren CakePHP Seiten zu erstellen.
Hoffe ich zumindest, schließlich ist ja Sonntag
PS: Ich suche derzeit dringend nach einem guten Code Einfüge Plugin…
Veröffentlicht am Sonntag, den 13. Mai 2007 um 16:54 Uhr veröffentlicht
Du kannst einen Kommentar schreiben, oder einen Trackback auf deiner Seite einrichten.
2 Reaktionen zu “CakePHP Guestbook”
-
Am 21. Mai 2007 um 11:33 Uhr
Nett gemacht - bin auch gerade in CakePHP eingestiegen und nach schlechten Erfahrungen mit CodeIgniter sehr begeistert.
-
Am 20. Juni 2007 um 12:07 Uhr
Great cakephp tutorial, thanks
Muy buen tutorial, gracias
Großer cakephp Tutorenkurs, vielen dank
Greetings from León (Spanien)
Auf einen Blick
Archiv
- November 2008
- Oktober 2008
- August 2008
- Juli 2008
- Juni 2008
- Mai 2008
- April 2008
- März 2008
- Februar 2008
- Januar 2008
- Dezember 2007
- November 2007
- Oktober 2007
- September 2007
- August 2007
- Juli 2007
- Juni 2007
- Mai 2007
- April 2007
Kategorien
- Allgemein (72)
- Cake vs Rails (3)
- CakePHP (42)
- Meinung (16)
- Projekt (6)
- Rails (6)
- Tipps (28)
- Typo3 (4)
Letzte Einträge:
- 11.04.2007: Neustart
- 11.04.2007: Design Version 1 steht
- 11.04.2007: Blog