Home  >  Article  >  Backend Development  >  Detailed explanation of file entry examples of MVC framework in PHP_PHP tutorial

Detailed explanation of file entry examples of MVC framework in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:59:53864browse

This article introduces new knowledge about PHP. Friends who need to know more about the file entry usage of the MVC framework in PHP can refer to this article.

How to write the file entry of MVC is completely based on the programmer's engine design and the programmer's preferences, but our ultimate goal is to introduce the engine to handle other transactions through simple code, like Just like when we want to drive a car, we first need to ignite it before the engine can start. Before writing the entry, we need to consider several aspects, such as the URL parsing method, which user parameters or systems need to be brought in, and where the user parameters need to be changed, we need to use a file to introduce them. The file can be xml or PHP. , or other methods that come to mind, but Keheng here uses the array form in PHP to introduce it. This form looks like a write cache. It reads the data from the database and then generates a PHP file, and then requires this file. . Of course, we can also write the entry in the process of writing the engine, as long as it can achieve our purpose.

As far as the file entries we usually see are concerned, file entries are generally divided into single file entries and multi-file entries. Of course, there may be other file entries that we have not seen before.

Single file entry, of course, means that the file index.php is always accessed when accessing the website, but the content displayed inside is loaded according to the background parameters,

For example: http://www.XXXX.com/index.php?Conttoller=index&action=show&id=1

The Controller here is the page we need to display. By getting the value of the Controller to determine which model our MVC is loading and which view is displayed, it is generally necessary to establish a dedicated routing class to determine the address. Action refers to the operation of this model, such as displaying data, adding data, or displaying articles. As for the role of ID, it goes without saying here.

Multi-file entry, of course, means that in addition to the index.php file name, there are other file accesses in the website, such as index.php, about.php, etc. under the same website.

But there is another URL method http://localhost/control/ index / action/1. This method does not specify which file in this directory to access. The default is of course index.php or index. html, I personally think that this method is more troublesome in terms of program production and maintenance, so it is rare to see URLs in this form now. I have read articles about SEO before, and it seems that the entrance is this type of SEO optimization. Not very good (keheng's humble opinion). In fact, we can usually observe whether it is not very good. For example, if we enter a keyword casually in Baidu, there will be almost no similar addresses found in the first few pages. In SEO optimization, it is advisable to keep the hierarchical structure of the address URL within three levels. Of course we must consider these issues before we start a WEB project.

The following is the file entry of a template downloaded from the Internet:

The code is as follows Copy code
 代码如下 复制代码

define('UPLOAD_PATH', './Uploads/');
 
 define('VERSION', 'v2.1 Released');
 
 define('UPDATETIME', '20120323');
 
 define('APP_NAME', 'myphp');
 
 define('APP_PATH', './myphp/');
 
 define('APP_LANG', true);
 
 define('APP_DEBUG',false);
 
 define('THINK_PATH','./Core/');
 
 require(THINK_PATH.'/Core.php');

define('UPLOAD_PATH', './Uploads/');

define('VERSION', 'v2.1 Released');

define('UPDATETIME', '20120323');

define('APP_NAME', 'myphp');

define('APP_PATH', './myphp/');

define('APP_LANG', true);

define('APP_DEBUG',false);

define('THINK_PATH','./Core/');

require(THINK_PATH.'/Core.php');

The core of it is THINK. The entrance specifies the program version, update date, etc., but the real processing file is in the Core.php file. Since PHP templates are all open source, I personally think that in order to prevent others from fully understanding their products, some Internet companies deliberately make multi-level calls in the PHP code to confuse programmers who want to learn his code ideas and let programmers follow. I’m confused as to what I’m going to follow. In fact, I’ve lived like this, and it may be due to my lack of experience.
 代码如下 复制代码


require_once './include/common.inc.php';

$views->display('index.html');        
 
 ?>

The code is as follows Copy code

            require_once './include/common.inc.php';

             $views->display('index.html');                                                       
?>

After loading the engine file, tell the $views class which view file to display. This is indeed much more intuitive, but I personally don’t like this method. Although if you modify the file template and modify it directly in the corresponding file, it feels like It's not easy to control, and $views is not closed at the end, occupying memory.

After reading some other people’s entrances, Keheng also had ideas for his own entrance. Whether it is a single file or multiple files, all use this entry. In short, the contents of all the files in the root directory of the website are this

Actually, my object model is set in config.php
The code is as follows
 代码如下 复制代码


require 'command/config.php';

require 'command/app.php';

app::run($config);

?>

Copy code

 代码如下 复制代码


$config['Templates'] = array( //名称记得全转为小写

‘keheng’ =>                                 array('keheng.php',0),
 
          ' index '                                 =>                                 array('index.php',0),
 
 );
 
 ?>


              require 'command/config.php';

              require 'command/app.php';

            app::run($config);

?>

The code is as follows
Copy code


$config['Templates'] = array(                                                                                                                    
'keheng'
'index '
);

?> The analysis address calls the corresponding view, so that I can use a file to set up the corresponding model and view. When using it, I can use require 'command/config.php'; to load this array. Some experts may think that this method seems not very good. It may be that Keheng has too little experience or insufficient skills. He has not thought of a better method yet
http://www.bkjia.com/PHPjc/631285.html
www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631285.htmlTechArticleThis article introduces the new knowledge of php. Regarding the usage of file entry of the MVC framework in PHP, friends who need to know You can refer to this article. How to write the file entry of MVC is completely based on the process...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn