ThinkPHP SAE development considerations, thinkphpsae

Source: Internet
Author: User

ThinkPHP SAE development considerations, thinkphpsae

This article describes ThinkPHP SAE development considerations in detail. We will share this with you for your reference. The details are as follows:

ThinkPHP's SAE development is basically the same as ThinkPHP in the standard version. You don't need to know the SAES Interface Usage. ThinkPHP's SAE engine has automatically integrated the SAES interface for you, as long as you know ThinkPHP development, you can easily master ThinkPHP-based SAE development.

The following are some notes for using the SAE engine for development, which can help you better develop and deploy the SAE.

Configuration

When the SAE engine is running, it has its own conventional configurations and proprietary configurations. Therefore, the configuration file loading sequence is as follows:

Conventional configuration-> project configuration-> SAE conventional configuration-> SAE proprietary Configuration

Configurations in SAE Convention configuration and SAE proprietary configuration will overwrite 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 configurations for SAE to it.

Note: SAE conventional configuration and SAE proprietary configuration are unique configurations for the SAE environment and will not be loaded during local operation.

Database

Developers do not need to define database configuration items related to SAE in the project configuration file (config. php). They only need to define the database connected 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 overwrite the database configuration in your project configuration file.

When the code is run on SAES, a distributed database is connected and read/write splitting is performed.

Cache

During SAE development, you can still use the built-in caching method of ThinkPHP for processing. The following is the difference between the local and SAE platforms that the SAE engine uses different caching methods (note that the difference is that the SAE engine will automatically determine the processing ):

Cache Method Run locally SAE Platform
S Cache File is used by default. Fixed implementation using Memcache, so the DATA_CACHE_TYPE configuration item in SAE will be invalid. If you need to use the Mecache service provided by SAE, you can directly use the S function.
F Cache Use File Implementation Using KVDB
Static Cache Generate static Html files Store static files in KVDB
SQL queue Supports File, Xcache, and APC Use KVDB for storage

The new ThinkPHP version supports the SQL cache queue function. We can configure DB_ SQL _BUILD_CACHE to enable the SQL statement parsing cache. KVDB is used to store SQL cache on the SAE platform. Therefore, the DB_ SQL _BUILD_QUEUE configuration item does not work. When running in SAE, the Counter service will be used to record the number of SQL cache queues, which is in the Counter management background.

Http://sae.sina.com.cn /? M = counter

If you see that the value of the calculator name think_queue_out_times is very large, it means that the number of queues you set is too small, 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. The usage remains unchanged. When the same code is run locally, it will be uploaded to the specified directory. When running on SAE, it will automatically use the Storage service 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

Multiple domains can be created here. Which domain will be uploaded to our file is determined by the name of the first directory in the upload path. For example:

$upload->savePath = './Public/Uploads/';

The domain name is Public. You do not need to create the Uploads folder in this domain. The Storage service of SAE will automatically create the folder for you.

Image address problems:

We use the UploadFile class to upload images. The browsing address on a local image is different from that on an SAE image. For example, if an image address is "/Public/upload/1.jpg" and/Public is a template replacement variable, it is replaced with the address of the directory where the Public folder is located, you can view the source code in the browser to view the effect after replacement. As you can see, replace it with "/Public/upload/1.jpg ". However, the SAE slices are not in the Public/upload directory, but in the storage. We need to replace/Public/with the storage domain name so that it can be properly displayed on SAE.

The following code is defined in the Conf/config_sae.php file of the SAE proprietary Configuration:

<?phpreturn array( 'TMPL_PARSE_STRING'=>array(   '/Public/upload'=>sae_storage_root('Public').'/upload' ));

In this way,/Public/upload is replaced with the storage address on the SAE, and can be displayed normally on the SAE chip.

File Deletion problems:

Because the uploaded files are stored in different places locally and in SAE, we cannot directly use unlink to delete the files. Sae_unlink function added to ThinkPHP for sae_unlink compatibility. For example:

sae_unlink('./Public/Uploads/xxx.jpg');

When running locally, images in the Public/Uploads folder are deleted. When running on SAE, images in Storage with the domain name Public are deleted. This function will delete which domian file is determined by the first directory name of the path.

Image Processing

The SAE engine also performs automatic image processing. The differences between the local image processing and the SAE platform are as follows:

Image Function Run locally SAE Platform
Thumbnail Call the Image class library for processing Automatic use of the SaeImage Service
Verification Code Call the Image class library for processing Automatic use of SaeVcode Service

You don't have to learn how to use SaeImage to generate thumbnails, or how to use the SaeVcode service. You can simply use ThinkPHP for verification code and thumbnails in the previous method.

When using the verification code, note that the verification code is in the digit format by default when running locally, while the verification code is in the digit + Letter format when running on SAE, and there is a case-insensitive problem. If you want the verification code to be case sensitive, you must convert the verification code to uppercase for matching.

For example:

