SpeedPHP framework learning-. Basic and MVC understanding

Source: Internet
Author: User

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}>

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.