Thinkphp's SAE development related notes detailed _php example

Source: Internet
Author: User
Tags error handling php template system log in domain smarty template to domain

This article details the thinkphp of the SAE development-related considerations. Share to everyone for your reference, specific as follows:

Thinkphp SAE development and standard version of the thinkphp basically the same, you do not need to understand the SAE interface usage, thinkphp SAE engine has automatically integrated the SAE interface for you, as long as the control of thinkphp Development, You will be able to easily master the SAE development based on thinkphp.

Here are some of the things we've given you to take advantage of the SAE engine development process to help you complete the development and deployment of the SAE.

Configuration

The SAE engine runs with SAE's own custom configuration and proprietary configuration, so the configuration file loading order is:

Convention Configuration-> Project configuration->sae Convention configuration->sae Proprietary configuration

The configuration items in the SAE Convention configuration and the SAE proprietary configuration will overwrite the project configuration.
Sae Convention configuration: Located in the engine directory/sae/conf/convention_sae.php, which defines the database connection configuration items that are fixed when the program runs on the SAE.
SAE proprietary configuration: Located in the project's conf directory, the file name is config_sae.php, you can write to the SAE configuration.

Note: The SAE Convention configuration and SAE proprietary configuration are unique configurations for the SAE environment and will not be loaded when run locally.

Database

Developers do not need to define an SAE-related database configuration item in the project configuration file (config.php), only to define a database to connect to when debugging locally. When the code is submitted to the SAE, it can also run without modifying any configuration items, because the SAE Convention configuration automatically overwrites the database configuration in your project configuration file.

When code runs on the SAE, it makes a distributed database connection and reads and writes the separation.

Cache

In the SAE development process, you can still use the thinkphp built-in caching method for processing. Here is the difference between the SAE engine using different caching methods under the local and SAE platforms (note that the SAE engine will automatically judge the processing):

Caching methods Run locally SAE Platform
S cache The default is implemented using the file method Fixed use of memcache implementation, so under the SAE Data_cache_type configuration items will fail. If you need to use the Mecache service provided by SAE, using the S function directly can
F Cache Using the file implementation Using KVDB to implement
Static caching Generating static HTML files Static file stored in Kvdb
SQL queues Supports file, XCache, and APC methods Using KVDB Storage

The new version of thinkphp supports SQL cache queue functionality, and we can configure Db_sql_build_cache to turn on SQL statement resolution caching. Fixed use of the KVDB storage SQL cache under the SAE platform, so db_sql_build_queue configuration items will not work. and run under the SAE with the counter service record SQL cache queue number of queues, in the counter management background

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

If you see the value of the calculator name Think_queue_out_times is very large, you set the number of queues is too small, you need to adjust the Db_sql_build_length configuration items.

File Upload

File uploads still use the UploadFile extension class library to upload files, using the same method. The same code is uploaded to the specified directory when it is run locally, and the storage service is automatically used on the SAE to upload the file to the specified storage. First you need to create a storage domain on the SAE platform for storing uploaded files:

Http://sae.sina.com.cn/?m=storage

Here you can build multiple domain. Which domain is uploaded to our file is determined by the first directory name of the upload path. Such as:

$upload->savepath = './public/uploads/';

will be uploaded to domain named public. You don't have to create uploads folders in this domain, and the SAE storage service will be created automatically for you.

Problem with picture address:

We use the UploadFile class upload picture, in the local and under the SAE image browsing address is not the same. For example, there is a picture address "/public/upload/1.jpg",/public is a template replacement variable, he will be replaced with the address of the directory where the public folder resides, we can view the browser's source code features to see what effect is replaced. Can be seen, replaced by "/public/upload/1.jpg". But the picture on the SAE is not in the Public/upload directory, but in the storage. We need to replace the/public/with the storage domain name, on the SAE to normal display.

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, the SAE will replace the/public/upload with the storage address, on the SAE picture can be normal display.

File deletion problem:

Because the uploaded file is not the same place as the SAE store, we can't delete the file directly with unlink. SAE version thinkphp new Sae_unlink function implementation compatibility. Such as:

Sae_unlink ('./public/uploads/xxx.jpg ');

When run locally, the picture under the Public/uploads folder is deleted. When run on the SAE, the picture in storage in domain is deleted. This function deletes which Domian file is also determined by the first directory name of the path.

Picture processing

SAE engine in the image processing has also done automatic processing, in the local and SAE platform difference is as follows:

Picture Features Run locally SAE Platform
Thumbnail image Call Image Class Library processing Automatic use of the Saeimage service
Verification Code Call Image Class Library processing Automatic use of the Saevcode service

You do not have to learn how to use Saeimage to generate thumbnails, and do not need to learn how to use the Saevcode service, you still use the thinkphp for the previous way to verify the code and thumbnail function can be.

When using the CAPTCHA, it is necessary to note that the validation code defaults to digital form while running on the SAE, while the verification code is numeric + alphabetic, and there are letter capitalization problems. If you want the CAPTCHA to be case-sensitive, you need to convert the verification code to uppercase and match.

Such as:

