Phrame MVC Framework learning notes (1)

Source: Internet
Author: User

To maintain a website written using phrame, follow the MVC design pattern.

Phrame is a web development framework implemented using PHP. It provides the basic MVC Architecture and some useful basic classes, such as hashmap, stack, and arraylist. Phrame can be downloaded from the following address:

Http://sourceforge.net/projects/phrame/

Phrame documents are hard to find. Maybe there is a problem with the keywords used during self-search. It is very difficult to find the phrame user's guide. When I read the source code of phrame, I found that its comments are very similar to the javadoc style. So I searched for it and found phpdoc. The documents generated from the source code of phrame can be downloaded from the following address:

Http://download.csdn.net/source/1776647

Phrame users guide:

Http://phrame.sourceforge.net/docs/guide/guide.php

I have posted the phrame users guide to my blog. Address:

Http://blog.csdn.net/jcwKyl/archive/2009/10/29/4743332.aspx

The following is a phrame tutorial found on Google,
Phrame tutorial with smarty:

Http://wp.uberdose.com/2004/01/25/phrame-tutorial-with-smarty/

Phrame Tutorial: Building the hello Application

Http://phrame.sourceforge.net/docs/tutorials/hello.php

You can also download its examples on the phrame SourceForge website.

Since I have little experience in website creation, I have a poor understanding of MVC. This document mainly uses a simple example to discuss the use of phrame. The example is from the article "Building the hello application" mentioned above. The main function is to display the name entered by the user in an editing box, and then display the welcome information after the user submits it. Because PhP5 does not have XSLT functions, the view section in the original text uses common PHP code for implementation.

In the original example, a total of six steps are completed:
Step 1: directory structure for hello Application
Step 2: Building the models
Step 3: Building the actions
Step 4: Building the views
Step 5: Building the form objects
Step 6: Piecing together the hello Application
Here we use the original example.
Directory structure:
[WHB @ jcwkyl HTML] $ tree hello
Hello
| -- Actions
| -- Forms
| -- Include
| -- Models
'-- Views

Step 2: Build models
The main implementation is the person class. The person class has a $ _ name attribute and uses the getname () and setname () attributes of the person class to operate on _ name.
File: models/person. php

Step 3: Build actions
The responsibility of action is to determine "what to do", the responsibility of models is to determine "how it is done", and action is part of the controller. Here we mainly implement the helloaction class, which inherits from the action. In its perform () method, It extracts the name entered by the user from the passed $ actionform parameter, construct a person object and use user input to set the $ _ name attribute of the object. Then, place the object in the session and retrieve the redirection URI from the $ actionmapping parameter, return it.
File: actions/helloaction. php

Step 4: Build views
One is the index page displayed by default, and the other is the hello page that displays the welcome information after the user submits the page.
File: views/index. php views/Hello. php

Step 5: build forms
In this step, implement your own form object. The user's from object inherits from the phrame actionform class, And the phrame actionform inherits from hashmap, the main task of implementing the form object by the user is to reload the validate () function to check the user input.
File: forms/helloform. php

Step 6: Build a controller
As explained in the phrame users guide, the main task of building a controller is to ensure that each user request URI has an action subclass that processes the URI. In this example, the only URI is that the user submits the form after entering the name. The value of the hidden element action in the form is sayhello, and we have already written helloaction for it. Another task is to build a ing table, form-action -- action forward ing table.
File: Include/mappings. php Include/errorhandler. php Include/options. php phrame. php index. php
Phrame. php is the bootstrap file. Here, the actioncontroller object is built and the request is processed.
Index. php is the view handler, which is mainly targeted to views/Default. php.
Options. php specifies some options of the controller and serves as the final parameter of the actioncontroller constructor.
Mappings. php builds a ing table, which is the final parameter of the Controller's process method.

The source code and file directory structure are attached at the end.

There are two points worth noting:
I. When debugging such a scenario, click OK to convert it to views/hello. PHP pages always go to error pages. One-Step debugging finds that $ _ session [_ errors] is true in the perform implementation of the helloaction class, so I want to check the value of $ _ session, but this helloaction. PHP is purely processing background logic (in fact, the stack when it is called is like this: actioncontroller-> process ---> actioncontroller-> _ processaction ---> helloaction-> perform, this can be seen by using the xdebug_print_function_stack () function, or by using the vim plug-in + xdebug single-step debugging). Therefore, all the output of var_dump used here will not be seen in the browser. Therefore, you need to record the error information to the log, which can be done through error_log. As mentioned in PHP manul, the method for storing var_dump errors in strings can also be easily searched online. The Code is as follows:
Function vardumptostring ($ var) <br/>{< br/> ob_start (); <br/> var_dump ($ var); <br/> $ result = ob_get_clean (); <br/> return $ result; <br/>}< br/> // <br/> // example usage: <br/> // $ DATA = array ('first', 'second', 'third'); <br/> // $ result = vardumptostring ($ data ); <br/> //
2. In mappings. php, _ action_forward is written as follows:
_ Action_forwards => array (<br/> 'hello' => array (<br/> _ Path => 'Views/Hello. php? ', <Br/> _ redirect => 0 <br/>), <br/> 'index' => array (<br/> _ Path => 'error. PHP? ', <Br/> _ redirect => 0 <br/>)
Note that the _ path is views/Hello. php? Instead of views/Hello. php, you can see from the definition of _ processforward in actioncontroller. php:
Function _ processforward (& $ actionforward) <br/>{< br/> $ redirect = $ actionforward-> getredirect (); <br/> $ Path = $ actionforward-> getpath (); <br/> If (! $ Redirect) {<br/> header ("Location: $ Path &". sid); <br/>}else {<br/> $ _ session = array (); <br/> session_destroy (); <br/> header ("location: $ path "); <br/>}< br/> $ this-> destroy (); <br/>}
If $ redirect is 0, the header ("Location: $ Path &") will be executed &". sid), there is a file. PHP, if $ path is ". PHP ", then. PHP? & Can be accessed correctly. A. php & Error 404 will occur. In addition, we can see that the value of $ redirect determines whether $ _ Session will be cleared, the difference between the words "forward on" and "Redirect" in phrame users guide.

Another point: One problem encountered today is to try to put an object in the session, and then retrieve this object from the session in another file. The result is always wrong, var_dump ($ _ Session) looked at it and said it was _ php_incomplete_class. This problem can be found many discussions on the Internet. My solution is to include the class definition file.

File directory structure:
 
[WHB @ jcwkyl HTML] $ tree hello

Hello

| -- Actions

| '-- Helloaction. php

| -- Error. php

| -- Forms

| '-- Helloform. php

| -- Include

| -- Errorhandler. php

| -- Include. php

| -- Mappings. php

| '-- Options. php

| -- Index. php

| -- Models

| '-- Person. php

| -- Phrame. php

'-- Views

| -- Default. php

'-- Hello. php

 
5 directories, 12 files

 

You can find the source code at http://download.csdn.net/source/1785206.

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.