ZendFramework1.x tutorials for beginners-PHP source code

Source: Internet
Author: User
Tutorial 1. create a config. ini file under project/application/configs/and add the following code

[mysql]db.adapter = PDO_MYSQLdb.params.host = 127.0.0.1db.params.username = rootdb.params.password =db.params.dbname = floating_cloud

2. create a Base. php file in the controllers Directory for the database controller.

 Db); $ db-> query ('set names utf8'); Zend_Db_Table: setdefaadapter adapter ($ db); $ authAdapter = new Zend_Auth_Adapter_DbTable ($ db ); // used for database Auth verification Zend_Registry: set ('authadapter ', $ authAdapter); // Save the Zend_Auth_Adapter_DbTable instance to the global directory for later use }}

3. use the init () of the parent class to use the database by inheriting the Base class

 

4. create a new model. the following code can be used to operate the database.

  

5. compile a page that can count the number of browsing times

   GetAdapter (); // The adapter that obtains this table provides some methods for us to generate some SQL statements or execute statements. $ db-> query ("update fc_comment set counter = counter + 1 where columnName = 'pagecounter '"); // update the counting field $ where = $ db-> quoteInto ('columnname =? ', 'Pagecounter'); // Generate the where columnName = 'pagecounter 'string, which can automatically add ''; $ orderBy = 'commentid '; $ n = $ table-> fetchAll ($ where, $ orderBy)-> toArray (); // Obtain the query result and convert it to an array print_r ($ n ); // test }}

6. remaining descriptions

// When using zend framework, you must note that some of the small configurations are the easiest way for new users to encounter results. Therefore, zend framework cannot be used here. only zend framework 1.x does not include the latest zend framework2.0. // install zendframework: download the compressed package decompress, enter in cmd command line (my zf is decompressed to c: \) c: \ ZendFramework-1.12.0 \ bin \ zf. bat create project webapp to generate project files, and then copy the generated project webapp directory to the apache www directory, copy the zend directory in the library directory in the compressed package to the library in the project. // you must first enable the pdo module of php. in the ini configuration file, find // search extension = php_pdo_mysql.dll; if there is; extension = php_pdo_mysql.dll remove; then you can // enable the apache rewrite module in httpd. search LoadModule rewrite_module modules/mod_rewrite.so in the conf file. if it is found and commented out, for example, # LoadModule rewrite_module modules/mod_rewrite.so, remove #. // after completing this, you can use zend framework normally..

7. use Layout to dress up our view page

// (The declaration may be annotated here. please be patient and hope to clarify the layout Zend_Layout. In fact, the layout Zend_Layout is very easy to use and is easy to use. there are not many examples. it will be easy to see) // The Layput of the Zend framework is very powerful. you can make whatever Layout you want, and use Layout, first, create a layouts folder under the project directory/application/views/// open the project directory/public/index. the php file initializes our Layout in this entry file and adds the following code under/** Zend_Application */to require_once 'zend/Layout. php '; Zend_Layout: startMvc (array ('layoutpath' => APPLICATION_PATH. '/views/layouts'); // initialize the layout object Directory, that is, the directory I just created, where the layout file is stored // after completing the Zend layout, the IndexController is opened after initialization. php adds the following code to the init method of the master controller to add the layout page // (the purpose of the layout is to take advantage of duplicate pages without generating duplicate code to improve development efficiency) for example, the navigation of a website and the description of Copyright at the bottom of the website can be written at one time, without adding each view page. $ Resp = $ this-> getResponse (); $ resp-> insert ('header', $ this-> view-> render ('default/header. phtml '); // add the header section in the layout $ resp-> insert ('footer', $ this-> view-> render ('default/footer. phtml '); // similarly // we create a new default folder in the view directory to store our navigation header. phtml and bottom fooeter. phtml // create a new layout in the layouts/directory in the view directory. phtml (because the default layout file is layout. you can set your own layout file after phtml) // layout in the layout file. you can write in phtml like this.

Layout ()-> header?>

Layout ()-> content;?>

Layout ()-> header?>

// Here, $ this-> layout ()-> header is what we put in $ resp-> insert ('header ', $ this-> view-> render ('default/header. phtml '); the header added in this line of code. on the phtml page, similarly, footer // is talking about $ this-> layout ()-> content, which displays the index corresponding to our IndexAction. phtml view file // similarly, if other actions such as viewAction are accessed, the corresponding view file is displayed, that is, view. phtml // you can control the page layout as you like, and complete more complex applications, such as applications with widgets similar to pages.

8. write verification code for our login page

// Refer to the previous Base. in the php file, $ authAdapter = new Zend_Auth_Adapter_DbTable ($ db); this line of code, this chapter is about database verification that is commonly used in login verification. there is not much nonsense about code. if ($ this-> _ request-> isPost ()) {$ u = $ this-> _ request-> getParams (); $ fil = new Zend_Filter_StripTags (); $ user = array (); $ user ['username'] = strtolower ($ fil-> filter ($ u ['username']); $ user ['userpass'] = $ fil-> filter ($ u ['userpass']); //************************************ * ********************** $ Auth = Zend_Registry: get ('authadapter '); $ auth-> setTablename ('note _ user')-> setIdentityColumn ('Username')-> setCredentialColumn ('userpass')-> setIdentity ($ user ['username']) -> setCredential (md5 (md5 ($ user ['userpass'])-> authenticate (); //************************************** * ******************** $ signup = $ auth-> getResultRowObject (); if (is_object ($ Signup) {echo $ signup-> userName ;}} // the portion between asterisks is the code used for verification. The previous code is used to obtain the parameters passed in the login form. // Here $ auth is an instance of Zend_Auth_Adapter_DbTable, zend_Auth_Adapter_DbTable provides five setXXXX methods to set the parameter // setTablename () method for verifying the database table to set the name of the table where the verification field is located. for example, we create a new user table, the table contains the userId userName userPassword field. enter 'user' // setIdentityColumn () to set the ID field, for example, our userName field // setCredentialColumn () set the verification information field, for example, our user password userPassword field // setIdentity () Set the value of the identity we get from the form, for example, username table ['username'] // setCredential () set the value of the authentication information we obtain from the form, for example, the password table ['userpassword'] // authenticate () returns the authentication object. this object contains a method getResultRowObject () that returns the authentication result () if the authentication succeeds, an object containing all fields in the table is returned. if the authentication fails, null is returned to complete a simple login page. This should be convenient. of course, this can also be implemented in the model. here zend has implemented a simple database authentication function for us.

9. secure form file Upload validation process

Here, the file upload Zend provides a Zend_File_Transfer_Http class to process the form file, and a Zend_File_Transfer to verify the file information, such as the size and format, almost contains all the file information, you can use it to filter and view the code directly. if (isset ($ _ FILES ['photo']) {$ upload = new Zend_File_Transfer (); $ upload-> addValidator ('isimage', false); // verify the image format if ($ upload-> isValid ()) {// valid Upload $ adapter = new Zend_File_Transfer_Adapter_Http (); $ adapter-> setDestination (ROOT. '/upload/'); // Set the directory where the file is stored if ($ adapter-> receive ()) {// whether print_r ($ adapter-> getFileInfo () ;}} is successfully saved. This allows you to filter and save simple files.

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.