if (MD5 (Strtoupper ($_post[' verify '))!=$_session[' Verify ']) {
 //validation error handling code
}

Log records

The SAE version thinkphp also implements the build system log function, where running will record the log to the project's project's Runtime/logs folder, while running on the SAE will log the logs to the SAE platform's log Center:

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

Please select "Debug" in the dropdown menu in the Search box selection to view it.

Trace Information

It is recommended that you configure Show_page_trace=>true to open page TRACE information when you develop your program. When it is turned on, the code will display some of the SAE's unique trace information in the SAE environment to help us develop it. You may be able to go to the following trace information.

Template caching: Trace information name is "[SAE] Template Cache"

The template compilation cache is not generated under the SAE in the runtime directory, but is stored in the memcache. If you want to view the cached cache after the template is compiled, this shows the cached name of the template cache in Memcache. You can get cached content in the SAE Memcache service management platform by entering the cache name:

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

Note: The cached content you are looking at begins with a string of numbers that is independent of the cached content and is the cache generation time of the record.

Core cache: Trace Information name is "[SAE] Core cache"

It records the cache name of the core compilation cache in Memcache. If you want to get the core compilation cache, for example, we need to use the core compilation cache instead of the portal file. You can enter the cache name obtained here in the SAE Memcache service management platform.

Note:

The core compilation cache is not generated when debugging is turned on, and if you get the core compilation cache, turn off debugging first.

The number at the beginning of the cached content is the cache generation time of the record, please remove the number and use it as the entry file.

Static caching: Trace information name is "[SAE] Static Cache"

It records the name of the generated static cache in Kvdb. At present, SAE management platform can not directly input KVDB name to obtain content, we need to write their own program to obtain content.

Note: This trace information occurs only when a static cache is generated. This trace information will not be available if the page you are accessing does not perform the operation of generating a static cache.

Hide index.php

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

Create a Config.yaml file in your project's root directory, the code is:

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

So we can hide the entrance.

such as the address HTTP://SERVERNAME/INDEX.PHP/BLOG/READ/ID/1 can also pass

HTTP://SERVERNAME/BLOG/READ/ID/1 access.

Code across recommendations

SAE version thinkphp, is a cross-cutting, please do not destroy it across the sex. For example, do not write and SAE database-related configuration items in the project configuration file. Write your own code, but also as far as possible across, so that the same code can be run under the SAE, but also in the ordinary environment to run, so that you can upload the local debugging after the SAE also do not need to modify any code to run.

Here are some tips to keep your code across:

(1) Use the original SAE service as little as possible

can use thinkphp function substitution, try to use thinkphp function. For example, to use the SAE Kvdb service, in the thinkphp can be completely replaced by the F function. If you want to use the SAE Memcache service, use the S function to implement it. This will not cause your code to move from the SAE to the general environment after the performance is very low.

Individual SAE services cannot be replaced with thinkphp functions to consider the use of native SAE services.

(2) Using Is_sae constants

Thinkphp's SAE engine adds Is_sae constants to determine whether the code is running in a normal or SAE environment. If you have a piece of code in the general environment and in the SAE environment to implement different ways, you can use Is_sae to do different processing or load different files.

(3) using SAE proprietary documents

In the SAE convention configuration, we can see that there is a Sae_specialized_files configuration entry in addition to a fixed database configuration entry, which defines the system-specific files. The SAE proprietary files for the UploadFile class and the image class are currently defined, so when our code import ("@.org.uploadfile") is run locally, the project lib/org/uploadfile.class.php files are imported in the normal way. And on the SAE run is the system check to UploadFile.class.php has SAE proprietary files, which are imported into the Sae_specialized_files configuration items defined in the file address. This enables the common environment and the SAE environment the same code imported into the different classes of libraries, and the class call methods are the same, but the actual method is different, so that the code can be guaranteed across.

You can also create your own SAE proprietary files, and you can place proprietary files in the same directory as normal files so that you do not have to define Sae_specialized_file configuration items and the system can recognize proprietary files. Like our Image.class.php files. If a file named Image_sae.class.php is defined, when run on the system SAE, the Image.class.php file is imported instead Image_ sae.class.php files.

If a class library defines both the proprietary files under the sibling directory and the Sae_specialized_file configuration items, the proprietary files in the sibling directory are imported preferentially. We recommend that if you need to establish a proprietary file, the normal file in the same-level directory to establish.

If the imported class library does not have SAE proprietary files, running under the SAE also imports the normal files.

We can use SAE proprietary files for the general environment and SAE environment to encapsulate different class libraries, but the use of class library is the same, so that the class library's client code across.

(4) using SAE proprietary configuration

When the SAE and general environment configuration need not be the same, you can write the configuration of the general environment to the project configuration file conf/config.php, and the SAE need to use the configuration of the SAE exclusive configuration conf/config_sae.php.

More interested in thinkphp related content readers can view the site topics: "thinkphp Introductory Course", "thinkphp Template Operation Skills Summary", "thinkphp Common Methods Summary", "Smarty Template Introductory Course" and "PHP template technology Summary."

I hope this article will help you with the PHP program design based on thinkphp framework.

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.