PHP development tool-prado 1.6 advanced Topics

Source: Internet
Author: User
Tags implement php programming require requires sessions
Ado| Advanced

4th Chapter Advanced Topics

4.1 Maintenance of display status

The display state of a component refers to the data that the component should always hold in a series of postback. For example, when a user changes the font color of a component in a page, he wants the page to be displayed again, and still see the color that has been modified. In the development of desktop GUI programs, this is very simple. But it's not so easy in Web programming because the Web server doesn't always keep the page state in memory. Prado The concept of the display state of ASP.net to solve this problem.

If you want to persist some data by displaying the state, you can call the getviewstate () and setviewstate () methods in the Tcomponent class. You can maintain many types of data in the display state, but you do not need to use it in resource handles, such as database joins.

PRADO

Returned to the user with a restricted state saved in a hidden field. When the user submits the page and displays the page again, the data in the display state is read from the submitted hidden field and the status of the page is restored. You can customize the storage method of the display state by using overloaded methods. For example, you can save a display state in session or in a database to avoid transferring large amounts of data through hidden fields.

4.2 Session

  Session is used to save the state between the different pages, which is different from the display state mechanism of the page, the display state can only save the status of this page.

If you want to use session, first define a session class in the applied configuration file. The session class must implement ISession
Interface. If you don't want to write a session class yourself, you can use the class System.Security.TSession , which encapsulates the $_session variable to implement the ISession interface. You can also write a session class to work with sessions in different ways (such as saving sessions to a database).

If the session class is defined, the corresponding session object is created before the requested page is created. The tpage Page class has a session attribute that makes it easy to visit the session object.

4.3 Authentication and Authorization

PRADO provides a validation/authorization framework to support visitors to the validation page and determine whether they have permission to access the page.

In the validation framework, a user class plays a major role. If a visitor passes a certain authentication (such as a login) and obtains a correct identity on the server side, the validation passes. The user object represents such an identity. In addition to identity checks, some pages require additional permission checks to ensure that authenticated identities have sufficient permissions to access them.

The validation framework supports role-based validation. A page if the required validation (secured) is named in the application's configuration page, the framework will ensure that the user must be authenticated before accessing the page. If you also specify a role, the framework will also ensure that the user belongs to that role. Finally, you can customize the validation process for the page. If authentication or authorization fails, theonauthenticationrequired or onauthorizationrequired method is triggered, where an error message can be displayed or redirected to the login page.

The user object always exists on a different page, and it uses a session. Therefore, if you want to use the validation framework, you must declare the session class. Because the user object is saved in session, you can use it to save some data that needs to be accessed on different pages.

If you want to use the validation framework, you need to declare a user class in the application's configuration file. The user class must implement the Iuser interface. A class System.Security.TUser that implements the interface is already provided in the framework.

Phonebook This example illustrates the validation framework if it is used.

4.4 Form Validation

  Prado provides an easy way to validate forms, and if you use traditional PHP programming, the process is very repetitive and monotonous. A set of validation components in Prado. These validation components allow data to be validated on the server side as well as at the client, and we'll give you an example here.

Now we are going to create a user registration page. This page requires the user to enter a username and a password to create a new account. Password requirements are entered two times to prevent input errors. In addition, the username and password can only contain numbers and numbers, and they require no less than 3-bit and 6-bit lengths respectively.

To implement this feature, you can create a template that:

...
<com:TForm>
...
Username:
<com:ttextbox id= "username"/>
<com:trequiredfieldvalidator
Controltovalidate= "username"
Errormessage= "You must choose a username."/>
<com:tregularexpressionvalidator
Controltovalidate= "username"
Regularexpression= "[\w]{3,}"
Errormessage= "Username must ..."/>
<br/>
Password:
<com:ttextbox id= "password" textmode= "password"/>
<com:trequiredfieldvalidator
controltovalidate= "Password"
Errormessage= "You must choose a password."/>
<com:tregularexpressionvalidator
controltovalidate= "Password"
Regularexpression= "[\w]{6,}"
Errormessage= "Password must ..."/>
<br/>
Repeat Password:
<com:ttextbox id= "Password2" textmode= "Password"/>
<com:trequiredfieldvalidator
Controltovalidate= "Password2"
Errormessage= "Please re-type your password."/>
<com:tcomparevalidator
Controltovalidate= "Password2"
controltocompare= "Password"
errormessage= "Your password entries did not match."/>
<br/>
<com:tbutton text= "Register"/>
...
</com:TForm>
...

The above template allows the page to perform client authentication and server-side validation of user input (assuming browsers support JavaScript). No additional code is needed. If the validation fails, the form is not committed, and an error message is displayed.

You can turn off client authentication by setting the EnableClientScript property of the validation component in the template file. At this point, you can check the page's IsValid property to see if the validation passes through the OnLoad event in the page or later.

4.5 Caching

  The Prado Framework provides a caching mechanism to improve the operational efficiency of Prado applications. For each component type, if it has been instantiated before, then a corresponding file exists in the cache directory. This file contains the serialized instance of the control. Once you need to create the same component later, you can create it directly from the cache file. This will greatly save the creation time of the component because the process of creating the component requires parsing several XML specification files and template files.

If you want to use caching, you only need to set a directory for the properties in the applied configuration file. This directory must be writable by the Web server. You can use a path that is either an absolute or a relative path.

If you want to turn off caching, you only need to assign an empty string to the property.

Note that if you modify the component's files, you should include all cached files deleted or removed. The next time the component is created, it is re-created automatically. So when developing, you should turn off the cache.

4.6 Custom Tapplication

  The tapplication class can be expanded. There are several ways that tapplication can be overloaded. For example, you can overload the beginrequest () method to perform some of the required preprocessing of the request data. If you want to learn more about this, you can refer to Prado's documentation.



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.