PHP微型框架slim的安装使用

slim安装和使用初步

最简单粗暴和直接的方法——到github下载zip文件,slim github【链接】。解压之后把【1】Slim文件夹,【2】.htaccess文件和【3】index.php文件复制到www目录中。若看到以下网页说明slim安装成功。
PHP微型框架slim的安装使用
图2 slim安装成功

4.简单的修改和测试

    Slim提供完善的REST框架,支持GET、POST、PUT和Delete等方法,可以把index.php修改的更简单一些。可从以下代码中可以熟悉Slim的基本框架和使用方法。
[php] view plain copy
  1. <?php  
  2. /** 
  3.  * Step 1: Require the Slim Framework 
  4.  * 
  5.  * If you are not using Composer, you need to require the 
  6.  * Slim Framework and register its PSR-0 autoloader. 
  7.  * 
  8.  * If you are using Composer, you can skip this step. 
  9.  */  
  10. require 'Slim/Slim.php';  
  11.   
  12. \Slim\Slim::registerAutoloader();  
  13.   
  14. /** 
  15.  * Step 2: Instantiate a Slim application 
  16.  * 
  17.  * This example instantiates a Slim application using 
  18.  * its default settings. However, you will usually configure 
  19.  * your Slim application now by passing an associative array 
  20.  * of setting names and values into the application constructor. 
  21.  */  
  22. $app = new \Slim\Slim();  
  23.   
  24. /** 
  25.  * Step 3: Define the Slim application routes 
  26.  * 
  27.  * Here we define several Slim application routes that respond 
  28.  * to appropriate HTTP request methods. In this example, the second 
  29.  * argument for `Slim::get`, `Slim::post`, `Slim::put`, `Slim::patch`, and `Slim::delete` 
  30.  * is an anonymous function. 
  31.  */  
  32.   
  33. // GET route  
  34. $app->get(  
  35.     '/',  
  36.     function () {  
  37.         echo 'Hello Slim';  
  38.     }  
  39. );  
  40.   
  41. // POST route  
  42. $app->post(  
  43.     '/post',  
  44.     function () {  
  45.         echo 'This is a POST route';  
  46.     }  
  47. );  
  48.   
  49. // PUT route  
  50. $app->put(  
  51.     '/put',  
  52.     function () {  
  53.         echo 'This is a PUT route';  
  54.     }  
  55. );  
  56.   
  57. // PATCH route  
  58. $app->patch('/patch'function () {  
  59.     echo 'This is a PATCH route';  
  60. });  
  61.   
  62. // DELETE route  
  63. $app->delete(  
  64.     '/delete',  
  65.     function () {  
  66.         echo 'This is a DELETE route';  
  67.     }  
  68. );  
  69.   
  70. /** 
  71.  * Step 4: Run the Slim application 
  72.  * 
  73.  * This method should be called last. This executes the Slim application 
  74.  * and returns the HTTP response to the HTTP client. 
  75.  */  
  76. $app->run();  
  77.   
  78.     此时再打开浏览器输入localhost将只能看到以下内容,其实浏览器使用get方法,在slim的Get路由中输出了Hello Slim。  
  79. $app->post(  
  80.     '/post',  
  81.     function () {  
  82.         echo 'This is a POST route';  
  83.     }  
  84. );  


在slim中, '/post'为相对路径,该路径可支持变量。 function ()为后续的处理函数。其他HTTP方法也类似。
PHP微型框架slim的安装使用
图3 Slim Get路由
      其他类型的测试方法可借助cURL工具
    【1】测试post
    curl --request POST http://localhost/post
    【2】测试put方法
    curl --request PUT http://localhost/put
    【3】测试delete
    curl --request DELETE http://localhost/delete
    
    【火狐浏览器】
    如果你不喜欢使用curl工具,也可以选择火狐浏览器中的HTTPRequest工具,那么命令操作就成了愉快的GUI操作了。
PHP微型框架slim的安装使用