Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Intro To Mvc Development In Php
1. Intro to MVC Development in PHP
Ed Finkler • http://funkatron.com • @funkatron
#tekmvc • php|tek 2009
2. Thee Agenda
All About MVC
Why CodeIgniter?
The CI MVC Model
CI Basics
Running CI out of the box
Making a basic web app
Making a web api
Libraries, components, logging and caching
3. All About MVC
http://www.flickr.com/photos/airship/118352487/
16. PAC
Presentation-Abstraction-Controller
Presentation Control Model
Presentation Control Model
Presentation Control Model
Presentation Control Model Presentation Control Model
22. All of them have value*
* except Zend Framework
23. That being said, CI is…
Easy to understand
Simple
doesn't require advanced OOP
Doesn't force lots of conventions
Plays well with others
Quick to get up and running
Good docs and great community
Backed by invested entity (http://ellislab.com)
31. Controller
The core of everything <?php
class Site extends Controller {
quot;Heavyquot;: you could do function Site() {
parent::Controller();
everything in controller $this->load->library('session');
}
public methods are function index() {
available as actions from // mletters model is auto-loaded
$rows = $this->mletters->getMany(10);
URL $data['rows'] = $this->_prepData($rows);
$this->load->view('index', $data);
}
private methods prefixed
with “_” function _prepData($rows) {
// do some cleanup on the data…
}
?>
32. Model
ActiveRecord pattern available, not required
Query binding
$sql = quot;SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?quot;;
$this->db->query($sql, array(3, 'live', 'Rick'));
Don't like the DB layer? Use something else
Zend_DB, Doctrine, DataMapper (http://bit.ly/
datamapper), IgniteRecord (http://bit.ly/igrec) …
34. Helper
Procedural funcs, grouped by file
Mostly for views; available in controllers
/**
* Plural
*
* Takes a singular word and makes it plural
*
* @access public
* @param string
* @param bool
* @return str
*/
function plural($str, $force = FALSE)
{
// [...]
}
40. CI Basics
http://www.flickr.com/photos/canoafurada/395304306/
41. CI Structure
front controller
index.php points to system and
application folders
system application
base classes & app-specific classes
built-in functionality & functionality
49. Population estimates DB
Get our data from Numbrary: http://short.ie/w3f6h3)
Make a new controller
Change the default route
Config DB settings
Make a model
Make a view
Make fancier views
50. Make a web API
http://www.flickr.com/photos/dunechaser/2429621774/
51. Web API for pop. est. DB
Let users query our DB via HTTP
Return results on JSON or serialized PHP