Step-by-step writing of PHP's Framework (11)

Source: Internet
Author: User
Tags foreach array include interface key requires root directory

Before told how to make the implementation of jump and request forwarding, of course, but also just very simple to say, deeper content requires you to read the specific framework of the implementation.

Now jump and forwarding have, for the model can be written back, then I will say how to tell the data from the controller passed to the view, before we implement the way is very ugly:

1 $viewPath = DirName (__file__). '/.. /views/index.php ';
2 if (file_exists ($viewPath)) {
3 Include $viewPath;
4 } else {
5 Echo ' view does not exists ';
6 }

Now that I'm ready to pass the data to the view in a more elegant way, let's talk about how to call:

01 <?php
02 Class TestController extends Controller {
03 Public Function test () {
04 $this->_assign Array (
05 ' Arr ' => array (
06 ' Test ', ' test2 '
07 ),
08 ' Str ' => ' It is a str '
09 ));
10 $this->_display (' Test ');
11 }
12 }

This is the way I write after today, if you have studied smarty, then you will find this and smarty very similar, indeed, I write Toper is also affected by the smarty, the first idea is the view of this piece on the use of smarty, after thinking about it, Since the claim is its own framework, then the view of this together must also write all of their own, although so, but I still retain the assign,display this way, and as long as the configuration file changes, the view can be completely switched to Smarty without using the framework itself implementation.

Seems to pull a bit far, first of all, for the simple code, I will assign and display of the implementation of simplification, we understand the idea is good, the real framework of the implementation of a little more complex.

First assign, its function is to assign values to variables, here I assume that the passed parameters are associative arrays, which is often said Hashtable, it should not directly call the view interface, this interface implementation how to assign value:

1 protected function _assign (Array $arr) {
2 View::assign ($arr);
3 }

In order to deal with the data storage and display of this piece of view, I defined view.php this file, View::assign this method is its static method, of course, realize the function everybody understands ...

How to achieve in the concrete view.php?

1 private static $_data = Array ();
2 public static function assign ($arr) {
3 foreach ($arr as $key => $val) {
4 if (!is_int ($key)) {
5 Filter out such data as array (' Test ', ' test2 ')
6 self::$_data[$key] = $val;
7 }
8 }
9 }

Since assign is a static method and data needs to be stored in this class, you need to define a static member variable $_data, which stores the data that the controller passes. Since the type hint was previously in the controller's assign, it is guaranteed that the assign passed in view is an array, and now only one foreach is needed and then stored in sequence, so why not use Self::$_data = $arr directly? So there's only one sentence?

First, some of the data in the array may be wrong, for example, array (' Test ', ' test2 ') I don't understand what data test and test2 really represent, such data should be eliminated in the assign, if you ask more stringent, you can also directly to the user warning.

Second, it is possible for users to call assign more than once, if the direct use of the reference, then the second call assign will be the first data lost, this is intolerable.

Ok, assignment is done, and then how to display the problem, in the controller, or directly call the interface, but not responsible for the internal implementation, but before calling the interface requires a certain format:

01 protected function _display ($STR) {
02 if (is_string ($STR)) {
03 $str = Str_replace (Array (
04 '.','#'
05 ), Array (
06 '/','.'
07 ), $STR);
08 View::d isplay (Modules_path). View::view_base_path. $str. '. php ');
09 }
10 }

For the sake of visual beauty and other reasons for XXX, we can change the representation of the path/when passed as an argument, so that if you want to invoke test/test2.php, you just need to pass test.test2.php. Of course, this also has a problem, sometimes need to use. Like just this example, actually test.test2.php will be parsed into test/test2/php, so there is actually a problem, so how to solve it, I use a # representative. So just this passing time becomes the test.test2#php. There is also a problem,. php basically every page has, then why to pass into it, the direct framework to help you add it, so the user only need to input test.test2, so logical also easy to understand, test module, the Test2 this view file. If you have used thinkphp, you will find that it is similar to its writing, in fact, I was at the beginning of the time is to look at the thinkphp source side of the writing, so many things are borrowed from its ideas. I personally hate to use #, so I'm basically not used in the view file #, because I think you're not so boring, To write a meaningless file name similar to test.view.php, direct test.php is good, such a name framework to help you solve a part, so basically does not exist this problem.

There is also a constant View::view_base_path, which represents the path to the view root directory, so the actual absolute path is actually passed in.

Since display is the display of this view file, it will definitely use include a file, specifically implemented as follows:

1 public static function display ($file) {
2 if (file_exists ($file)) {
3 Extract (Self::$_data);
4 Include $file;
5 } else {
6 throw new Viewexception (viewexception::not_exists_template);
7 }
8 }

Extract is to tell the array to scatter, more please query the PHP manual.

Here because this view file may not exist, so you need to determine, if the view file does not exist, then throw the exception directly, note that the exception is the use of viewexception, this class is a new definition, passed parameters to indicate that the exception is due to the view template does not exist.

Then let me see the implementation of this exception class:

01 <?php
02 Class Viewexception extends Baseexception {
03 Const NOT_EXISTS_TEMPLATE = 1;
04 Public function __construct ($code = 0) {
05 Switch ($code) {
06 Case Viewexception::not_exists_template:
07 $msg = ' The template file is not exists ';
08 Break
09 Default:
10 $msg = ' Unknown exception ';
11 Break
12 }
13 Parent::__construct ($msg, $code);
14 }
15 }

This class it inherits the Baseexception, because Baseexception realizes the debug mode to open with the different situation display content different, viewexception also has this characteristic, when debug closes, throws the exception, also only then jumps to the error page, No exceptions are shown directly, and such processing is easier to maintain.



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.