Real World Web App Development(in 2 hours or less)GT College of ComputingOctober 13, 2010Jason ArdellJoshua Silver
What do we mean, “Real World”Web App Development?
What are we building?(Demo)
Who are we?Joshua Silver@1yellowbrickJoshua.silver@securehealthpay.comCS ‘09Jason Ardell@ardellardell@gmail.comCS ‘05
Our Toolkit Today
What is MVC and why use it?
http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/
InteractiveFollow along at:http://github.com/joshuasilver/RealWorldWebApp
Connect to ServerYou should have credentialshttp://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:  fooWelcome 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 DBcd /var/www/cakephpnano db_schema.sql(Be sure to use spaces, not tabs)
Make sure it workedmysql> source db_schema.sqlmysql> 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)
Insert Fake Datacd /var/www/cakephpnano db_testdata.sql(Be sure to use spaces, not tabs)
Make sure it workedmysql> source db_testdata.sqlmysql> 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 MySQLOver to CakePHP … its already installedcd /var/www/cakephp
Tour of CakePHPAll we care about is:/app/models//app/views//app/controllers/
A few notesFor 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<?phpclass Student extends AppModel {var $name = 'Student';}// CakePHPautomagically 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'));  }}?>
ViewsClass nameMethod Names
Create student view folder# mkdir /var/www/cakephp/app/views/students
nano /var/www/cakephp/app/views/students/index.ctproot@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 -->    <?phpforeach ($studentListas $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>    <?phpendforeach; ?></table>Add index view
http://{ip_address}/students/indexMethodController
Let’s figure out how to add a student1.) Add new method called “add” to students_controller2.) 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

Real

  • 2.
    Real World WebApp Development(in 2 hours or less)GT College of ComputingOctober 13, 2010Jason ArdellJoshua Silver
  • 3.
    What do wemean, “Real World”Web App Development?
  • 4.
    What are webuilding?(Demo)
  • 5.
    Who are we?JoshuaSilver@1yellowbrickJoshua.silver@securehealthpay.comCS ‘09Jason Ardell@ardellardell@gmail.comCS ‘05
  • 7.
  • 8.
    What is MVCand why use it?
  • 9.
  • 10.
  • 12.
    Connect to ServerYoushould have credentialshttp://dl.dropbox.com/u/5037034/gt.txt$> ssh root@[your.ip.address] [enter password]
  • 13.
    Connect to MySQLOncelogged in, from Command line:# mysql -u cakephpuser -p cakephpdbEnter password: << PASSWORD IS: fooWelcome 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>
  • 14.
    Setup DBcd /var/www/cakephpnanodb_schema.sql(Be sure to use spaces, not tabs)
  • 15.
    Make sure itworkedmysql> source db_schema.sqlmysql> 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)
  • 16.
    Insert Fake Datacd/var/www/cakephpnano db_testdata.sql(Be sure to use spaces, not tabs)
  • 17.
    Make sure itworkedmysql> source db_testdata.sqlmysql> 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)
  • 18.
    Done with MySQLOverto CakePHP … its already installedcd /var/www/cakephp
  • 19.
    Tour of CakePHPAllwe care about is:/app/models//app/views//app/controllers/
  • 20.
    A few notesForautomagic to work, you must name your files exactly to the spec. (case and spacing sensitive)
  • 21.
    Create a studentmodel# nano /var/www/cakephp/app/models/student.php<?phpclass Student extends AppModel {var $name = 'Student';}// CakePHPautomagically completes the rest?>
  • 22.
    Create a studentcontroller# 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'));  }}?>
  • 23.
  • 24.
    Create student viewfolder# mkdir /var/www/cakephp/app/views/students
  • 25.
    nano /var/www/cakephp/app/views/students/index.ctproot@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 --> <?phpforeach ($studentListas $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> <?phpendforeach; ?></table>Add index view
  • 26.
  • 28.
    Let’s figure outhow to add a student1.) Add new method called “add” to students_controller2.) Make associated view3.) Add link on homepage to add a student
  • 29.
    nano /var/www/cakephp/app/controllers/students_controller.php<?phpclass StudentsControllerextends 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')); } } }
  • 30.
    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)
  • 32.