最近由於工作需要,在公司需要部署一個webservice 服務端,經別人介紹及個人綜合判斷,決定採用Slim框架來編寫。
在網上專門搜索了關於Slim的一些文章,在這些文章的幫助下,自己完整在電腦上安裝上了Slim及簡單的測試,在接下來將要編寫業務需要的webservice數據轉發接口,在此將Slim的安裝、測試記錄下來,幫助自己,也幫助後面可能需要用到的人。
1、安裝web服務器
在安裝Slim前,需要在你所安裝的電腦上安裝好web服務器,大部分都是推薦用Wampserver,可以幫助你快速安裝好所需要的Apache+PHP+Mysql。
關於Wampserver,自己可以上網搜搜,資料很多。針對自己電腦的操作系統、版本、下載合適的軟件,然後按照指示,直接下一步就可以。推薦教程:windows安裝
2、重新配置Apache
安裝好web服務器後,需要重新配置Apache,配置如下:
slim採用了前端控制器模式,利用.htaccess文件中的重寫規則,把HTTP請求轉給了 index.php。所以還需要啓用 mod_rewrite,並修改httpd.conf文件中相應Directory指令的AllowOverride None爲AllowOverride All。http.conf位於wampserver安裝目錄\bin\apache\Apache2.4.4\conf
【1】啓動mod_rewrite——去掉#,#代表註釋
LoadModule rewrite_module modules/mod_rewrite.so
【2】修改AllowOverride None爲AllowOverride All
#
# Deny access to the entirety of your server's filesystem. You must
# explicitly permit access to web content directories in other
# <Directory> blocks below.
#
<Directory />
AllowOverride All
Require all granted
</Directory>
【3】訪問服務器合法IP——任何IP均可以訪問服務器。
註釋Deny from all,插入Allow from all任何
DocumentRoot "D:/wamp/www"
<Directory "D:/wamp/www">
Options Indexes FollowSymLinks
AllowOverride All
Order Deny,Allow
# Deny from all
Allow from all
</Directory>
【4】重啓apache服務
3、Slim的安裝
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app2=new \Slim\Slim();
$app2->get('/hello/:name', function ($name) {
echo 'hello'.' '.$name;
});
$app2->run();
?>
編輯結束後,重啓Apache,可在 Apache ->Service->Restart 。然後在瀏覽器中輸入http://localhost/SlimTwigExample/hello/zhang,就會在頁面中顯示 hello zhang