MVC in Trax. <? $link = mysql_connect('localhost', 'myuser', 'mypass'); if (!$link) { die('Could not connect: '. mysql_error()); } If($submit) { $sql.

Slides:



Advertisements
Similar presentations
PHP + Framework + MVC. What is Framework? Common code - Generic functionality Extensible - Specific functionality Unlike library – Flow Dictated by.
Advertisements

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 15 Introduction to Rails.
The Librarian Web Page Carol Wolf CS396X. Create new controller  To create a new controller that can manage more than just books, type ruby script/generate.
Keys, Referential Integrity and PHP One to Many on the Web.
Ruby on Rails Model of MVC. Model-View-Controller Paradigm A way of organizing a software system Benefits: Isolation of business logic from the user interface.
1.  Understanding about How to Working with Server Side Scripting using PHP Framework (CodeIgniter) 2.
Ruby on Rails by Manik Juneja Ruby On Rails. Ruby on Rails by Manik Juneja Rails is a Web Application development framework. Based on the MVC pattern.
Objectives Connect to MySQL from PHP
Software Architecture Patterns (2). what is architecture? (recap) o an overall blueprint/model describing the structures and properties of a "system"
Ruby on Rails. What is Ruby on Rails? Ruby on Rails is an open source full-stack web framework. It is an alternative to PHP/MySQL. It can render templates,
Dataface API Essentials Steve Hannah Web Lite Solutions Corp.
Lecture 3 – Data Storage with XML+AJAX and MySQL+socket.io
CodeIgniter - [Overview]
Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries.
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
MySQL in PHP – Page 1 of 17CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: MySQL in PHP Reading: Williams &
Server-side Scripting Powering the webs favourite services.
4-1 INTERNET DATABASE CONNECTOR Colorado Technical University IT420 Tim Peterson.
SIMPLE ROUTER The slide made by Salim Malakouti. Next we will create the Router  What do I we mean by a router?  Routers work similar to a map. It receives.
What is Sure BDCs? BDC stands for Batch Data Communication and is also known as Batch Input. It is a technique for mass input of data into SAP by simulating.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
Mark Dixon Page 1 23 – Web applications: Writing data to Databases using PhP.
Design Patterns Phil Smith 28 th November Design Patterns There are many ways to produce content via Servlets and JSPs Understanding the good, the.
SHOPPING CARTS CHAPTER 19. E-COMMERCE Typically, an e-commerce site will have public pages and admin pages.
MVC & ActiveRecord by Christian Mohr & Mohamed Souiai.
1 Dr Alexiei Dingli Web Science Stream Advanced ROR.
Comp4711 – Internet Development CodeIgniter Cookbook Comp4711 #2 – Sept 14, 2011.
Standalone Java Application vs. Java Web Application
Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4.
Introduction to Model-Glue Rachel Lehman Perpetual Intermediate Designer-Developer Introduction to Model-Glue.
Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
ActiveRecord. What? Model for database records. Grouping of data and behaviour. “Active Record Pattern” recorded by Martin Fowler, circa >
PHP + Framework + MVC CakePHP Lectures 1. What is Framework? CakePHP Lectures 2.
PHP MySQL Introduction. MySQL is the most popular open-source database system. What is MySQL? MySQL is a database. The data in MySQL is stored in database.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
SYST Web Technologies SYST Web Technologies Databases & MySQL.
CAKEPHP Blog tutorial. what you’ll need examples/blog/blog.html 2  A running web server  A database server.
Lecture 8 – Cookies & Sessions SFDV3011 – Advanced Web Development 1.
NMD202 Web Scripting Week3. What we will cover today Includes Exercises PHP Forms Exercises Server side validation Exercises.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
PHP+MySQL Integration. Connecting to databases One of the most common tasks when working with dynamic webpages is connecting to a database which holds.
Templates, Databases and Frameworks. Databases: DBI Common database interface for perl Provides a functional,
Associations INFO 2310: Topics in Web Design and Programming.
Photo Gallery INFO 2310: Topics in Web Design and Programming.
HTML, PHP, and MySQL: Putting It All Together. Making a Form Input tags Types: “text” “radio” “checkboxes” “submit”
Creating PHPs to Insert, Update, and Delete Data CS 320.
Pertemuan 10 Enterprise Application Patterns Mata kuliah: T0144 – Advanced Topics in Software Engineering Tahun: 2010.
MVC Concepts Basics Model-View-Controller (MVC) Concepts for Web Developers SoftUni Team Technical Trainers Software University
 Registry itself is easy and straightforward in implementation  The objects of registry are actually complicated to store and manage  Objects of Registry.
NMD202 Web Scripting Week5. What we will cover today PHP & MySQL Displaying Dynamic Pages Exercises Modifying Data PHP Exercises Assignment 1.
Introduction to Ruby&Rails Yuri Veremeyenko Monica Verma.
Date : 3/04/2010 Web Technology Solutions Class: PHP Web Application Frameworks.
Web Technologies Lecture 11 Implementing RESTful services.
Ruby on Rails Controller of MVC. Routes How we map URIs like /tweets/1 to calling the show method of the Controller.
Rails and routing INFO 2310: Topics in Web Design and Programming.
CS 160 and CMPE/SE 131 Software Engineering February 9 Class Meeting Department of Computer Science Department of Computer Engineering San José State University.
Working With Database Library And Helpers. Connecting to your Database First you need to set parameters in you database.php file residing in config folder.
Ruby on Rails. Web Framework for Ruby Designed to make it easier to develop, deploy, and maintain web applications Design with Model-View-Controller –almost.
Building Your Own Website Using:. Install & configure LAMP. Download WordPress and run it as a local website on your Raspberry Pi. Configure WordPress.
Web Technology Solutions
Play Framework: Introduction
Introduction to CodeIgniter (CI)
Introduction to MVC PHP Web Development Ivan Yonkov Training Manager
MVC Framework, in general.
Ruby on Rails by Manik Juneja
Ruby on Rails by Manik Juneja
Controllers.
Chapter 15 Introduction to Rails.
Presentation transcript:

MVC in Trax

<? $link = mysql_connect('localhost', 'myuser', 'mypass'); if (!$link) { die('Could not connect: '. mysql_error()); } If($submit) { $sql = “INSERT INTO my_table (name,address,city,state,zip) VALUES (”; $sql.= “’$name’,’$address’,’$city’,’$state’,’$zip’)”; mysql_query($sql); } else { $result = mysql_query(“SELECT * FROM my_table WHERE id = 1”); $userArray = mysql_fetch_array($result); } ?> Add User My HTML code blah blah Name: ”> … … Typical PHP Code – Everything shoved into one file.  Not Good!

<? require_once(“config.inc.php"); require_once(“database.inc.php"); $dbh = dbConnect(); If($submit) { $sql = “INSERT INTO my_table (name,address,city,state,zip) VALUES (”; $sql.= “’$name’,’$address’,’$city’,’$state’,’$zip’)”; $dbh->query($sql); } else { $result = $dbh->query(“SELECT * FROM my_table”); $userArray = $dbh->fetchRow($result); } printHeader(); ?> My HTML code blah blah Name: ”> … … Better but still not great.

Leads to frustrating code to maintain and update.

What are our options for MVC in PHP? PHP on TraxPHP on Trax - A True Ruby on Rails framework clone for PHP (PHP5). PhpMVCPhpMVC (A Struts port)Struts SolarPHPSolarPHP - A PHP5 Web Framework. MVC-based. CakeCake - A Ruby on Rails like framework for PHP. MIT licensed. BiscuitBiscuit - Similar to Cake only using much more procedural code (rather than OO). BSD licensed. TaniPHPTaniPHP - PHP MVC Ruby on Rails like framework for PHP. LGPL licensed. Aukyla PHP FrameworkAukyla PHP Framework - Nice ideas: Local URI’s (stream wrappers) and OpenDocument file handling. Released under a dual GPL/Commercial license. symfonysymfony - just another php5 framework ? Probably not. It takes the best of Mojavi, Propel and Rails, adds some more and packages it all into an integrated framework. MIT licensed.

Ruby on Rails

PHP on Trax Model (Active Record) - Connects business objects and database tables to create a persistable domain model where logic and data are presented in one wrapping. View (Action View) – Helper classes to display html elements in your views. Controller (Action Controller) - Is made up of one or more actions that performs its purpose and then either renders a template or redirects to another action. An action is defined as a public method in the controller, which will automatically be made accessible to the web-server through a mod_rewrite mapping.

Active Record An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data. An object carries both data and behavior. Much of this data is persistent and needs to be stored in a database. Active Record uses the most obvious approach, putting data access logic in the domain object. This way all people know how to read and write their data to and from the database. URL: Patterns of Enterprise Application Architecture by Martin FowlerMartin Fowler

Active Record Models <? class Post extends ActiveRecord { } ?> This Post Model/Object now knows all about the thing it represents, the database table “posts”, and has the ability to insert, update, delete records from itself.

Active Record Models <? class Post extends ActiveRecord { public $belongs_to = “author”; public $has_many = “comments”; } ?> A Post has many “comments” which means that there is a table called “comments” that has a foreign key in it called “post_id”. A Post belongs to an “author” which means that there is a foreign key called “author_id” in the “posts” table (this table), that is a foreign key to table “authors”.

URL: Controller ( app/controllers/blog_controller.php ) <? class BlogController extends ApplicationController { function show_post() { $post = new Post(); $this->post = $this->post->find($_REQUEST[‘id’]); } ?> View ( app/views/blog/show_post.phtml ) title ?> body ?> post date: created_on ?> Comments: comments)): ?> comments as $comment): ?> body ?> no comments found.

