Php. How to use MVC

Source: Internet
Author: User
Tags american time exit getmessage install php php file php and php error valid

1. Download the corresponding Apache version from http://www.apache.org.
2. Download the corresponding PHP version from http://www.php.net.
3. Download the corresponding PHP.MVC version from Http://www.phpmvc.net.
4. Create directory c:\www as Web server directory, C:\webapp as Web root directory,
You can also define your own directory, and make the following modifications accordingly.
5. Install Apache to c:\www, open the browser, enter http://localhost in the address bar to test whether the installation is successful
Note: If you have another Web server installed on your system (such as IIS 5) and you have 80 ports in use, stop the server first.
6. Compress php into a directory, such as: F:\PHP5
7. Copy the php.ini-dist in the PHP directory to the system directory, C:\WINNT, renamed PHP.ini
8. According to the PHP version, here is php5, copy Php5ts.dll and LibXXX.dll files to C:\winnt\system32
9. Open the Apache configuration file httpd.conf,apache default installation under C:\Program Files\apache group\apache2, so the configuration file is in this directory under the Conf
10. Find LoadModule Line, enter: LoadModule php5_module "F:/php5/php5apache2.dll" (Please change according to your position)
11. Find the AddType line, add below: AddType application/x-httpd-php. php
12. Save the configuration file and open the php.ini file
13. Find Doc_root, write your Apache Web root directory in quotes after the equals sign, such as: Doc_root = "C:\www"
14. Find Extension_dir, in the quotation mark after the equal sign, write down the ext directory address in your PHP5 directory, such as: Extension_dir = "F:\php5\ext". Note: PHP4 is the extension directory
15. Find extension, these lines are preceded by commas, is the annotation, you can see your php5 under the Ext directory (PHP4 is the extension directory) have those DLLs, and then the previous comma removed.
16. Save PHP.ini, open Apache, and if there are any errors please look carefully at the steps above. And then write a PHP file that reads:
<?php
Phpinfo ();
?>
Save it as test.php, then put it in the Web root, open the browser, and enter: http://localhost/test.php to see if it can be displayed correctly. installation is successful if correct.


Install PHP.MVC:


