Calculating Deployment Thinkphp 5.1 with Ali Cloud Function

Keywords: PHP

thinkphp is still used a lot in China, I use it myself. So we studied how to compute deployment tp5 with functions.
I learned from @rsong's article.< Ten Minutes Online-Function Computing Play WordPress>

Here is a demonstration of TP5: https://31199382.cn-beijing.fc.aliyuncs.com/2016-08-15/proxy/web-site/tp5/
In fact, there is nothing to demonstrate, that is, pure tp5 ()~*

Steps:

1. Background create php7.2 environment functions, create http triggers, these are not written in detail, you can see the document.

2. Open the entry file of tp5 (/ public/index.php) in namespace think; under this sentence, add the following code:

if(!$_SERVER['PATH_INFO']) $_SERVER['PATH_INFO'] = preg_replace("/^(\?s\=\/)/","",$_SERVER['REQUEST_URI']);

3. Prepare the tp5 source code and create the index.php file (as the entry file of the function) in the root directory. The code is as follows:
(The first time I wrote an article on Aliyun's blog, a bit handy)

<?php
#Custom domain names, bound to custom domain names, can be replaced by their own customized.
$MY_HOST    = "31199382.cn-beijing.fc.aliyuncs.com";
#web directory, default is tp5 public directory, absolute path, such as: / code/public
$WWW_DIR = '/code/public';

function handler($request, $context){

    #If you do not use the function to calculate the domain name of the factory provided by the background, this sentence can be commented out.
    if(strpos($request->getAttribute("requestURI"),"/2016-08-15/proxy") !== false) $request = clearFcHost($request,$context);#url address compatible with fc background

    $uri  = $request->getAttribute("requestURI");
    $file = explode("?", $uri)[0];
    if($file=='/') $uri='/';#
    $file = $GLOBALS['WWW_DIR'].$file;

    if(file_exists($file) and $uri!='/'){
        if(strpos($uri,".php")) return php_run(basename($file), $request, $context);#php_run
        return static_run($uri);#static_run
    }

    $request = $request->withAttribute("requestURI", "?s=".$uri);
    return php_run('index.php', $request, $context);# php_run

}

function php_run($name,$request, $context)
{
    return $GLOBALS['fcPhpCgiProxy']->requestPhpCgi($request, $GLOBALS['WWW_DIR'], $name,['SERVER_NAME' => $GLOBALS['MY_HOST'], 'SERVER_PORT' => '80', 'HTTP_HOST' => $GLOBALS['MY_HOST']],['debug_show_cgi_params' => false, 'readWriteTimeout' => 15000]);
}
use RingCentral\Psr7\Response;
function static_run($uri): Response{
    $file_dir = $GLOBALS['WWW_DIR'].$uri; #Full file path
    $file_dir = explode("?", $file_dir)[0]; #Remove dynamic paths
    if(is_dir($file_dir)) $file_dir .= '/index.html';# Default index pages for directories can be defined here
    $handle   = fopen($file_dir, "r");
    $contents = fread($handle, filesize($file_dir));
    fclose($handle);
    return new Response(200, ['Content-Type'  => $GLOBALS['fcPhpCgiProxy']->getMimeType($file_dir),'Cache-Control' => "max-age=8640000",'Accept-Ranges' => 'bytes'], $contents);
}

function clearFcHost($request,$context){
    $uri  = $request->getAttribute("requestURI");
    $uri  = str_replace("/2016-08-15/proxy/".$context['service']['name']."/".$context['function']['name'],"",$uri);
    $request = $request->withAttribute("requestURI", $uri);
    return $request;
}

#error handling
function error($code) {
    #if($resp->getStatusCode() !=200) return error($resp->getStatusCode());
    return 'There's still something to write about here, Ha-ha-ha-ha-ha-ha-ha-ha-ha-ha-ha-ha-ha-ha-ha-ha-ha-ha-ha-ha-ha-ha-ha-ha~~';
}

4. Since only the / tmp directory is writable for function calculation, we need to change the configuration file of tp5.
Edit/config/log.php

'path'  => '/tmp/log',

Edit/config/cache.php

'path'  => '/tmp/cache',

OK, so far we have deployed it.

ps. A simple test, there should be no problem. It was first published in Aliyun Blog. If you have any questions, please leave a message in the original text.

Posted by KFC on Thu, 31 Jan 2019 17:30:15 -0800