最簡單粗暴直接的方法-到github下載zip文件,slim github【連結】。解壓縮之後把【1】Slim資料夾,【2】.htaccess檔案和【3】index.php檔案複製到www目錄中。若看到以下網頁說明slim安裝成功。
圖2 slim安裝成功
Slim提供完善的REST框架,支援GET、POST、PUT和Delete等方法,可以把index.php修改的更簡單一些。可從以下程式碼中可以熟悉Slim的基本框架和使用方法。
[php] view plain copy
#
<?php /** * Step 1: Require the Slim Framework * * If you are not using Composer, you need to require the * Slim Framework and register its PSR-0 autoloader. * * If you are using Composer, you can skip this step. */ require 'Slim/Slim.php'; \Slim\Slim::registerAutoloader(); /** * Step 2: Instantiate a Slim application * * This example instantiates a Slim application using * its default settings. However, you will usually configure * your Slim application now by passing an associative array * of setting names and values into the application constructor. */ $app = new \Slim\Slim(); /** * Step 3: Define the Slim application routes * * Here we define several Slim application routes that respond * to appropriate HTTP request methods. In this example, the second * argument for `Slim::get`, `Slim::post`, `Slim::put`, `Slim::patch`, and `Slim::delete` * is an anonymous function. */ // GET route $app->get( '/', function () { echo 'Hello Slim'; } ); // POST route $app->post( '/post', function () { echo 'This is a POST route'; } ); // PUT route $app->put( '/put', function () { echo 'This is a PUT route'; } ); // PATCH route $app->patch('/patch', function () { echo 'This is a PATCH route'; }); // DELETE route $app->delete( '/delete', function () { echo 'This is a DELETE route'; } ); /** * Step 4: Run the Slim application * * This method should be called last. This executes the Slim application * and returns the HTTP response to the HTTP client. */ $app->run(); 此时再打开浏览器输入localhost将只能看到以下内容,其实浏览器使用get方法,在slim的Get路由中输出了Hello Slim。 $app->post( '/post', function () { echo 'This is a POST route'; } );
在slim中, '/post'為相對路徑,路徑可支援變數。 function ()為後續的處理函數。其他HTTP方法也類似。
圖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的安裝使用方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!