1. Unzip the downloaded PHP.MVC to the PHPMVC under the Web root (you can change your name).
2. Open PHPMVC under the main.php open, find
$appServerRootDir = ' C:\webapp\phpmvc '; No trailing slash
The middle of the quotes is the PHPMVC you want to modify.
$moduleRootDir = ' C:\webapp\phpmvc '; No trailing slash
The middle of the quotation marks is modified to be the same as $appserverrootdir, because this is the home directory of the PHPMVC, so the same
$osType = ' WINDOWS ';
Quotation marks in the middle of the change to your operating system name, its value can only be one of the Unix,windows,mac
3. Save this file and confirm that the Phpmvc-config.data has writable permissions under the C:\webapp\phpmvc\WEB-INF directory.
4. Open the browser, enter Http://localhost/phpmvc/Main.php?do=stdLogon in the address bar to test whether the installation was successful. (But I've never been successful)
5. For more detailed installation documentation, see Http://www.phpmvc.net/docs/installIdx.php?doc=all
6. If a non-object in \web-inf\classes\phpmvc\utils\bootutils.php error occurs on the call to a member function, delete the/web-inf/first Phpmvc-config.data the contents of the file, and then manually modify the Phpmvc-config.xml, such as adding a space (just to make PHP. MVC detects that the file has been modified and needs to regenerate the Phpmvc-config.data file.


Official example of installing PHP.MVC: (optional)


1. Note: this instance application requires the class library version to ask PHP. MVC Beta 0.3.4 or higher
2. Download application: PHP. MVC Users Guide Example. (Http://www.phpmvc.net/download/rel/phpmvc-guide-101.zip)
3. Extract to the Web root directory, such as: C:\webapp\SalesReports
4. Edit main.php file and set PHP. MVC class Library path and application path, as follows:
$appServerRootDir = ' C:/webapp/phpmvc '; No trailing slash
Quotes inside to be modified into your PHPMVC extract directory
$moduleRootDir = ' c:/webapp/salesreports '; No trailing slash
Quotation marks inside to be modified to your example of the application of the Extract directory
5. Test the application. Access Address Http://localhost/SalesReports/Main.php?do=salesReport (this I tested successfully)


Develop a project of your own:


1. Create a new directory in the Web root directory, such as: study
2. Copy the main.php and Web-inf directories under the PHPMVC decompression directory to the study directory, we want to remove the files we don't need
3. Enter the Web-inf directory and delete directories and files other than. htaccess,modulepaths.php,phpmvc-config.xml,phpmvc-config_1_1.dtd,prepend.php
4. Back to the top directory, modify the main.php file (in fact, this file is the MVC C, that is, the controller), find $modulerootdir, modify the directory for this project, such as: C:/webapp/study
5. Also want to modify $actiondispatcher, later need to modify, now do not need
6. Under Web-inf, create a new classes directory with Actionform,action and Actiondispatcher classes (these classes can refer to the example above and are not mentioned here). Write the Actiondispatcher class and write it to the main.php in the $actiondispatcher.
7. You can write PHP files in the upper Web-inf directory for display (that is, MVC's V, view), and then configure the Phpmvc-config.xml under Web-inf (for example, or to view the official website tutorials).
8. After writing, enter in the browser: Http://localhost/study/Main.php?do=XXX
where xxx represents some of the action you configured in Phpmvc-config.xml. Look at you. Your project has not been successful.


Use PHPMVC to bring the ADODB connection pool from:


1. Download ADO library, you can find in http://www.phpe.net/class/106.shtml
2. Unzip it to your PHPMVC decompression directory under the Web-inf/lib, so that your ADO library may be under C:\WEBAPP\PHPMVC\WEB-INF\LIB\ADODB
3. Open the globalpaths.php under C:\webapp\phpmvc\WEB-INF, you can see many $appdirs[in function getglobalpaths (), add one row to these $appdirs[]: $ appdirs[] = ' web-inf/lib/adodb ';
To specify your ADO library directory
4. Save this file, open globalprepend.php, add:
Include_once ' web-inf/lib/adodb/adodb.inc.php '; Main file for ADO
Include_once ' web-inf/lib/adodb/drivers/adodb-mysqlt.inc.php '; The ADO driver of MySQL
Include_once ' adodbdatasource.php '; PHPMVC the ADO interface file with self
5. Save this file, open the Phpmvc-config.xml file and add it to the <phpmvc-config> tab:
<data-sources>
<data-source key = "Adodb_mysql_data_source" type = "Adodbdatasource" >
<set-property property = "Host" value = "localhost"/>
<set-property property = "DatabaseType" value = "MySQL"/>
<set-property property = "Database" value = "study"/>
<set-property property = "username" value = "root"/>
<set-property property = "Password" value = "123456"/>
<set-property property = "persistent" value = "False"/>
</data-source>
</data-sources>
Please modify the value in value according to the situation, where databasetype that value need to see what database you are, different database value is not the same, here is MySQL
6. Find the WEB-INF\CLASSES\PHPMVC\DBCP directory of adodbdatasource.php, this is PHPMVC with the ADO interface file, but this file can not work, you need to modify.
First of all, its annotations do not expand, in the File class definition class Adodbdatasource extends adodb_mysqlt above those comments no good baskets, you need to add a/*
And although you set up database information in Phpmvc-config.xml but it seems like it doesn't all get it, you need to add it before the $this->pconnect in function open ():
$this->databasetype = ' MySQL '; Specifies that the database is MySQL
$this->database = ' study '; Specifies that the database name is study, please change it as appropriate
7. Save this file. Now you can use ADO, where you need a database connection, such as Actionform and action.
The method is:
$dsKey = ' Adodb_mysql_data_source '; This is the key you set in the Phpmvc-config.xml.
$db =& $this->actionserver->getdatasource ($dsKey); Get Connected
$sql = "SELECT * from user"; SQL statement
$recordSet =& $db->execute ($sql); Perform
if (! $recordSet)
{
Exit ($db->errormsg ());
}
$rowArray = $recordSet->fetchrow (); Get a record
if (! $rowArray)
{
Exit ($db->errormsg ());
}
echo $rowArray [' username ']; Please change the quotation marks to your field name
echo $rowArray [' Password '];
$recordSet->close (); Close connection
$db->close (); Close connection
8. Let's run your program now.


Internationalization:


1. You can use the. Properties file (a property file) to record all the text you need to display, so that when the view is displayed, you don't need to hard-code the text, so that we can use multiple. properties files to display the languages of each country.
2. In this example, we write a small file: my.properties, Content:
#井号是注释
logon.username.reqd= Please enter a valid username [{0}]
logon.password.reqd= Please enter a valid password [{0}]
Let's not take care of the things in [] that we'll talk about later
3. Save the file to Web-inf/classes, that's where you put it with the action class.
4. You can use them now, write the following code where you want the file:
$config = ' my '; Here you write the name of your property file, no need to write. Properties, the program will automatically find
$defaultLocale = new Locale (); The default time zone
$PMR = new Propertymessageresources (NULL, $config, False);
$PMR->setdefaultlocale ($defaultLocale);
Start output text
echo @ $pmr->getmessage ($defaultLocale, ' logon.username.reqd ');
Quotation marks are the names of the attributes you want to output in the attribute file.
Now it's OK, I used the @ in the front, because this may appear ' notice ' information to destroy the display
If you think the above is too verbose, you can write the statement before the output in a PHP file such as global.php, and then each page contains it
5. The contents of the property file are now clearly exported when the username and password are entered incorrectly. So we also want the output to display the illegal username and password, so we simply add [{0}] after the name of the property to be displayed. We may also want to display multiple messages, then write [{0}] [{1}] ), so on and so forth.
6. Now we need to judge the legality of the username and password in the Actionform and output the error message illegally. So how do we pass [{0}] the wrong username to display? Follow me:
$args = Array ($username); Where $username is the wrong username and, if there are multiple, can be placed in this array
Now it's ready to be exported.
echo @ $pmr->getmessage ($locale, ' logon.username.reqd ', $args);
7. Now to do an English version, copy the My.properties file into the my_en_us.properties, and the text in the file to modify into English, save
8. The global.php document we just made, revise it:
$defaultLocale = new Locale (' en_US '); Change to American Time zone, language is English
9. Now let's take a look at the output, whether it has become English. If you do not know the country and language code, such as: en_US, then PHPMVC in the/docs/locale directory under the directory can find



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.