You are on page 1of 8

Cake PHP

Install Guide
1. A http://cakephp.org/ honlaprl letltjk a legjabb verzit, amely jelen esetben a 2.2.3
verziszmmal rendelkezik
2. A xampp/htdocs mappban ltrehozunk egy j mappt pldul cake nven, majd a letlttt
fjlokat kitmrtjk ebbe a mappba
3. A xampp-ban mdostjuk a httpd.conf fjt az albbi mdon:
Megkeressk a mod_rewrite sort s kivesszk elle a kommentet
4. A phpMyAdmin-ban ltrehozunk egy j adatbzist pldul cake_blog_tutorial nven, utf8
kdolssal
5. Az app/Config mappban tallhat database.php.default fjlunkat tnevezzk
database.php-ra
6. Megnyitjuk a database.php s mdostjuk benne a kvetkez sorokat:
login => root,
password => ,
database => cake_blog_tutorial, (egyeznie kell a korbban ltrehozott adatbzis nevvel)
7. Megnyitjuk az app/Config mappban tallhat core.php fjt s rkeresnk az albbi
sorokra:
Configure::write('Security.salt', 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi');
A msodik idzjelek kztt tallhat szm s betsort tetszleges helyen kiegsztjk egy j
szmmal vagy karakterrel
Ha ez megtrtnt megkeressk a kvetkez sort:
Configure::write('Security.cipherSeed', '7485712659625147843639846751');
Itt szintn a msodik idzjelek kztt kifejezst fogjuk mdostani, mg pedig gy hogy
kiegsztjk tetszleges helyen egy j szmmal
8. Ksz is vagyunk a Cake PHP teleptsvel, amelyet a localhost/cake/ paranccsal el is
tudnunk rni a bngszben
Creating the Blog Database
9. A korbbiakban ltrehozott cake_blog_tutorial adatbzisban ltrehozunk egy j tblt az
albbi SQL kd segtsgvel:
CREATE TABLE posts (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(50),
body TEXT,
created DATETIME DEFAULT NULL,
modified DATETIME DEFAULT NULL
);
10. Az elzleg ltrehozott posts tblnkat feltltjk a kvetkez sorokkal:
INSERT INTO posts (title,body,created)
VALUES ('Elso post', 'Ez az elso post tartalma.', NOW());
INSERT INTO posts (title,body,created)
VALUES ('Masodik post', 'Ez a masodik post tartalma', NOW());
INSERT INTO posts (title,body,created)
VALUES ('Harmadik post', 'Ez a harmadik post tartalma', NOW());
Create a Post Model
11. Az app/Modell mappban ltrehozunk egy j fjt Post.php nven s feltltjk a
kvetkez sorokkal
<?php
class Post extends AppModel {
}
Create a Posts Controller
12. Az app/Controller mappban ltrehozunk egy j fjlt PostsController.php nven
<?php
class PostsController extends AppController {
public $helpers = array('Html', 'Form');

public function index() {
$this->set('posts', $this->Post->find('all'));
}
}
Creating Post Views
13. Az app/View mappban ltrehozunk egy j mappt Posts nven
14. Az elzleg ltrehozott Posts mappban ltrehozunk egy j fjt index.ctp nven s
feltltjk az albbi sorokkal:
<h1>Blog posts</h1>
<table>
<tr>
<th>Id</th>
<th>Title</th>
<th>Created</th>
</tr>
<?php foreach ($posts as $post): ?>
<tr>
<td><?php echo $post['Post']['id']; ?></td>
<td>
<?php echo $this->Html->link($post['Post']['title'],
array('controller' => 'posts', 'action' => 'view', $post['Post']['id'])); ?>
</td>
<td><?php echo $post['Post']['created']; ?></td>
</tr>
<?php endforeach; ?>
<?php unset($post); ?>
</table>
15. Az app/Controller mappban tallhat PostsController.php fjlt kiegsztjk az albbi
metdossal:
public function view($id = null) {
$this->Post->id = $id;
$this->set('post', $this->Post->read());
}
16. Az app/View/Posts mappban ltrehozunk egy j fjt view.ctp nven
<h1><?php echo h($post['Post']['title']); ?></h1>
<p><small>Created: <?php echo $post['Post']['created']; ?></small></p>
<p><?php echo h($post['Post']['body']); ?></p>
Adding Posts
17. Az app/Controller mappban tallhat PostsController.php fjlunkat mdostjuk az
albbi mdon:
Eredeti sor:
public $helpers = array('Html', 'Form');
Mdostott sor:
public $helpers = array('Html', 'Form', 'Session');
public $components = array('Session');
Ha ez megtrtnt az albbi mdon mdostjuk a PostsController.php fjlunkat
Eredeti sor:
public function view($id = null) {
Mdostott sor:
public function view($id) {
Majd kiegsztjk egy jabb metdussal:
public function add() {
if ($this->request->is('post')) {
$this->Post->create();
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash('Your post has been saved.');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('Unable to add your post.');
}
}
}
Data Validation
18. Az app/View/Posts mappban ltrehozunk egy j fjlt add.ctp nven
<h1>Add Post</h1>
<?php
echo $this->Form->create('Post');
echo $this->Form->input('title');
echo $this->Form->input('body', array('rows' => '3'));
echo $this->Form->end('Save Post');
?>
19. Az app/Model mappban tallhat Posts.php fjlt mdostjuk az albbi mdon:
<?php
class Post extends AppModel {
public $validate = array(
'title' => array(
'rule' => 'notEmpty'
),
'body' => array(
'rule' => 'notEmpty'
)
);
}
20. Az albbi sorral kiegsztjk az app/View/Posts mappban tallhat index.ctp fjlunkat (a
<table> kd el kerl ez az j sor)
<?php echo $this->Html->link('Add Post', array('controller' => 'posts', 'action' => 'add'));
?>
Editing Posts
21. Az app/Controller mappban tallhat PostsController.php fjlunkat kiegsztjk egy
jabb metdussal
public function edit($id = null) {
$this->Post->id = $id;
if ($this->request->is('get')) {
$this->request->data = $this->Post->read();
} else {
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash('Your post has been updated.');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('Unable to update your post.');
}
}
}
22. Ltrehozunk egy j fjlt edit.ctp nven az app/View/Posts mappban
<h1>Edit Post</h1>
<?php
echo $this->Form->create('Post', array('action' => 'edit'));
echo $this->Form->input('title');
echo $this->Form->input('body', array('rows' => '3'));
echo $this->Form->input('id', array('type' => 'hidden'));
echo $this->Form->end('Save Post');
23. Az index.ctp fjlunkat az app/View/Posts mappban mdostjuk az albbi mdon:
Eredeti sor:
<?php echo $this->Html->link("Add Post", array('action' => 'add')); ?>
Mdostott sor:
<p><?php echo $this->Html->link("Add Post", array('action' => 'add')); ?></p>
<th>Title</th>
<th>Action</th> (ezzel az j sorral egsztjk ki a korbbi kdunkat)
<th>Created</th>
Eredeti sor:
<td>
<?php echo $this->Html->link($post['Post']['title'],
array('controller' => 'posts', 'action' => 'view', $post['Post']['id'])); ?>
</td>
Mdostott sor:
<td>
<?php echo $this->Html->link($post['Post']['title'], array('action' => 'view',
$post['Post']['id'])); ?>
</td>
Az elzleg mdostott sor al beszrjuk a kvetkez sorokat:
<td>
<?php echo $this->Html->link('Edit', array('action' => 'edit', $post['Post']['id'])); ?>
</td>
Az albbi sort pedig eltvoltjuk:
<?php unset($post); ?>
Deleting Posts
24. Az app/Controller mappban tallhat PostsController.php fjlunkat kiegsztjk egy
jabb metdussal
public function delete($id) {
if ($this->request->is('get')) {
throw new MethodNotAllowedException();
}
if ($this->Post->delete($id)) {
$this->Session->setFlash('The post with id: ' . $id . ' has been deleted.');
$this->redirect(array('action' => 'index'));
}
}
25. Az index.ctp fjlunkat az app/View mappban mdostjuk az albbi mdon:
Ez utn a sor utn:
<td>
<?php echo $this->Html->link('Edit', array('action' => 'edit', $post['Post']['id']));
?>
Beszrjuk a kvetkez sorokat:
<?php echo $this->Form->postLink(
'Delete',
array('action' => 'delete', $post['Post']['id']),
array('confirm' => 'Are you sure?'));
?>
</td>
Routes
26. Megnyitjuk az app/Config mappban tallhat routes.php fjl, majd megkeressk az
albbi sort s kikommenteljk (// jelek segtsgvel)
Eredeti sor:
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
Mdostott sor:
//Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
Ha ez megtrtnt az elz sorunk al beillesztjk a kvetkez kdot:
Router::connect('/', array('controller' => 'posts', 'action' => 'index'));
Users
27. A cake_blog_tutorial adatbzisunkban ltrehozunk egy tblt az albbi SQL kd
segtsgvel:
CREATE TABLE users (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50),
password VARCHAR(50)
);
28. Az elzleg ltrehozott users tblt feltltjk az albbi SQL kddal
INSERT INTO users (username,password)
VALUES ('Jozsi', 'titkos');
INSERT INTO users (username,password)
VALUES ('Joe', 'secret');
INSERT INTO users (username,password)
VALUES ('Dzso', 'titkosabb');
29. Ltrehozunk egy krnyezeti vltozt az albbi mdon
Vezrlpult - Rendszer - Specilis rendszerbelltsok - Specilis - Krnyezeti vltozk
A rendszervlotzknl kikeressk a Path-ot, majd hozzadjuk a kvetkez kt sort:
C:\xampp\php\;
C:\xampp\htdocs\cake\app\Console\;
30. Elindtjuk a konzolt a cmd parancs segtsgvel, majd berjuk az albbi parancsokat
cd c:\xampp\htdocs\cake\app
cake help
cake bake all
31. A megjelen listbl kivlasztjuk, hogy mely modelleket szeretnnk a gp ltal
legenerltatni, amely jelen esetben a User
32. Ha mindent jl csinltunk el is kszlt a Users-hez a Model, a View s Controller
fjljaink
33. Immron el tudjunk rni a bngszben a Users felletet a localhost/cake/users
paranccsal
Static Pages
34. Lehetsgnk van statikus oldalak ltrehozsra is, amely az albbi mdon trtnik
35. Ltrehozunk egy j fjt tetszleges nven (pldul testpage.ctp) az app/View/Pages
mappban, amit feltltnk az albbi egyszer kddal
<?php echo 'Test page <br><br> Teszt oldal <br>....!!!'; ?>
36. A bngszben a localhost/cake/pages/testpage paranccsal el is elrhet a ltrehozott
statikus oldalunk
CSS
37. Lehetsgnk van sajt CSS fjl hasznlatra is
38. A sajt CSS fjlunkat az app/webroot/css mappba kell elhelyezni tetszleges nven
39. A CSS fjlt alakalmazni pedig az albbi mdon lehet:
Megnyitjuk az app/View/Layouts mappban tallhat default.ctp fjlt s megkeressk benne
a kvetkez sort:
echo $this->Html->css(' ');
Az idz jelekben pedig megadjunk a CSS fjlunk nevt pldul style (a .css kiterjesztst nem
szksges megadni)

You might also like