Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Slim 從零開始 - PHPConf 2013

Alansun
October 08, 2013

Slim 從零開始 - PHPConf 2013

Alansun

October 08, 2013
Tweet

Other Decks in Technology

Transcript

  1. 初始化 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⽇日星期六
  2. 確定你的Route //GET route $app->get('/hello/:name', function ($name) { echo "Hello, $name";

    }); //POST route $app->post('/person', function () { //Create new Person }); 13年10月5⽇日星期六
  3. Route HTTP請求(GET, POST, PUT ...) 與 URI 作為流程 依據。 判別不到的

    Routing,slim⾃自動返回404。 13年10月5⽇日星期六
  4. Http Request $app->get('/books', function () { ... }); $app->post('/books', function

    () { ... }); $app->put('/books', function () { ... }); $app->delete('/books', function () { ... }); 13年10月5⽇日星期六
  5. 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⽇日星期六
  6. 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⽇日星期六
  7. 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⽇日星期六
  8. 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⽇日星期六
  9. 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⽇日星期六
  10. 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⽇日星期六
  11. 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⽇日星期六
  12. 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⽇日星期六
  13. Stop 終⽌止現在Route 執⾏行 $app->get('/stop', function () use ($app) { echo

    "You will see this..."; $app->stop(); echo "But not this"; }); 13年10月5⽇日星期六
  14. 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⽇日星期六
  15. 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⽇日星期六
  16. 當然你也可以 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⽇日星期六
  17. 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⽇日星期六
  18. 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⽇日星期六
  19. // 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⽇日星期六
  20. // 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⽇日星期六
  21. // 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⽇日星期六
  22. 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⽇日星期六
  23. 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⽇日星期六
  24. 最後關於 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⽇日星期六