Using the fleaphp framework to build a simple message application

Source: Internet
Author: User
Tags define array config include php file table name root directory zend framework
Architecture

"Fleaphp Introduction"

Fleaphp is a domestic MVC framework, the current mainstream frame Zend Framework, Symfony, cake, domestic and FCS, Plite and other frameworks are worth looking forward to.

Let's take a look at the official introduction:
Fleaphp helps developers create applications with ease and speed. The fleaphp framework is simple, clear, easy to understand and learn, and has a complete culture of documentation and rich sample programs to reduce learning costs. Applications developed using the Fleaphp framework can automatically adapt to a variety of operating environments and are compatible with PHP4 and PHP5. Fleaphp's full name is the Fast-lightweight-extensible-automatic PHP Web application framework.

Today I simply use fleaphp to build a simple message this procedure to probably understand the following fleaphp operating mechanism. More information on fleaphp visit the official website: www.fleaphp.org (Telecom) www.fleaphp.net (Netcom).
fleaphp Development Guide: Http://www.fleaphp.net/index.php?q=guide

"Build a message book application"

1. Data table structure

The request of the message is relatively simple, is to be able to leave a message, display a message, so simple function, look at the following data table structure:

--
--The structure of the table ' guestbook '
--
CREATE TABLE ' Guestbook ' (
' ID ' int (a) not NULL auto_increment,
' Nicker ' varchar not NULL default ',
' Email ' varchar (MB) default NULL,
' URL ' varchar (+) default NULL,
' Content ' text not NULL,
' Created ' datetime not NULL default ' 0000-00-00 00:00:00 ',
PRIMARY KEY (' id ')
) Type=myisam;

2. Program directory structure


The entire message the structure of this program is this:

/fleaphp/----BASIC Framework Directory
/guestbook----Message This root directory
/guestbook/config----Configuration file directory
/guestbook/model----Model-level file directory
/guestbook/view----Display Layer file directory
/guestbook/controller----Control Layer file directory

3. configuration file

I first build the configuration file to save the basic configuration information of the database, the configuration file path is:/guestbook/config/dsn.config.php


<?php
/**
* DSN
* Data Source configuration file
*/

Return Array (
' DbDSN ' => Array (
' Driver ' => ' MySQL ',
' Host ' => ' localhost ',
' Login ' => ' root ',
' Password ' => ',
' Database ' => ' test '
)
);

?>

4. Program Entry point (Home)

We're going to build the homepage, which is the entry program for all our applications:/guestbook/index.php


<?php
//======================================
Name:gueskbook
Desc:first fleaphp Application
//======================================

Include file
Define ("App_dir", DirName (__file__));
Define ("View_dir", App_dir.) /view/");
Require_once (".. /flea/flea.php ");

Load DSN configuration file
$dsnConfigFile = './config/dsn.config.php ';
Register_app_inf ($dsnConfigFile);
Import (DirName (__file__));

Perform
Run ();

?>

Basically, we're going to configure the App_dir constant, then load the basic fleaphp framework file and the data source configuration information, then add a class search directory, and run the entire program by executing run ().

5. Controller (Controller)

Now look at our main thing, CONTROLLER (Controller):/guestbook/controller/default.php


<?php
/**
* Default Controller
*/

