Slide 1

Slide 1 text

Slim 從零開始 2013.10.05 @ PHPConf Alan Sun [email protected] 13年10月5⽇日星期六

Slide 2

Slide 2 text

About me 接觸網站約六年 接觸PHP CakePHP 2~3年 SlimPHP 1~2年 主⼒力致⼒力於前端開發 ⺫⽬目前任於穎客資訊前端⼯工程師 13年10月5⽇日星期六

Slide 3

Slide 3 text

WHY Framework 加速開發 架構相同,多⼈人開發容易 更⽅方便的維護與升級 重覆使⽤用相同的模組在其他專案上 不⽤用重造輪胎? 13年10月5⽇日星期六

Slide 4

Slide 4 text

CakePHP 達⼈人 第⼀一套接觸的Framework 架構完整 功能強⼤大 13年10月5⽇日星期六

Slide 5

Slide 5 text

CakePHP 達⼈人 第⼀一套接觸的Framework 架構完整 功能強⼤大 許多功能都⽤用不到 ORM的效能不彰 13年10月5⽇日星期六

Slide 6

Slide 6 text

閃光洽說:我們換套Framework吧! 輕量化、速度快為考量重點 好開發、架構簡單也是重點 Yaf, Phalcon都在考量內 那時候差點選了CI 最後挑上了Slim 13年10月5⽇日星期六

Slide 7

Slide 7 text

WHY Slimphp 架構單純 輕量化(因為什麼東⻄西都沒有) 素肚速度快 好理解 13年10月5⽇日星期六

Slide 8

Slide 8 text

WHY Slimphp http://systemsarchitect.net/performance-benchmark-of-popular-php-frameworks/ 13年10月5⽇日星期六

Slide 9

Slide 9 text

SlimPHP MVC正夯 ⼀一套沒有MVC的Framework 沒有MVC,它到底出來幹嘛 只靠Route 貫穿全場 2011.09.15 by Josh Lockhart 13年10月5⽇日星期六

Slide 10

Slide 10 text

SlimPHP 適合我嗎? 開發API service 快速開發 ⼩小型、活動網站(⼤大型網站) 厭倦了現在的Framework 想要嘗鮮?丟掉沈重的包袱! 新⼿手上路 13年10月5⽇日星期六

Slide 11

Slide 11 text

安裝 使⽤用Composer ⼿手動下載 PHP >= 5.3.0 http://www.slimframework.com/install 13年10月5⽇日星期六

Slide 12

Slide 12 text

1.載⼊入Slim

Slide 13

Slide 13 text

初始化 Slim App //With default settings $app = new \Slim\Slim(); //With custom settings $app = new \Slim\Slim(array( 'mode' => 'development', 'debug' => true, 'log.level' => Slim\Log::DEBUG, 'view' => 'MyCustomViewClassName' )); 13年10月5⽇日星期六

Slide 14

Slide 14 text

確定你的Route //GET route $app->get('/hello/:name', function ($name) { echo "Hello, $name"; }); //POST route $app->post('/person', function () { //Create new Person }); 13年10月5⽇日星期六

Slide 15

Slide 15 text

RUN $app->run(); http://mydomain.me/hello/alansun 13年10月5⽇日星期六

Slide 16

Slide 16 text

Route HTTP請求(GET, POST, PUT ...) 與 URI 作為流程 依據。 判別不到的 Routing,slim⾃自動返回404。 13年10月5⽇日星期六

Slide 17

Slide 17 text

Http Request $app->get('/books', function () { ... }); $app->post('/books', function () { ... }); $app->put('/books', function () { ... }); $app->delete('/books', function () { ... }); 13年10月5⽇日星期六

Slide 18

Slide 18 text

Multiple Http Methods ex: http://phpconf.slim/books $app->map('/books', function() { echo "I respond to multiple HTTP methods!"; })->via('GET', 'POST'); 13年10月5⽇日星期六

Slide 19

Slide 19 text

