Upload
joshua-silver
View
628
Download
1
Tags:
Embed Size (px)
Citation preview
Real World Web App Development(in 2 hours or less)
Jason ArdellJoshua Silver
GT College of ComputingOctober 13, 2010
What do we mean,
“Real World”Web App Development?
What are we building?
(Demo)
Our Toolkit Today
What is MVC and why use it?
http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/
Interactive
Follow along at:
http://github.com/joshuasilver/RealWorldWebApp
Connect to Server
• You should have credentials
• http://dl.dropbox.com/u/5037034/gt.txt$> ssh root@[your.ip.address]
[enter password]
Connect to MySQLOnce logged in, from Command line:
# mysql -u cakephpuser -p cakephpdbEnter password: << PASSWORD IS: foo
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 183Server version: 5.1.41-3ubuntu12.6-log (Ubuntu)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
Setup DB
(Be sure to use spaces, not tabs)
cd /var/www/cakephpnano db_schema.sql
mysql> source db_schema.sql
mysql> show tables;+---------------------+| Tables_in_cakephpdb |+---------------------+| students |+---------------------+1 row in set (0.00 sec)
mysql> describe students;+--------------+------------------+------+-----+-------------------+----------------+| Field | Type | Null | Key | Default | Extra |+--------------+------------------+------+-----+-------------------+----------------+| id | int(10) unsigned | NO | PRI | NULL | auto_increment || first_name | varchar(50) | NO | | NULL | || last_name | varchar(50) | NO | | NULL | || phone_number | char(12) | NO | | NULL | || time_created | timestamp | NO | | CURRENT_TIMESTAMP | |+--------------+------------------+------+-----+-------------------+----------------+5 rows in set (0.01 sec)
Make sure it worked
Insert Fake Data
(Be sure to use spaces, not tabs)
cd /var/www/cakephpnano db_testdata.sql
Make sure it workedmysql> source db_testdata.sql
mysql> select * from students;+----+------------+-----------+--------------+---------------------+| id | first_name | last_name | phone_number | time_created |+----+------------+-----------+--------------+---------------------+| 1 | John | Doe | 678-555-0000 | 2010-10-13 16:08:22 || 2 | Sally | Smith | 770-555-1234 | 2010-10-13 16:08:23 |+----+------------+-----------+--------------+---------------------+2 rows in set (0.00 sec)
Done with MySQL
Over to CakePHP … its already installed
cd /var/www/cakephp
Tour of CakePHP
All we care about is:
/app/models//app/views//app/controllers/
A few notes
• For automagic to work, you must name your files exactly to the spec. (case and spacing sensitive)
Create a student model# nano /var/www/cakephp/app/models/student.php
<?php
class Student extends AppModel { var $name = 'Student';}
// CakePHP automagically completes the rest
?>
Create a student controller# nano
/var/www/cakephp/app/controllers/students_controller.php
<?phpclass StudentsController extends AppController {
var $name = 'Students'; function index() {
$this->set('studentList', $this->Student->find('all'));
}}
?>
Views
Class name
Method Names
Create student view folder# mkdir /var/www/cakephp/app/views/students
Add index view
nano /var/www/cakephp/app/views/students/index.ctp
root@gt-tutorial-jos<h1>Students</h1><table> <tr> <th>Id</th> <th>First Name</th> <th>Last Name</th> <th>Phone Number</th> <th>Created</th> </tr>
<!-- Here is where we loop through our $students array, printing out the students -->
<?php foreach ($studentList as $student): ?> <tr> <td><?php echo $student['Student']['id']; ?></td> <td><?php echo $student['Student']['first_name']; ?></td> <td><?php echo $student['Student']['last_name']; ?></td> <td><?php echo $student['Student']['phone_number']; ?></td> <td><?php echo $student['Student']['time_created']; ?></td> </tr> <?php endforeach; ?></table>
http://{ip_address}/students/index
ControllerMethod
Let’s figure out how to add a student
1.) Add new method called “add” to students_controller
2.) Make associated view3.) Add link on homepage to add a student
nano /var/www/cakephp/app/controllers/students_controller.php
<?phpclass StudentsController extends AppController {
var $name = 'Students';
function index() { $this->set('studentList', $this->Student->find('all')); }
function add() { if (!empty($this->data)) { if ($this->Student->save($this->data)) { $this->Session->setFlash('Your student has been added.'); $this->redirect(array('action' => 'index')); } } }
Add view<h1>Add Student</h1><?php echo $form->create('Student'); echo $form->input('first_name'); echo $form->input('last_name'); echo $form->input('phone_number'); echo $form->end('Add Student');?>
<?php echo $html->link("Add Student", array('controller' => 'students', 'action' => 'add')); ?>
Add link on homepage (index.ctp)
Twilio