Class Controller_default extends Flea_controller_action
{
/**
* Message Book Model
*/
var $_MODELGB;

/**
* Constructor
*/
function Controller_default () {
$this-&GT;_MODELGB =& Get_singleton ("MODEL_GB");
}

/**
* Default Action
*/
function Actionindex () {
$posts = $this->_modelgb->findall (null, ' created DESC ');
Include ("view/index.php");
}

/**
* Insert a message
*/
function Actioncreate () {
$CREATEARR = Array (
' Nicker ' => htmlspecialchars ($_post[nicker]),
' Email ' => htmlspecialchars ($_post[email]),
' URL ' => htmlspecialchars ($_post[url]),
' Content ' => nl2br (Htmlspecialchars ($_post[content)),
);

$this->_modelgb->create ($CREATEARR);
Redirect ($this->_url ());
}
}

?>

Our controller Controller_default is a default controller that inherits all properties and methods from the Flea_controller_action class for its own control. The Controller_default class contains three methods, the constructor is used to initialize a Model class, Actionindex () is the default action method, it extracts all the messages from the controller MODEL_GB, and then passes through the view/index.php File to display the interface. Actioncreate () method is to create a message of the action, that is, construct a database, key is the field name, value is the form of the field value of the array, submitted to the MODEL_GB model for processing, inserted into the database.

6. Model Layer

Let's take a look at what code the model layer implements:/guestbook/model/gb.php


<?php
//======================
Guestbook Model
//======================

Load_class ("Flea_db_tabledatagateway");

Class MODEL_GB extends Flea_db_tabledatagateway
{
/**
* Data table name
*/
var $tableName = ' Guestbook ';

/**
* Data Table PRIMARY key
*/
var $primaryKey = ' id ';
}

?>

We see that the model code is very simple, is a MODEL_GB class that inherits the Flea_db_tabledatagateway class, and there is no method code, only two attributes, a $tableName record the name of the message table, $primaryKey The primary key field in the record table.

7. Display Layer (View)

Finally, look at the implementation HTML of the display layer (View) that our users can look at:/guestbook/view/index.php

<! DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en"
<title> message Book </title>
<meta name= "keywords" content= ""
<meta name= "description" "
< Link rel= "stylesheet" href= "View/resource/css/gb2.css"
<style>
*{
    margin:0;
    padding:0;
}
body{
    width:760px;
    height:100%;
    margin:10px Auto 10px;
    font-size:12px;
}
#main {
    width:758px;   
    border:1px solid;
    background-color: #eee; 
}
#posts {
    height:200px;
    Text-align:center;
    font-size:12px;
}
#posts input, #posts textarea{
    border:1px solid;
}

#posts h2{
    padding:10px
    Text-align:center;
    font-size:14px;
}
#content-list{
    text-align:center;
}
#content-list h2{
    margin-left:70px;
    padding:10px;
    Text-align:left;
    font-size:14px;
}
. tbl-style{
    display:block;
    width:600px;
    Background-color: #ccc;
    margin-bottom:10px;
    padding:0 5px 0 5px;
    border:1px Solid green;
}
. td1-style{
    width:38px;
    Text-align:left;
    line-height:20px;

}
. td2-style{
    width:100px;
    Text-align:left;
    line-height:20px;
}
. td3-style{
    width:450px;
    Text-align:left;
    padding:10px;
}
</style>

<script language= "JavaScript" >
function Checkform () {
var o = document.getElementById ("guestbook");
if (O.nicker.value = = "") {
Alert (' must enter nickname Oh ... ');
O.nicker.focus ();
return false;
}
if (O.content.value = = ' && o.content.value.length<10) {
Alert (' En, the message content must lose, I think at least can not be less than 10 words, or I call message pinch ... ');
O.content.focus ();
return false;
}
return true;
}
</script>

<body>
<div id= "Main" >
<div id= "Posts" >
<form action= ". echo $this->_url (' create ');?> "method=" POST "name=" Guestbook "id=" Guestbook ">
Nickname: <input type= "text" size= "name=" Nicker "id=" Nicker "/>"
E-mail: <input type= "text" size= "" name= "email" id= "email"/>
Website: <input type= "text" size= "name=" url "id=" url "/><br/><br
<textarea name= "Content" id= "content" rows= "ten" cols= "" ></textarea><br/><br
<input type= "Submit" value= "Leave a message"/>
</form>
</div>

<div id= "Content-list" >
? foreach ($posts as $GB) {?>
<table class= "Tbl-style" >
<tr>
&LT;TD class= "Td1-style" > Nickname: </td><td class= "Td2-style" ><?= $GB [nicker]?></td>
&LT;TD class= "Td1-style" > Mailbox: </td><td class= "Td2-style" ><?= $GB [email]?></td>
&LT;TD class= "Td1-style" > Website: </td><td class= "Td2-style" ><?= $GB [url]?></td>
</tr>
<tr>
&LT;TD class= "Td1-style" > Message: </td><td class= "Td3-style" colspan= "5" ><?= $GB [content]?></td >
</tr>
</table>
? }?>

</div>
</div>
</body>

8. Realize the effect chart

The overall structure comes out, and we look at the following picture of the running result:

Picture address:/xrssfile/2007-1/25/200712510335349.jpg
(Do not know why CSDN can not pass the picture, so put it into my Baidu space)

Third, the end

For more applications please refer to Fleaphp's official website and download the sample program in the source code, try the following yourself, perhaps, this framework is suitable for you.



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.