Route Parameters ex: http://phpconf.slim/books/tech/t003022 $app->get('/books/:category/:id', function ($category, $id) { echo "The paramters are " . $category . ”&” . $two; }); OR $app->get('/books/:params+', function ($params) { var_dump($params); // print array[‘tech’, ‘t003022’] }); 13年10月5⽇日星期六

Slide 20

Slide 20 text

Optional Route Parameters $app->get('/books(/:category(/:id))', function ($category = null, $id = 0) { echo "The paramters are " . $category . ”&” . $id; }); http://phpconf.slim/books http://phpconf.slim/books/ http://phpconf.slim/books/tech http://phpconf.slim/books/tech/t003022 13年10月5⽇日星期六

Slide 21

Slide 21 text

Route Conditions $app->get('/books/:category/:id', function ($category = null, $id = 0) { echo "The paramters are " . $category . ”&” . $id; })->conditions(array(‘category’ => ‘[\w]+’, ‘id’ => ‘[0-9]+’)); 13年10月5⽇日星期六

Slide 22

Slide 22 text

Route Names $app->get('/books/:category/:id', function ($category = null, $id = 0) { echo "The paramters are " . $category . ”&” . $id; })->name(‘books’); $uri = $app->urlFor(‘books’, array(‘one’ => ‘tech’, ‘two’ => ‘t123’)); // /books/tech/t123 13年10月5⽇日星期六

Slide 23

Slide 23 text

App Scope $app = new \Slim\Slim(); $app->get(‘/hello:name’, function($name) { $app->render(‘hello.html’); <-- ERROR }); $app->get(‘/hello:name’, function($name) use ( $app ) { $app->render(‘hello.html’); <-- SUCCESS }); 13年10月5⽇日星期六

Slide 24

Slide 24 text

View Helper Slim 透過 $app->render() 去讀取 template,並且給予參與及狀態。 $app->get(‘/hello:name’, function( $name ) use ( $app ) { $app->render( ‘hello.html’, array( ‘name’=> $name, ‘id’ => ‘t00221’ ), 200 <-- Response Status ); }); 13年10月5⽇日星期六

Slide 25

Slide 25 text

Halt Pass Redirect Stop UrlFor Route Helper 13年10月5⽇日星期六

Slide 26

Slide 26 text

Halt 直接回復⼀一個HTTP response $app->halt( 403, ‘You shall not pass!’); 13年10月5⽇日星期六

Slide 27

Slide 27 text

Pass 表⽰示告知Slim去尋找下⼀一個匹配的Route 當無匹配Route時就回傳404 http://mydomain.me/hello/frank $app->get('/hello/frank', function () use ($app) { echo "You won't see this..."; $app->pass(); }); $app->get('/hello/:name', function ($name) use ($app) { echo "But you will see this!"; }); 13年10月5⽇日星期六

Slide 28

Slide 28 text

Redirect 轉址⾄至另⼀一個URL 預設回傳302 $app->get('/old', function () use ($app) { $app->redirect('/new', 301); }); 13年10月5⽇日星期六

Slide 29

Slide 29 text

Stop 終⽌止現在Route 執⾏行 $app->get('/stop', function () use ($app) { echo "You will see this..."; $app->stop(); echo "But not this"; }); 13年10月5⽇日星期六

Slide 30

Slide 30 text

UrlFor 動態建⽴立已命名Route URL //Create a named route $app->get('/hello/:name', function ($name) use ($app) { echo "Hello $name"; })->name('hello'); //Generate a URL for the named route $url = $app->urlFor('hello', array('name' => 'Josh')); --> /hello/Josh 13年10月5⽇日星期六

Slide 31

Slide 31 text

Route Group v2.3 同⼀一組Route path 不同的HTTP Request 同⼀一組Route path 不同的sub path 13年10月5⽇日星期六

Slide 32

Slide 32 text

Different Http Request http://myDomain.me/library/book/b12345 $app->group('/library', function () use ($app) { $app->get('/books/:id', function ($id) use ($app) { // Get book with ID }); $app->post('/books/:id', function ($id) use ($app) { // Create book with ID }); $app->put('/books/:id', function ($id) use ($app) { // Update book with ID }); $app->delete('/books/:id', function ($id) use ($app) { // Delete book with ID }); 13年10月5⽇日星期六

