当前位置: 首页 > 工具软件 > Slim Lang > 使用案例 >

slim:入门(二)运行slim

宇文嘉勋
2023-12-01

入门(二)运行slim

  • 创建入口文件
# public/index.php
<?php

require '../vendor/autoload.php';

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

$app = new \Slim\App;
$app->get('/user/{name}', function (Request $request, Response $response) {
    $name = $request->getAttribute('name');
    $response->getBody()->write("Hello, $name");

    return $response;
});

$app->run();
  • nginx 配置
server {

    listen			80;
    server_name		slim.my;
    root			/Users/akio/develop/slim/public/;
    
    access_log		/Applications/MxSrvs/logs/slim.access.log;
    error_log		/Applications/MxSrvs/logs/slim.error.log;
    
    index index.php index.html index.htm;
    #limit_conn one 10;
    
    if (!-e $request_filename){
        rewrite ^(.*)$ /index.php break;
    }
    location ~ \.php$ {
    include         fastcgi_params;
    fastcgi_pass    127.0.0.1:10080;
    fastcgi_index   index.php;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

cd public
php -S localhost:8080
  • 测试访问
http://localhost:8080/user/akio

slim.my/user/akio

code

 类似资料: