Example of MVC framework file entry in PHP

Source: Internet
Author: User
How to write the MVC file entry is completely based on the programmer's engine design and the programmer's preferences. However, our ultimate goal is to use simple code to introduce the engine to handle other things.

How to write the MVC file entry is completely based on the programmer's engine design and the programmer's preferences. However, our ultimate goal is to use simple code to introduce the engine to process other transactions, just as we need to drive, we need to ignition first before the engine can start.

Before writing an entry, we need to consider several aspects: URL resolution, which user parameters or systems need to be brought in, and where user parameters need to be changed, we need to use a unified file for introduction, files can be xml, PHP, or other methods, but keheng uses arrays in php to introduce them. this format seems to be the form of cache writing, the data read from the database is regenerated into a php file, and then the file can be require. of course, you can also write the entry in the write engine process as long as it can achieve our goal.

As we usually see in the file Portal, the file Portal is generally divided into single file Portal and multi-file portal, of course, there may be other file portals not seen.

Single file entry of course is that the website access is always there to access the index. php file, but the content is related to the display is loaded according to the background parameters, such as: http://www.XXXX.com/index.php? Conttoller = index & action = show & id = 1

Here, Conttoller is the page we need to display. by obtaining the Conttoller value, we can determine which model is loaded by MVC and which view is displayed, generally, you need to create a dedicated routing class to determine the address. action is the operation of this model, such as displaying data, adding data, or displaying the article class, I don't need to mention the role of ID.

Multi-file portal means that the website has access to other files except the index. php file name. for example, index. php, about. php ........... .

However, there is another URL method, http: // localhost/control/index/action/1, which does not specify which file is stored in this directory. generally, index.php#index.html is used. I personally think this method is troublesome for program creation or maintenance, so now we rarely see this form of URL. I have read SEO articles before, and it seems that the entrance is of this type and SEO optimization is not very good (keheng's personal opinion ).

In fact, it is not very good. we can usually observe it. for example, if we enter a keyword under Baidu, we cannot find similar addresses on the first few pages, in SEO optimization, it is recommended that the hierarchical structure of the url be kept within the three layers. we must consider these issues before doing a WEB project.

The following is the file Portal for a template downloaded from the internet. the code is as follows:

  1. Define ('upload _ path', './Uploads /');
  2. Define ('version', 'v2. 1 released ');
  3. Define ('updatetime', '123 ');
  4. Define ('app _ name', 'myphp ');
  5. Define ('app _ path', './myphp /');
  6. Define ('app _ LANG ', true );
  7. Define ('app _ debug', false );
  8. Define ('think _ path', './Core /');
  9. Require (THINK_PATH. '/Core. php ');

Its Core is THINK. the entry specifies the program version, update date, and so on. The actual processing file is in the Core. in the PHP file, because PHP templates are open-source, I personally think that some network companies may intentionally call the PHP code at multiple levels to prevent them from completely figuring out their products, to confuse the programmers who want to learn their code ideas, let the programmers follow suit, in fact, this is the case, it may be caused by lack of experience.

The code is as follows:

  1. Require_once './include/common. inc. php ';
  2. $ Views-> display('index.html ');
  3. ?>

After the engine file is loaded, it is really intuitive to tell the $ views class which view file to display, but I personally don't like this method, although it is difficult to modify the file template directly in the corresponding file, and $ views does not close at last, it occupies the memory.

After reading some other people's portals, keheng also has the idea of the portal. whether it is a single file or multiple files, all use this portal. In short, all the files under the root directory of the website are in this way, the code is as follows:

  1. Require 'command/config. php ';
  2. Require 'command/app. php ';
  3. App: run ($ config );
  4. ?>

In fact, my image model is set in config. php. the code is as follows:

  1. $ Config ['templates'] = array (// The name must be converted to lowercase.
  2. 'Keheng' => array ('keheng. php', 0 ),
  3. 'Index' => array ('index. php', 0 ),
  4. );?>

The analysis address calls the corresponding view, so that I can use a file to set the model and view. when using the address, I can use require 'command/config. php '; load this array.

Some people may think that this method does not seem very good. it may be because keheng has little experience or is not enough technology. now we have not thought of a better method.

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.