Slide 33

Slide 33 text

當然你也可以 Different subpath http://myDomain.me/facebook/oauth (getAlbum, getPhotos) $app->group('/facebook', function () use ($app) { $app->get('/oauth', function () use ($app) { // facebook oauth }); $app->get('/getAlbum', function () use ($app) { // Get facebook albums }); $app->get('/getPhotos/:album_id', function ($album_id) use ($app) { // Get facebook photos by album }); }); 13年10月5⽇日星期六

Slide 34

Slide 34 text

Middleware 源⾄至 ruby Rack protocol 概念 簡單、可重覆使⽤用的堆疊架構 最常被使⽤用在認證檢查、Ajax request 檢查等.... 全域 Middleware Route Middleware 13年10月5⽇日星期六

Slide 35

Slide 35 text

Middleware 1 Middleware 2 Middleware 3 Middleware 4 Route 13年10月5⽇日星期六

Slide 36

Slide 36 text

Inner Middleware class SessionMiddleware extends \Slim\Middleware { public function call() { // Get reference to application $app = $this->app; // Run inner middleware and application $this->next->call(); // Start session if (headers_sent()) { if (empty($_SESSION)) { $_SESSION = array(); } } else { session_start(); } } } 13年10月5⽇日星期六

Slide 37

Slide 37 text

Route Middleware 在執⾏行Route之前所執⾏行的 function 能插⼊入任意Route,並以序列⽅方式執⾏行 13年10月5⽇日星期六

Slide 38

Slide 38 text

function mware1() { echo ‘This is middleware1 ’; } function mware2() { echo ‘This is middleware2 ’; } $app->get(‘/hello’, ‘mware1’, ‘mware2’, function() use ($app) { echo ‘hello everyone!!’; } output --> This is middleware1 This is middleware2 hello everyone!! 13年10月5⽇日星期六

Slide 39

Slide 39 text

Hook 在特定的流程下被執⾏行 silm.before slim.before.router slim.before.dispatch slim.after.dispatch slim.after.router slim.after Custom Hooks 13年10月5⽇日星期六

Slide 40

Slide 40 text

// before output buffering turn on $app->hook('slim.before', function () { //Do something }); // after output buffering turn off and after the Response is sent $app->hook('slim.after', function () { //Do something }); 13年10月5⽇日星期六

Slide 41

Slide 41 text

// after output buffering turn on and before the router is dispatched $app->hook('slim.before.router', function () { //Do something }); // after the router is dispatched, before the Response is sent $app->hook('slim.after.router', function () { //Do something }); 13年10月5⽇日星期六

Slide 42

Slide 42 text

// before the current matching route is dispatched $app->hook('slim.before.dispatch', function () { //Do something }); // after the current matching route is dispatched $app->hook('slim.after.dispatch', function () { //Do something }); 13年10月5⽇日星期六

Slide 43

Slide 43 text

Custom Hook $app->hook('json.dispatch', function () { //Do something }, 5); priority --> it will sort all callables assigned to it by priority (ascending) $app->get(‘/json_callback’, function() use ($app) { $app->applyHook('json.dispatch'); }); 13年10月5⽇日星期六

Slide 44

Slide 44 text

Error Handler $app->error(function (\Exception $e) use ($app) { $app->render('error.php'); }); $app->notFound(function () use ($app) { $app->render('404.html'); }); 13年10月5⽇日星期六

Slide 45

Slide 45 text

最後關於 View & model View Twig http://twig.sensiolabs.org/ Smarty http://www.smarty.net/ backbone.js http://backbonejs.org/ Model idiorm https://github.com/j4mie/idiorm PDO 13年10月5⽇日星期六

Slide 46

Slide 46 text

Reference http://www.slimframework.com/ http://systemsarchitect.net/performance-benchmark-of-popular-php- frameworks/ 13年10月5⽇日星期六

Slide 47

Slide 47 text

Q & A 13年10月5⽇日星期六

Slide 48

Slide 48 text

Thank you 13年10月5⽇日星期六