blog tutorial - pakistandasti.files.wordpress.com · what you’ll need 2 a running web server a...

28
CAKEPHP Blog tutorial

Upload: others

Post on 01-Nov-2019

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Blog tutorial - pakistandasti.files.wordpress.com · what you’ll need  2 A running web server A database server Basic PHP knowledge

CAKEPHP

Blog tutorial

Page 2: Blog tutorial - pakistandasti.files.wordpress.com · what you’ll need  2 A running web server A database server Basic PHP knowledge

what you’ll need

http://book.cakephp.org/2.0/en/tutorials-and-

examples/blog/blog.html

2

A running web server

A database server

Basic PHP knowledge

basic knowledge of the MVC

Page 3: Blog tutorial - pakistandasti.files.wordpress.com · what you’ll need  2 A running web server A database server Basic PHP knowledge

Database

http://book.cakephp.org/2.0/en/tutorials-and-examples/blog/blog.html 3

/* First, create our posts table: */

CREATE TABLE posts (

id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,

title VARCHAR(50),

body TEXT,

created DATETIME DEFAULT NULL,

modified DATETIME DEFAULT NULL

);

/* Then insert some posts for testing: */

INSERT INTO posts (title,body,created)

VALUES ('The title', 'This is the post body.', NOW());

INSERT INTO posts (title,body,created)

VALUES ('A title once again', 'And the post body follows.', NOW());

INSERT INTO posts (title,body,created)

VALUES ('Title strikes back', 'This is really exciting! Not.', NOW());

Page 4: Blog tutorial - pakistandasti.files.wordpress.com · what you’ll need  2 A running web server A database server Basic PHP knowledge

First ugly Look

http://book.cakephp.org/2.0/en/tutorials-and-

examples/blog/blog.html

4

Page 5: Blog tutorial - pakistandasti.files.wordpress.com · what you’ll need  2 A running web server A database server Basic PHP knowledge

A Note on mod_rewrite

http://book.cakephp.org/2.0/en/tutorials-and-

examples/blog/blog.html

5

Make sure that an .htaccess override is allowed

Make sure you are editing the correct httpd.conf

Make sure Apache is loading up mod_rewrite

LoadModule rewrite_module

Page 6: Blog tutorial - pakistandasti.files.wordpress.com · what you’ll need  2 A running web server A database server Basic PHP knowledge

Half Done

http://book.cakephp.org/2.0/en/tutorials-and-

examples/blog/blog.html

6

Page 7: Blog tutorial - pakistandasti.files.wordpress.com · what you’ll need  2 A running web server A database server Basic PHP knowledge

Cake Database Configuration

http://book.cakephp.org/2.0/en/tutorials-and-

examples/blog/blog.html

7

/app/Config/database.php.default

Rename to database.php

Fill in your own DB configurations

/app/Config/core.php

Configure::write('Security.salt', 'pl345e-P45s_7h3*S@l7!');

Configure::write('Security.cipherSeed',

'7485712659625147843639846751');

Page 8: Blog tutorial - pakistandasti.files.wordpress.com · what you’ll need  2 A running web server A database server Basic PHP knowledge

Ready to Go

http://book.cakephp.org/2.0/en/tutorials-and-

examples/blog/blog.html

8

Page 9: Blog tutorial - pakistandasti.files.wordpress.com · what you’ll need  2 A running web server A database server Basic PHP knowledge

Controller

http://book.cakephp.org/2.0/en/tutorials-and-

examples/blog/blog.html

9

class PostsController extends AppController {

public $helpers = array('Html', 'Form');

public function index() {

$this->set('posts', $this->Post->find('all'));

}

}

Page 10: Blog tutorial - pakistandasti.files.wordpress.com · what you’ll need  2 A running web server A database server Basic PHP knowledge

Index View

http://book.cakephp.org/2.0/en/tutorials-and-

examples/blog/blog.html

10

<?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; ?>

Page 11: Blog tutorial - pakistandasti.files.wordpress.com · what you’ll need  2 A running web server A database server Basic PHP knowledge

Add Action

http://book.cakephp.org/2.0/en/tutorials-and-

examples/blog/blog.html

11

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.');

}

}

}

Page 12: Blog tutorial - pakistandasti.files.wordpress.com · what you’ll need  2 A running web server A database server Basic PHP knowledge

Add View

http://book.cakephp.org/2.0/en/tutorials-and-

examples/blog/blog.html

12

<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');

?>

Page 13: Blog tutorial - pakistandasti.files.wordpress.com · what you’ll need  2 A running web server A database server Basic PHP knowledge

What Helpers do

http://book.cakephp.org/2.0/en/tutorials-and-

examples/blog/blog.html

13

$this->Form->create()

<form id="PostAddForm" method="post"

action="/posts/add">

$this->Html->link('Add Post', array('controller' => 'posts',

'action' => 'add'));

Hyper Link

echo $this->Form->postLink( 'Delete', array('action' =>

'delete', $post['Post']['id']), array('confirm' => 'Are you

sure?')); ?

Page 14: Blog tutorial - pakistandasti.files.wordpress.com · what you’ll need  2 A running web server A database server Basic PHP knowledge

Edit Action

http://book.cakephp.org/2.0/en/tutorials-and-

examples/blog/blog.html

14

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.');

}

}

}

Page 15: Blog tutorial - pakistandasti.files.wordpress.com · what you’ll need  2 A running web server A database server Basic PHP knowledge

Delete Action

http://book.cakephp.org/2.0/en/tutorials-and-

examples/blog/blog.html

15

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'));

}

}

Page 16: Blog tutorial - pakistandasti.files.wordpress.com · what you’ll need  2 A running web server A database server Basic PHP knowledge

Routes, /app/Config/routes.php

