>  기사  >  백엔드 개발  >  PHP 프레임워크 Slim을 설치하고 사용하는 방법

PHP 프레임워크 Slim을 설치하고 사용하는 방법

小云云
小云云원래의
2018-03-20 11:39:317587검색


가장 간단하고 투박하며 직접적인 방법 - github, slim github [링크]에서 zip 파일을 다운로드하세요. 압축을 푼 후 [1] Slim 폴더, [2] .htaccess 파일, [3] index.php 파일을 www 디렉터리에 복사합니다. 다음과 같은 웹페이지가 보이면 slim이 성공적으로 설치되었음을 의미합니다.



그림 2 Slim이 성공적으로 설치되었습니다.


4. 간단한 수정 및 테스트

Slim은 GET, POST, PUT 및 삭제와 같은 방법을 지원하는 완전한 REST 프레임워크를 제공합니다. . Index.php를 더 간단하게 수정할 수 있습니다. 다음 코드를 통해 Slim의 기본 프레임워크와 사용법을 익힐 수 있습니다.


[php] 보기 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 &#39;Slim/Slim.php&#39;;  
  
\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(  
    &#39;/&#39;,  
    function () {  
        echo &#39;Hello Slim&#39;;  
    }  
);  
  
// POST route  
$app->post(  
    &#39;/post&#39;,  
    function () {  
        echo &#39;This is a POST route&#39;;  
    }  
);  
  
// PUT route  
$app->put(  
    &#39;/put&#39;,  
    function () {  
        echo &#39;This is a PUT route&#39;;  
    }  
);  
  
// PATCH route  
$app->patch(&#39;/patch&#39;, function () {  
    echo &#39;This is a PATCH route&#39;;  
});  
  
// DELETE route  
$app->delete(  
    &#39;/delete&#39;,  
    function () {  
        echo &#39;This is a DELETE route&#39;;  
    }  
);  
  
/** 
 * 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(  
    &#39;/post&#39;,  
    function () {  
        echo &#39;This is a POST route&#39;;  
    }  
);

슬림에서 '/post'는 변수를 지원할 수 있는 상대 경로입니다. function ()은 후속 처리 기능입니다. 다른 HTTP 메서드도 비슷합니다.



그림 3 Slim Get 라우팅

cURL 도구를 사용할 수 있는 다른 유형의 테스트 방법

[1] 테스트 게시물

컬 --request POST http://localhost/post

【2】 테스트 넣기 방법

컬 --request PUT http://localhost/put

【3】Test delete

컬 --request DELETE http://localhost/delete

【Firefox 브라우저】

컬 도구를 사용하고 싶지 않다면 다음을 수행하세요. 또한 서버에서 Firefox HTTPRequest 도구를 선택하면 명령 작업이 즐거운 GUI 작업이 됩니다.


위 내용은 PHP 프레임워크 Slim을 설치하고 사용하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.