Controller is the Controller file. The program starts from index. php.
[Php]
<? Php
Define ("SP_PATH", dirname (_ FILE _). "/SpeedPHP ");
Define ("APP_PATH", dirname (_ FILE __));
$ SpConfig = array (
"Db" => array (
'Host' => 'localhost ',
'Login' => 'root ',
'Password' => 'root ',
'Database' => 'test ',
),
'View' => array (
'Enabled' => TRUE,
'Config' => array (
'Template _ dir' => APP_PATH. '/tpl ',
'Compile _ dir' => APP_PATH. '/tmp ',
'Cache _ dir' => APP_PATH. '/tmp ',
'Left _ delimiter '=>' <{',
'Right _ delimiter '=>'}> ',
),
)
);
Require (SP_PATH. "/SpeedPHP. php ");
SpRun ();
The above program defines the path of the App and SP, loads the configuration of the database and view layer, loads the core library file of the SP, and finally runs the entire system. When the above program runs, the index method under the main class is first executed under the Controller directory. The main class program is as follows:
[Php]
<? Php
Class main extends spController
{
Function index (){
$ Tpl = $ this-> spArgs ("tpl", "green ");
$ Guestbook = spClass ("guestbook ");
$ This-> results = $ guestbook-> findAll ();
$ This-> display ("{$ tpl}/index.html ");
}
Function write (){
$ Guestbook = spClass ("guestbook ");
$ Newrow = array (
'Name' => $ this-> spArgs ('name '),
'Title' => $ this-> spArgs ('title '),
'Contents' => $ this-> spArgs ('contents '),
);
$ Guestbook-> create ($ newrow );
Echo "<ahref =/index. php? C = main & a = index> return </a> ";
}
}
From index. php, the index function of the main method is called by default. In this example, this function first sets the Template Name parameter (tpl ). Create another model. Use the findall method of the model to find all database information. Finally, use the tpl template to display the results. The Controller program must inherit from the spController class. The method name is the called action name. When explicitly calling a program, the path is index. php? C = main & a = write. The Model is a class file with the same name as the database table. This model must inherit from the spModel and have the primary key and table name attributes set.
[Php]
<? 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
}
Use the following program in the index.html file in the tpltemplate.txt file to format the output result.
[Html]
<{Foreach from = $ results item = one}>
<H4> <{$ one. title}> <P> <{$ one. name }>:< br/> <{$ one. contents}> </p>
<{/Foreach}>