http://book.cakephp.org/2.0/en/tutorials-and-

examples/blog/blog.html

16

Router::connect('/', array('controller' => 'pages', 'action'

=> 'display', 'home'));

Router::connect('/', array('controller' => 'posts', 'action'

=> 'index'));

Page 17: Blog tutorial - pakistandasti.files.wordpress.com · what you’ll need  2 A running web server A database server Basic PHP knowledge

Layout

http://book.cakephp.org/2.0/en/tutorials-and-

examples/blog/blog.html

17

<?php echo $this->fetch('content'); ?>

echo $this->fetch('meta');

echo $this->fetch('css');

echo $this->fetch('script');

Page 18: Blog tutorial - pakistandasti.files.wordpress.com · what you’ll need  2 A running web server A database server Basic PHP knowledge

Switch layouts

http://book.cakephp.org/2.0/en/tutorials-and-

examples/blog/blog.html

18

public function admin_view() {

// stuff

$this->layout = 'admin';

}

Page 19: Blog tutorial - pakistandasti.files.wordpress.com · what you’ll need  2 A running web server A database server Basic PHP knowledge

Elements /app/View/Elements/helpbox.ctp

http://book.cakephp.org/2.0/en/tutorials-and-

examples/blog/blog.html

19

<?php echo $this->element('helpbox'); ?>

echo $this->element('helpbox', array( "helptext" => "Oh,

this text is very helpful." ));

Page 20: Blog tutorial - pakistandasti.files.wordpress.com · what you’ll need  2 A running web server A database server Basic PHP knowledge

Scaffolding

http://book.cakephp.org/2.0/en/tutorials-and-

examples/blog/blog.html

20

<?php

class CategoriesController extends

AppController {

public $scaffold;

}

Page 21: Blog tutorial - pakistandasti.files.wordpress.com · what you’ll need  2 A running web server A database server Basic PHP knowledge

Simple Authentication and Authorization

Application

http://book.cakephp.org/2.0/en/tutorials-and-

examples/blog/blog.html

21

CREATE TABLE users (

id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,

username VARCHAR(50),

password VARCHAR(50),

role VARCHAR(20),

created DATETIME DEFAULT NULL,

modified DATETIME DEFAULT NULL

);

Page 22: Blog tutorial - pakistandasti.files.wordpress.com · what you’ll need  2 A running web server A database server Basic PHP knowledge

User Model

http://book.cakephp.org/2.0/en/tutorials-and-

examples/blog/blog.html

22

class User extends AppModel {

public $validate = array(

'username' => array(

'required' => array(

'rule' => array('notEmpty'),

'message' => 'A username is required'

)

),

'password' => array(

'required' => array(

'rule' => array('notEmpty'),

'message' => 'A password is required'

)

),

);

}

Page 23: Blog tutorial - pakistandasti.files.wordpress.com · what you’ll need  2 A running web server A database server Basic PHP knowledge

ByPass Some Views

http://book.cakephp.org/2.0/en/tutorials-and-

examples/blog/blog.html

23

public function beforeFilter() {

parent::beforeFilter();

$this->Auth->allow('add', 'logout');

}

Page 24: Blog tutorial - pakistandasti.files.wordpress.com · what you’ll need  2 A running web server A database server Basic PHP knowledge

AppController

http://book.cakephp.org/2.0/en/tutorials-and-

examples/blog/blog.html

24

public $components = array(

'Session',

'Auth' => array(

'loginRedirect' => array('controller' => 'posts', 'action' =>

'index'),

'logoutRedirect' => array('controller' => 'pages', 'action'

=> 'display', 'home')

)

);

public function beforeFilter() {

$this->Auth->allow('index', 'view');

}

Page 25: Blog tutorial - pakistandasti.files.wordpress.com · what you’ll need  2 A running web server A database server Basic PHP knowledge

Login Logout

http://book.cakephp.org/2.0/en/tutorials-and-

examples/blog/blog.html

25

public function login() {

if ($this->request->is('post')) {

if ($this->Auth->login()) {

$this->redirect($this->Auth->redirect());

} else {

$this->Session->setFlash(__('Invalid username or

password, try again'));

}

}

}

public function logout() {

$this->redirect($this->Auth->logout());

}

Page 26: Blog tutorial - pakistandasti.files.wordpress.com · what you’ll need  2 A running web server A database server Basic PHP knowledge

Encrypt password

http://book.cakephp.org/2.0/en/tutorials-and-

examples/blog/blog.html

26

public function beforeSave($options = array()) {

if (isset($this->data[$this->alias]['password'])) {

$this->data[$this->alias]['password'] =

AuthComponent::password($this->data[$this-

>alias]['password']);

}

return true;

}

Page 27: Blog tutorial - pakistandasti.files.wordpress.com · what you’ll need  2 A running web server A database server Basic PHP knowledge

Login View

http://book.cakephp.org/2.0/en/tutorials-and-

examples/blog/blog.html

27

<div class="users form">

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

<?php echo $this->Form->create('User'); ?>

<fieldset>

<legend><?php echo __('Please enter your username and password');

?></legend>

<?php

echo $this->Form->input('username');

echo $this->Form->input('password');

?>

</fieldset>

<?php echo $this->Form->end(__('Login')); ?>

</div>

Page 28: Blog tutorial - pakistandasti.files.wordpress.com · what you’ll need  2 A running web server A database server Basic PHP knowledge

Who is Login?

http://book.cakephp.org/2.0/en/tutorials-and-

examples/blog/blog.html

28

$this->Auth->user('id')

Null if not logged in

Another Way

public function isAuthorized($user) {

// Admin can access every action

if (isset($user['role']) && $user['role'] === 'admin') {

return true;

}