If (md5 (strtoupper ($ _ POST ['verify '])! = $ _ SESSION ['verify ']) {// verification error handling code}

Log records

ThinkPHP of SAE also provides the system log generation function. Logs are recorded in the Runtime/Logs folder of the project during local running, on the SAE server, the shipping line records logs to the log center of the SAE platform:

Http://sae.sina.com.cn /? M = applog

Select "debug" from the drop-down menu in the search box.

Trace Information

We recommend that you configure SHOW_PAGE_TRACE => true to enable page Trace information during development. After it is enabled, the code running in the SAE environment will display some Trace information unique to the SAE, which is helpful for our development. You may obtain the following trace information.

Template cache: Trace information is named "[SAE] template cache"

In SAE, the template compilation cache is not generated in the Runtime directory, but stored in Memcache. If you want to view the compiled template cache, the cache name displayed here is the template cache name in Memcache. You can enter a cache name on the memcache Service Management Platform of SAE to get the cache content:

Http://sae.sina.com.cn /? M = mcmng

Note: The cache content you see starts with a string of numbers. This number is irrelevant to the cache content and is the cache generation time of the record.

Core cache: Trace information is named "[SAE] Core cache"

It records the cache name of the core compilation cache in Memcache. If you want to obtain the core compilation cache, for example, when we use the core compilation cache to replace the entry file. You can enter the cache name recorded here on the Memcache Service Management Platform of SAE.

Note:

The core compilation cache is not generated when debugging is enabled. If you obtain the core compilation cache, disable debugging first.

The number starting with the cached content is the cache generation time of the record. Remove the number and use it as the entry file.

Static cache: Trace information is named "[SAE] Static cache"

It records the name of the generated static cache in KVDB. Currently, no content can be obtained directly by entering the KVDB name on the SAE management platform. You need to write your own programs to obtain the content.

Note: This Trace information appears only when a static cache is generated. If the page you access does not execute the static cache generation operation, this Trace information will not exist.

Hide index. php

SAE does not support. htaccess files, but we can use the AppConfig service provided by SAE to implement pseudo-static.

Create the config. yaml file in the root directory of your project. The code is:

handle:- rewrite: if(!is_dir() && !is_file() && path~"^(.*)$") goto "index.php/$1"

In this way, the portal can be hidden.

For example, the address http: // serverName/index. php/Blog/read/id/1 can also pass through

Http: // serverName/Blog/read/id/1 access.

Code span recommendations

ThinkPHP of SAE is a cross-spanning version. Do not destroy it. For example, do not write configuration items related to the SAE database in the project configuration file. When writing your own code, you should also try to achieve cross-cutting, so that the same code can be run in both SAE and normal environments, this allows you to upload data to SAE after local debugging without modifying any code.

The following are some suggestions for keeping code spans:

(1) Try to use fewer native SAE services

If you can use the built-in functions of ThinkPHP, try to use the built-in functions of ThinkPHP. For example, if you want to use the KVDB service of SAE, you can replace it with the F function in ThinkPHP. If you want to use the Memcache service of SAE, use the S function. This will not cause your code to have low performance after being transferred from SAE to the normal environment.

Some SAE services cannot be replaced by the built-in ThinkPHP functions, so native SAE services are considered.

(2) Using IS_SAE Constants

The SAES engine of ThinkPHP adds the IS_SAE constant to determine whether the code runtime environment is a common environment or an SAE environment. If you have a code segment that is implemented differently in the normal environment and in the SAE environment, you can use IS_SAE to make judgments and perform different processing or load different files.

(3) using SAE proprietary files

In the SAE Convention configuration, we can see that in addition to configuring fixed database configuration items, there is also a SAE_SPECIALIZED_FILES configuration item that defines the system proprietary file. Currently, the UploadFile class and Image class SAE private files have been defined, so when our code import ("@. ORG. "UploadFile") during local running, the Lib/ORG/UploadFile under the project will be imported as normal. class. PHP file, while running on SAE is the system checking UploadFile. class. php has an SAE private file, which imports the file address defined in the SAE_SPECIALIZED_FILES configuration item. In this way, the same code in the general environment and the SAE environment is imported into different class libraries, and the class call methods are the same, but the actual methods are different, this ensures code overlay.

You can also create an SAE private file by yourself. You can store the private file in the same directory as the normal file, so that the system can identify the private file without defining the SAE_SPECIALIZED_FILE configuration item. For example, in Image. class. if a file named Image_sae.class.php is defined in the directory at the same level of the php file, import the Image. class. the PHP file is imported into the Image_sae.class.php file.

If a class library defines both the private files under the same level directory and the SAE_SPECIALIZED_FILE configuration item, the private files under the same level directory are imported first. If you need to create a private file, we recommend that you create it in the same directory as a common file.

If the imported class library does not have an SAE private file, a common file will be imported during running under SAE.

We can use the SAE proprietary file to encapsulate different class libraries for the common environment and the SAE environment, but the usage of the class libraries is the same, so that the client code of the class library is cross-cutting.

(4) use SAE proprietary Configuration

When the configurations of SAE and common environments are different, you can write the configurations of common environments to the project configuration file Conf/config. php, and write the configurations required by sae to the Conf/config_sae.php VPC configuration of SAE.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.