In this chapter, let's use the SP framework to create a simple message Program (why is this the most simple one? It can also reflect the speedphp function. I personally think that simplicity is beautiful, can also be understood more quickly ~),
It is very effective to use instances to learn program development. At the same time, we can also understand the development process of the actual network project.
This example will be provided for you to download later ~
First, let's take a look at the requirements of the message book, that is, what features our program will implement:
1. Visitors can view the homepage of a message (that is, the message list. Use the pagination feature provided by speedphp ).
2. Visitors can view the details of a single message.
3. Visitors can leave a message.
4. You can edit or delete the message content (no permission issues are involved at the moment)
5. You can reply to the message.
6. Generate HTML static files
Direct texture
(We are mainly studying the message board, but I have not made any beautification. You will take a look at it ~~, What is important is that you can learn)
This figure see Anhui PHP community: http://bbs.hfphp.org/thread-36-1-1.html copy good trouble, ah ..
As you can see, we need three fields: the name of a message sender, the message title, and the message content,
At the same time,
In summary, we mainly need five fields. The database design is as follows ~
Code
Create Table 'guestbook '(
'Id' int (11) not null auto_increment,
'Title' varchar (50 ),
'Contents' varchar (200 ),
'Name' varchar (20 ),
'Fid' int (6 ),
Primary Key ('id '),
) Engine = MyISAM default charset = utf8;
Here, FID is the parent ID, which is used to distinguish between reply and message. If the FID of the record is 0, it is a normal message. Otherwise, this record is a reply, FID records the Message ID of the reply.
The database has been created,
As for the configuration file on the homepage, I have already discussed it in the previous chapter. I will not repeat it here. You are familiar with the configuration file. Do not be lazy ~
Http://bbs.hfphp.org/
We recommend that you separate the configuration file from the homepage file in the actual application process. For example, we usually create a Config under the root directory. the PHP file code is as follows:
Code
<? PHP
Return array (
// Database
'Db' => array (
'Host' => 'localhost ',
'Login' => 'root ',
'Password' => 'jencon ',
'Database' => 'SP _ company ',
'Prefix' => 'qy _'
),
// Enable smarty
'View' => array (
'Enabled' => true,
'Config' => array (
'Template _ dir' => app_path. '/template ',
'Complie _ dir' => app_path. '/tmp ',
'Cache _ dir' => app_path. '/tmp ',
'Left _ delimiter '=>' <{',
'Right _ delimiter '=>'}> ',
),
),
// Enable static
'Html' => array (
'Enabled' => true,
),
);
?>
In the index. php file on the homepage, we can load the configuration file. The Code is as follows:
<? PHP
Header ("Content-Type: text/html; charset = UTF-8 ");
Define ("sp_path", dirname (_ file _). "/speedphp ");
Define ("app_path", dirname (_ file __));
@ Date_default_timezone_set ('prc ');
$ Spconfig = require (app_path. "/config. php ");
Require sp_path. '/speedphp. php ';
?>
However, here, we still use the configuration file described in the previous chapter, just to tell everyone to learn how to use it flexibly.
According to our design requirements, we need a controller. Here we use the default controller main, so we need to create the main. php file in the Controller. At the same time, we need several basic actions (actions)
<? PHP
Class main extends spcontroller
{
Function Index () {// The homepage
}
Function show () {// view the message content
}
Function write () {// here is the message
}
}
After the Controller framework has been set up, we will start to compile the model layer below ):
We also need to create a guestbook. php and put it in the model Directory, which is what we need to do to link the database. The guestbook. PHP code is simple:
<? PHP
Class guestbook extends spmodel
{
VaR $ PK = "ID"; // the unique identifier of each message, which can be called a primary key.
VaR $ table = "Guestbook"; // data table name
}
Please note that all PHP files must be UTF-8 encoded and the database must also be UTF-8 encoded. Otherwise, garbled characters may occur.
Note: All models must inherit from the spmodel, which is the main core class of the speedphp framework.
Http://bbs.hfphp.org/
At the same time, it should be noted that a framework will have its own development specifications. If you need to use this framework, you must abide by the specifications of this framework. Of course, there are many frameworks currently, there are also many similarities. This is because framework developers are often used to abide by things agreed to be vulgar. Such advantages are naturally unknown, developers who have experience using related frameworks can use the frameworks very quickly. Therefore, after learning speedphp, you can quickly learn other frameworks.
As you know, since it is a framework, he will have his own specifications. The speedphp framework should have the same class name as the file name in the model. Otherwise, an error will occur, and speedphp will crash: A route error occurs, check whether the function exists.
For example, for Class guestbook, the file name is guestbook. php, and the table name in the database should also have the same name. In the future, it will be very convenient ~, It is also in line with development specifications and is conducive to good habits.
In the following tutorial, we will begin to improve the functions. If you have any questions, we will post them in a timely manner .....
Original: Anhui PHP community: http://bbs.hfphp.org/thread-36-1-1.html
Reprinted please indicate the source, thank you