DIY PHP MVC Framework (i)--url
?
in today's framework, MVC is no longer a myth. Often hear many programmers discuss which frame is good, which frame is bad, in fact? The framework is just a tool, no good and bad, only suitable and not suitable for their own is the best.
Every time I interview a fresh student, I ask him what frame he has used and what he understands about these frameworks. When interviewing an experienced programmer, let him write a framework for himself. In fact, it is not let him code, as long as the idea is OK. I think if a programmer with a year of experience could not develop a framework v0.0.1, there would be no deep understanding of a frame.
A few days ago @phoenixg said to write an MVC framework. And he's not just talking about it, just one weekend, and the prototype of the framework is magically appearing on GitHub.
The name of this blog post is "DIY design ?" PHP MVC Framework, so this article does not involve too much coding, the text appears in any code fragment is I directly in the vim inside knocks, did not do any test, if want to use the code in the text to self-test.
Following this tutorial, you will design an MVC framework that belongs to you from scratch.
I have used zendframwork, CodeIgniter, each framework has its own advantages and disadvantages. Before writing this article, I looked at Symfony, cakephp, moophp, doitphp and other core source code, the following said I will put my framework design, this chapter mainly discusses the design of the URL.
1. REST
In this time of rest, if a framework does not support rest, it must be looked down upon by avant-garde programmers , so this framework also supports rest.
First design guidelines:? Everything is a resource, and there are many forms of resources .
Regardless of whether it exists or is abstract, all resources will have a constant identity (ID), and no API action on the resource should change the identity of the resource.
In fact, the above is entirely based on the characteristics of the Internet.
- In the Internet, a URL is a resource;
- The content of the resource is the HTML page;
- No matter how you change the HTML content, the URL will not change;
- Links between resources through HTML connections;
- Each time it gets, it gets the full HTML content.
Like what
PHP code??
- Get?http://justjavac.com/users?????????????//? All users ??
- Get?http://justjavac.com/users/phper???//? Identify the user as Phper ??
2. Extension
Here I don't discuss the relationship between extension and file type, as well as "extension is just convention, and file type is recorded in file header".
I usually interpret extensions as "Conventions" rather than file types. When we request a news.html, we are not sure that it is a news.html file that exists on the server, it may be a PHP file, or it may be a JSP file, in the Nodejs popular today, it may also be a JS file. But no matter how the page is generated, one thing is clear-finally we get an HTML document.
Although rest does not require the use of extensions, I was told that if you add a. rmvb extension to a female name, it will become very ... The framework will therefore support extensions, but the extension is part of the resource.
What do you mean?
As in the previous example, how can all users be represented by this resource? With URL? http://justjavac.com/users
? Can be uniquely identified, and? extensions can be used to identify different representations of resources .
A, when we ask? http://justjavac.com/users
? , the framework returns an HTML document that may be in a table or in a form, or in a div (such as).
b, when we ask? http://justjavac.com/user.json
? , the JSON-formatted data is returned.
JS code??
- [??
- ???? {??
- ???????? "FirstName"?:? "just",??
- ???????? "LastName"?:? "Javac",??
- ???????? "UserName"?:? "@justjavac"??
- ????},??
- ???? {??
- ???????? "FirstName"?:? "Tom",??
- ???????? "LastName"?:? "Cat",??
- ???????? "UserName"?:? "@tomcat"??
- ????},??
- ???? ......??
- ]??
C, when we ask? http://justjavac.com/user.xml
? , XML-formatted data is returned, and the XML document can be defined by a DTD or XSD.
D. If we want to send the list of all users to the administrator, or print it out?
can be accessed directly ? http://justjavac.com/user.xls
, the framework returns an EXCEL spreadsheet. What if we download the file happily and find that the computer does not have Excel installed? It doesn't matter, we can also visit http://justjavac.com/user.jpg
, after all, look at the figure tool we still have.
Have used the Google short URL service of the students know, such as my website http://justjavac.com short URL is? http://goo.gl/JMQJ8, Google also provides a two-dimensional code notation, only need to be added later. QR for example? HTTP://GOO.GL/JMQJ8.QR.
Taourl also provides a handy feature, such as where do we want to see the URL? Http://taourl.com/7c1ug , you only need to add a + sign at the end of the URL.
In short, no matter what extension is used, the same resource will be returned, only in the form of a different expression . Is that what you call it often? Data + template = Output .
What if there is no extension? Return HTML document?
Don't forget the HTTP request's Accept. Set the request header's? Accept: application/x-excel
? We can still get a spreadsheet.
Even when we visit a user,? http://justjavac.com/user/justjavac
, we can use? Accept: text/x-vcard
, if you don't know what it means, go Google yourself.
The following is the design mode, in this function, you can use an adapter mode, according to different extensions to choose different adapters, perform different functions, and finally provide the same interface , the specific implementation will not say much.
3. Multi-lingual support
@TODO? URL structure design with multi-language support
4. Make full use of HTTP
What about the error and other important status information about the request?
Simple, use the HTTP status code! By using the HTTP status code, you don't need to come up with error/success rules for your interface, it's already done for you.
example: if a Consumer submit data (POST) to /api/users
,
- You need to return a successfully created message, at which point you can simply send a 201 status code (201=created).
- If it fails, the server fails to send a 500 (500= internal server error),
- Sends a 400 (400= error request) If the request is interrupted.
- Perhaps they will try to submit data to an interface that does not accept a POST request, and you can send a 501 error (not executed).
- Or if your MySQL server hangs, the interface will also be temporarily interrupted, sending a 503 error (Service unavailable).
Fortunately, you already know this, if you want to learn more about the status code, you can find it on Wikipedia.
HTTP supports client-side caching, with cache-control,expires,last-modified three header fields in the HTTP response, allowing the browser to cache resources for a period of time.
REST can also use these headers to tell the client that there is no need to request resources again for a certain amount of time. This is a great benefit for improving performance. Expires, last-modified, and ETag can be provided through the properties of the resource, which is described in detail in the design of the Model layer.
5. Testing and Commissioning
The flexibility of PHP makes automated testing or TDD difficult, at least as bad as Java. In the framework, it will be very free to start debugging, such as my design is by adding URL parameters:
http://justjavac.com/user/justjavac?DEBUG=2
By adding the debug parameter to tell the framework to turn on debug mode, the following parameter value is the level at which to debug. Similarly, you can also join the log parameter to start the logs.
One of the benefits of this design is that you don't need to modify the configuration file, but you can? Open or close for a particular page . When I use CI, every time I find the problem in the program, the log level in the configuration file is set to all, and then reopen the page, when I look at the log file, it is already hundreds of rows, because every page I visited was recorded in the log.
Testing and URLs do not seem to have much to do with testing in a separate section of the discussion. The URL that I've agreed to test is to add test, such as the justjavac.controller.php written for the controller, which can be accessed through the http://justjavac.com/test/user/justjavac
.
But I still prefer to test in the command line, after all, when you manually click on the browser and manually enter the URL, the manual hit Enter, has violated the automated test .
6. Ajax
@TODO? URL structure design applied to single-page Ajax