Silex 튜토리얼

Silex 튜토리얼

1 사전작업[ | ]

2 index.php 작성[ | ]

  • 보통 웹루트폴더(DOCUMENT_ROOT)에 설정하는데, 본 실습에서는 DOCUMENT_ROOT/myapi 폴더에 설정한다.
root@zetawiki:~# cd /var/www/html
root@zetawiki:/var/www/html# mkdir myapi
root@zetawiki:/var/www/html# cd myapi/
root@zetawiki:/var/www/html/myapi# vi index.php
<?php
require 'silex/autoload.php';
$app = new Silex\Application();
$app->get('/', function() use($app) { 
    return 'Hello World!';
});
$app->get('/hello/{name}', function($name) use($app) { 
    return 'Hello '.$app->escape($name); 
});
$app->run();

3 아파치 rewrite 설정[ | ]

(CentOS) /etc/httpd/conf/httpd.conf
(우분투) /etc/apache2/sites-enabled/000-default.conf[1]
<Directory "/var/www/html/myapi">
    AllowOverride all
</Directory>
root@zetawiki:/var/www/html/myapi# /etc/init.d/apache2 restart
 * Restarting web server apache2                                                            
 ... waiting                                                                        [ OK ]
root@zetawiki:/var/www/html/myapi# vi .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?u=$1 [L,QSA]

4 테스트[ | ]

root@zetawiki:/var/www/html/myapi# cd
root@zetawiki:~# curl http://localhost/myapi/index.php
안녕 친구들!
root@zetawiki:~# curl http://localhost/myapi/
안녕 친구들!
root@zetawiki:~# curl -s http://localhost/myapi/hello | grep '<h1>'
                <h1>Sorry, the page you are looking for could not be found.</h1>
root@zetawiki:~# curl -s http://localhost/myapi/hello/asdf
안녕 asdf

5 같이 보기[ | ]

6 주석[ | ]

  1. 우분투12는 /etc/apache2/sites-enabled/000-default
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}