Step-by-step authoring of PHP Framework (ii)

Source: Internet
Author: User

In fact, it is not difficult to implement an MVC application, we know that all the contents of MVC must go through the controller, so how to give control to the controller?

We need a portal file, the entry file is responsible for the delivery of the request to the corresponding controller, in fact, the completion of the routing function.

In order to learn, the function of the route can be simplified, the user passes the name of the controller and the name of the action through the Get way, the entry file receives all the parameters, include a file according to the rules previously agreed, then instantiate the class, and then call the corresponding method of the class.

01 <?php
02 $controller = Empty ($_get[' C '])? ' Index ': Trim ($_get[' C ']); The default controller is set
03 $action = Empty ($_get[' a '])? ' Index ': Trim ($_get[' a ']); Set the default action
04 $controllerBasePath = DirName (__file__). '/modules/controllers/';
05 $controllerFilePath = $controllerBasePath. $controller. ' controller.php ';
06 if (Is_file ($controllerFilePath)) {
07 Include $controllerFilePath;
08 $controllerName = $controller. ' Controller ';
09 if (class_exists ($controllerName)) {
10 $controllerHandler = new $controllerName ();
11 if (Method_exists ($controllerHandler, $action)) {
12 $controllerHandler-> $action ();
13 } else {
14 Echo ' The method does not exists ';
15 }
16 } else {
17 Echo ' The class does not exists ';
18 }
19 } else {
20 Echo ' controller not exists ';
21st }

Look at the number of lines of code is still quite a lot of oh, the actual implementation of the function is relatively simple, that is, by accepting the pass through the get way of the controller name and action name, if not passed, then call the default controller name or action name, This problem may occur after the user passes the controller name and the action name. It is through the rules we have agreed before to find the appropriate file or class or method, then we need to display friendly information, rather than let PHP throw a series of warnings, which is very useful for the robustness of the program.

When there is a controller, everything becomes simpler, and for the model, we can think of it as a normal class, but functionally it is responsible for the processing of the business logic.

1 <?php
2 Class Indexmodel {
3 Public Function test () {
4 Return ' Hello world! ';
5 }
6 }

As a demo, we just return a Hello world in this model, so how does the controller use the model, which is actually first include the file, then instantiate the class, and invoke the appropriate method.

We now modify the controller to:

01 <?php
02 Class Indexcontroller {
03 Public Function index () {
04 $modelPath = DirName (__file__). '/.. /models/indexmodel.php ';
05 if (file_exists ($modelPath)) {
06 Include $modelPath;
07 $model = new Indexmodel ();
08 echo $model->test ();
09 } else {
10 Echo ' model does not exists ';
11 }
12 }
13 }

Now that the model has, how do you connect the view?

1
2
3
4 <body>
5 <p><?php if (isset ($var 1)) {echo $var 1;}?></p>
6 </body>
7

After you finish writing the view, how do you call this view through the controller?

Modify the controller to the following:

01 <?php
02 Class Indexcontroller {
03 Public Function index () {
04 $modelPath = DirName (__file__). '/.. /models/indexmodel.php ';
05 if (file_exists ($modelPath)) {
06 Include $modelPath;
07 $model = new Indexmodel ();
08 $var 1 = $model->test ();
09 $viewPath = DirName (__file__). '/.. /views/index.php ';
10 if (file_exists ($viewPath)) {
11 Include $viewPath;
12 } else {
13 Echo ' view does not exists ';
14 }
15 } else {
16 Echo ' model does not exists ';
17 }
18 }
19 }

Such an MVC application is set up, although relatively simple, the problem is still a lot of!!!



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.