Active Record Validations <? class Person extends ActiveRecord { public $belongs_to = “author”; public $has_many = “comments”; function validate() { if(strlen($this->title) < 4) { $this->add_error(“Title must be at least 4 chars long”, “title”); } function validate_title() { return array(!empty($this->title), “Title can’t be empty”); } ?> String: $model->get_errors_as_string(); “error a error b …” Array: $model->get_errors(); array(“error a”,”error b”,…)

Action Controller Layouts – Templates that contains the look of your site. Partials – For convenience html that will be used in multiple places can be put into partials for inclusion into other parts of your code. Routing – Allows you to define specific urls and what they should link to. Filters – Specify one or more function that will be ran before or after every function in this controller class. Flash – Provides a way to pass temporary variables between actions, usually messages to be displayed out to the user.

Layouts Location : app/views/layouts Default layout: application.phtml My Application

Layouts <? class TestController extends ApplicationController { public $layout = “blue”; function some_method() { … some code } ?> Loads layout app/views/layouts/blue.phtml <? class TestController extends ApplicationController { public $layout = “my_layout”; function my_layout() { if(Session::get(‘user_level’) == ADMIN) { return “admin”; } else { return “user”; } ?> If this is an admin then it will load the layout app/views/layouts/admin.phtml Else it will load the layout app/views/layouts/user.phtml

Partials Add ( app/views/users/add.phtml ) “add”)) ?> render_partial(“form”) ?> Edit ( app/views/users/edit.phtml ) “edit”, “:id” => $user)) ?> render_partial(“form”) ?> Form Partial ( app/views/users/_form.phtml ) Name: Age:

Filters before_filters / after_filter – Making a protected area of your application. <? class TestController extends AdminAreaController { function delete_user() { … some code } ?> <? class AdminAreaController extends ApplicationController { public $before_filter = “authenticate”; # not really working so have to put in constructor function __construct() { parent::__construct(); $this->before_filter(“authenticate”); } function authenticate() { if(Session::get(‘logged_in’) == false) { Session::flash(‘error’, ‘You must be logged in to access this page.’); $this->redirect_to = “/login”; } ?>

Flash The flash provides a way to pass temporary variables between actions. Anything you place in the flash will be exposed to the very next action and then cleared out. This is a great way of doing notices and alerts, such as a create action that sets Session::flash(‘notice’,"Successfully created“) before redirecting to a display action that can then expose the flash to its template. If( $user->save() ) { Session::flash(‘notice’,"Successfully created user“); $this->redirect_to = “/users”; } else { Session::flash(‘error’,"Failed to created user“); }

Routing config/routes.php <? # Add your own custom routes here. # The priority is based upon order of creation: first created -> highest priority. # Here's a sample route: $router->connect( "products/:id", array(":controller" => "catalog", ":action" => "view") ); # You can have the root of your site routed by hooking up "". # Just remember to delete public_html/index.html. $router->connect( "", array(":controller" => "search") ); # Install the default route as the lowest priority. $router->connect( ":controller/:action/:id" ); ?>

mod_rewrite # php include_path to Trax config directory php_value include_path.:/home/ /trax/config # Redirect all requests not available on the filesystem to Trax RewriteEngine On # If you don't want Trax to look in certain directories, # use the following rewrite rules so that Apache won't rewrite certain requests # # Example: # RewriteCond %{REQUEST_URI} ^/nottrax.* # RewriteRule.* - [L] # If your Trax application is accessed via an Alias directive, # then you MUST also set the RewriteBase in this htaccess file. # # Example: # Alias /mytraxapp /path/to/mytraxapp/public # RewriteBase /mytraxapp RewriteRule ^$ index.html [QSA] RewriteRule ^([^.]+)$ $1.html [QSA] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond $1 !-d RewriteRule ^(.*)$ /dispatch.php?$1 [QSA,L] # In case Trax experiences terminal errors # Instead of displaying this message you can supply # a file here which will be rendered instead # # Example: # ErrorDocument 500 /500.html ErrorDocument 500 " Application error Trax application failed to start properly"

Questions? John Peterson

Lets see it in action!