This article details the considerations related to SAE development of ThinkPHP. I share it with you for your reference. The details are as follows:
ThinkPHP’s SAE development is basically the same as the standard version of ThinkPHP. You don’t need to know how to use the SAE interface. ThinkPHP’s SAE engine has automatically integrated the SAE interface for you. As long as you master ThinkPHP development, you can easily master SAE development based on ThinkPHP.
The following are some precautions we give when using the SAE engine development process, which can help you better complete the development and deployment of SAE.
Configuration
SAE engine has SAE's own conventional configuration and proprietary configuration when running, so the configuration file loading sequence is:
Conventional configuration->Project configuration-> ;SAE conventional configuration->SAE proprietary configuration
Configuration items in SAE conventional configuration and SAE proprietary configuration will override the project configuration.
SAE convention configuration: located in the engine directory/Sae/Conf/convention_sae.php, which defines fixed database connection configuration items when the program runs on SAE.
SAE proprietary configuration: Located in the Conf directory of the project, the file name is config_sae.php. You can write the SAE configuration into it.
Note: SAE conventional configuration and SAE proprietary configuration are unique configurations for the SAE environment and will not be loaded when running locally.
Database
Developers do not need to define SAE-related database configuration items in the project configuration file (config.php), they only need to define the database to connect to during local debugging. When the code is submitted to SAE, it can run without modifying any configuration items, because the SAE convention configuration will automatically override the database configuration in your project configuration file.
When the code runs on SAE, it will connect to the distributed database and separate reading and writing.
Caching
During the SAE development process, you can still use ThinkPHP's built-in caching method for processing. The following is the difference between the SAE engine using different caching methods locally and on the SAE platform (note that the SAE engine will automatically determine and process this difference):
The new version of ThinkPHP supports SQL caching For the queue function, we can configure DB_SQL_BUILD_CACHE to turn on the SQL statement parsing cache. Under the SAE platform, KVDB is used to store the SQL cache, so the DB_SQL_BUILD_QUEUE configuration item will not work. And when running under SAE, the Counter service will be used to record the number of SQL cache queue dequeues. In the Counter management background
http://sae.sina.com.cn/?m=counter
If you see a large value in the calculator named think_queue_out_times, it means that the number of queues you set is too small and you need to adjust the DB_SQL_BUILD_LENGTH configuration item.
File upload
File upload still uses the UploadFile extension class library to upload files, and the usage method remains unchanged. The same code will be uploaded to the specified directory when running locally. When running on SAE, the Storage service will be automatically used to upload files to the specified Storage. First you need to create a Storage domain on the SAE platform to store uploaded files:
http://sae.sina.com.cn/?m=storage
You can create it here Multiple domains. The domain to which our files will be uploaded is determined by the name of the first directory in the upload path. For example:
$upload->savePath = './Public/Uploads/';
will be uploaded to the domain named Public. You don't need to create the Uploads folder in this domain, SAE's Storage service will automatically create it for you.
Image address problem:
We use the UploadFile class to upload images. The browsing addresses of images locally and under SAE are different. For example, there is a picture with the address "/Public/upload/1.jpg", /Public is a template substitution variable, and it will be replaced with the address of the directory where the Public folder is located. We can view it through the browser's source code function. What is the effect after replacement? As you can see, it is replaced with "/Public/upload/1.jpg". But on SAE, the pictures are not in the Public/upload directory, but in storage. We need to replace /Public/ with the domain name of the storage so that it can be displayed normally on SAE.
We define the following code in the SAE proprietary configuration Conf/config_sae.php file:
<?php return array( 'TMPL_PARSE_STRING'=>array( '/Public/upload'=>sae_storage_root('Public').'/upload' ) );
In this way, / will be placed on SAE Public/upload is replaced with the storage address, and the image can be displayed normally on SAE.
File deletion problem:
Because the uploaded file is stored locally and in SAE, we cannot directly use unlink to delete the file. The SAE version of ThinkPHP has added the sae_unlink function to achieve compatibility. For example:
sae_unlink('./Public/Uploads/xxx.jpg');
When running locally, the images in the Public/Uploads folder will be deleted. When running on SAE, the images in the Storage whose domain is Public will be deleted. Which domian file this function will delete is also determined by the first directory name of the path.
Image processing
The SAE engine also performs automatic processing in image processing. The differences between the local and SAE platforms are as follows:
你完全不用去学习怎么用SaeImage生成缩略图,也不用学习SaeVcode服务怎么用,你还是按照以前的方式使用ThinkPHP进行验证码和缩略图功能就可以了。
使用验证码的时候需要注意,在本地运行时验证码默认为数字形式,而在SAE上运行时验证码为数字+字母形式,而且存在字母大小写问题。如果你希望验证码区分大小写的话,需要将验证码统一转化为大写后进行匹配。
如:
if(md5(strtoupper($_POST['verify']))!=$_SESSION['verify']){ //验证错误处理代码 }
日志记录
SAE版ThinkPHP同样实现了生成系统日志功能,在本地运行会将日志记录到项目的项目的Runtime/Logs文件夹下,而在SAE上运行会将日志记录到SAE平台的日志中心:
http://sae.sina.com.cn/?m=applog
请在搜索框选择中的下拉菜单处选择“debug”进行查看。
Trace信息
建议在开发程序时配置SHOW_PAGE_TRACE=>true 开启页面Trace信息。开启后,代码在SAE环境下运行时会显示一些SAE独有的Trace信息,有助于我们开发。你可能会到以下trace信息。
模板缓存:Trace信息名称为“[SAE]模板缓存”
在SAE下不会将模板编译缓存生成在Runtime目录下,而是存放在Memcache中。如果你想查看模板编译后的缓存,这里显示的就是模板缓存在Memcache中的缓存名称。你可以在SAE的memcache服务管理平台输入缓存名称得到缓存内容:
http://sae.sina.com.cn/?m=mcmng
注:你看得的缓存内容,都是以一串数字开始,这数字和缓存内容无关,是记录的缓存生成时间。
核心缓存:Trace信息名称为“[SAE]核心缓存”
它记录的是核心编译缓存在Memcache中的缓存名称。如果你要获得核心编译缓存,比如我们要用核心编译缓存代替入口文件的时候。你可以在SAE的Memcache服务管理平台 输入这里记录的缓存名称获得。
注:
在开启调试时不会生成核心编译缓存,如果你获得核心编译缓存,请先关闭调试。
缓存内容开头的数字是记录的缓存生成时间,请将数字去掉后再作为入口文件。
静态缓存:Trace信息名称为“[SAE]静态缓存”
它记录了生成的静态缓存在KVDB中的名称。 目前SAE管理平台没有能直接输入KVDB名称获得内容的地方,大家需要自己写程序获取内容。
注:此Trace信息是在生成静态缓存的时候才会出现。如果你访问到的页面没有执行生成静态缓存的操作时,将不会有此条Trace信息。
隐藏index.php
SAE不支持.htaccess文件,但我们可以使用SAE提供的AppConfig服务实现伪静态。
在你项目的根目录建立config.yaml文件,代码为:
handle: - rewrite: if(!is_dir() && !is_file() && path~"^(.*)$") goto "index.php/$1"
这样就可以隐藏入口了。
比如这样的地址 http://serverName/index.php/Blog/read/id/1也能通过
http://serverName/Blog/read/id/1访问。
代码横跨性建议
SAE版ThinkPHP,是具有横跨性的,请不要破坏它的横跨性。比如,不要在项目配置文件中写和SAE数据库相关配置项。 自己写代码时,也要尽量做到横跨性,这样就可以让同样的代码既能在SAE下运行,也能在普通环境下运行, 使你在本地调试完后上传到SAE也不用修改任何代码就能运行。
下面是一些保持代码横跨性的建议:
(1)尽量少使用原生的SAE服务
能使用ThinkPHP自带函数替代的,尽量使用ThinkPHP自带函数。比如要使用SAE的KVDB服务,在ThinkPHP中完全可以用F函数代替。如果要使用SAE的Memcache服务,都使用S函数实现。 这样就不会导致你的代码从SAE转移到普通环境后性能很低。
个别SAE服务无法使用ThinkPHP自带函数代替的,才考虑使用原生的SAE服务。
(2)利用IS_SAE常量
ThinkPHP的SAE引擎增加了IS_SAE常量,能判断代码运行环境是普通环境还是SAE环境。如果你有段代码在普通环境和在SAE环境下实现方式不同,你可以使用IS_SAE进行判断后做不同处理或者加载不同的文件。
(3)利用SAE专有文件
在SAE惯例配置中,我们可以看见除了配置了固定的数据库配置项,还有一个SAE_SPECIALIZED_FILES配置项,它定义了系统专有文件。目前已经定义了UploadFile类和Image类的SAE专有文件,所以当我们的代码 import(“@.ORG.UploadFile”) 在本地运行时会按普通方式导入项目下Lib/ORG/UploadFile.class.php文件, 而在SAE上运行是系统检查到UploadFile.class.php有SAE专有文件,它导入的是SAE_SPECIALIZED_FILES配置项中定义的文件地址。这样实现了普通环境和SAE环境下同样的代码导入了不同类库,而类的调用方法都是一样的,只是现实方法不同,这样就能保证了代码的横跨性。
You can also create SAE proprietary files yourself. You can place the proprietary files in the same directory as the ordinary files, so that the system can recognize the proprietary files without defining the SAE_SPECIALIZED_FILE configuration item. For example, if we define a file named Image_sae.class.php in the same directory as the Image.class.php file, when running on the system SAE, when importing the Image.class.php file, it will import Image_sae.class instead. .php files.
If a class library defines both proprietary files in the same directory and is also defined in the SAE_SPECIALIZED_FILE configuration item, the proprietary files in the same directory will be imported first. It is recommended that if you need to create a proprietary file, create it in the same directory as the ordinary file.
If the imported class library does not have SAE proprietary files, ordinary files will also be imported when running under SAE.
We can use SAE proprietary files to encapsulate different class libraries for the normal environment and SAE environment, but the usage methods of the class libraries are the same, so that the client code of the class library is cross-functional.
(4) Utilize SAE proprietary configuration
When you encounter SAE and ordinary environment configuration needs that are different, you can write the ordinary environment configuration to the project configuration file Conf/config.php , and write the configuration required by SAE into the SAE proprietary configuration Conf/config_sae.php.
I hope this article will be helpful to everyone’s PHP programming based on the ThinkPHP framework.
For more detailed explanations of matters needing attention in ThinkPHP's SAE development, please pay attention to the PHP Chinese website!