Self-developed PHPMVC framework (1) -- URL

Source: Internet
Author: User
Design the PHPMVC framework by yourself (1) -- URL? Today, when frameworks are prevalent, MVC is no longer a myth. I often hear a lot of programmers discuss which framework is good and which one is not good. actually, what? The framework is just a tool, not good or bad, only suitable and not suitable, suitable for yourself is the best. Every time I interview a freshman, I will ask him what framework he has used and talk about his understanding of these frameworks. Design php mvc framework by yourself (1) -- URL

?

Today, when frameworks are prevalent, MVC is no longer a myth. I often hear a lot of programmers discuss which framework is good and which one is not good. actually, what?The framework is just a tool, not good or bad, only suitable and not suitable, suitable for yourself is the best.

Every time I interview a freshman, I will ask him what framework he has used and talk about his understanding of these frameworks. When interviewing experienced programmers, they are asked to write a framework by themselves. In fact, it is not for him to code, as long as there is a train of thought, OK. In my opinion, if a programmer with one year of experience cannot even develop a Framework v0.0.1, he must have not understood it in depth.

A few days ago, @ phoenixg said he had to write an MVC framework. In addition, he did not just talk about it. just over a weekend, the prototype of the framework appeared magically? Github? .

This blog post is named "do it yourself ".Design? Php mvc framework, so this article does not involve too much encoding. any code snippets in this article are directly typed in vim without any tests, if you want to use the code in this article, you must test it yourself.

Follow this tutorial to design an MVC framework of your own from scratch.

I have used ZendFramwork and CodeIgniter. each framework has its own advantages and disadvantages. Before writing this article, I read the core source code of Symfony, cakephp, MooPHP, doitphp, etc. The following describes what I will design my framework like, this chapter mainly discusses URL design.

1. REST

In this age of REST, if a framework does not support RESTAvant-garde programmerThis framework also supports REST.

The first design principle :?All things are resources and resources have multiple forms of representation..

No matter whether it actually exists or is abstract, all resources will have a constant ID. no API operation on resources should change the resource ID.

In fact, all of the above are proposed based on the characteristics of the Internet.

  • A url is a resource on the Internet;
  • The resource content is an HTML page;
  • No matter how you change the HTML content, the URL will not change;
  • Resources are linked through connections in HTML;
  • Each time you get the complete HTML content.

For example

Php code ??
  1. GET? Http://justjavac.com/users ????????????? //? All users ??
  2. GET? Http://justjavac.com/users/phper ??? //? User identified as phper ??
2. extension

Here I will not discuss the relationship between the extension and the file type, and "The extension is just a convention, and the file type is recorded in the file header ".

I usually regard the extension as a "convention" instead of a file type. When we request a news.html file, we cannot believe that the news.html file stored on the server may also be a php file or jsp file. in the popular nodejs today, it may also be a js file. However, no matter how the page is generated, it is clear that we finally get an html document.

Although rest does not require an extension, someone tells me that if you add a. rmvb extension after a girl's name, it will become very ...... Therefore, this framework supports extensions, but extensions are part of resources.

What does it mean?

In the previous example, how do I represent the resource of all users? Use url?http://justjavac.com/users? It can be a unique identifier, and?The extension can be used to identify different expressions of resources..

A. when we request?http://justjavac.com/users? The framework returns an html document. The data may be in the table, form, or div (for example ).

B. when we request?http://justjavac.com/user.json? The return value is in json format.

Js code ??
  1. [??
  2. ???? {??
  3. ???????? "FirstName "? :? "Just ",??
  4. ???????? "LastName "? :? "Javac ",??
  5. ???????? "UserName "? :? "@ Justjavac "??
  6. ????},??
  7. ???? {??
  8. ???????? "FirstName "? :? "Tom ",??
  9. ???????? "LastName "? :? "Cat ",??
  10. ???????? "UserName "? :? "@ Tomcat "??
  11. ????},??
  12. ????......??
  13. ] ???

C. when we request?http://justjavac.com/user.xml? The data in xml format is returned. the xml document can be defined by DTD or XSD.

D. What if we want to send the list of all users to the administrator or print it out?

Can I access it directly?http://justjavac.com/user.xlsThe framework returns an Excel worksheet. When we were so happy to download the file, we found that Excel was not installed on the computer. what should we do? It doesn't matter. we can also accesshttp://justjavac.com/user.jpgAfter all, we still have some graph tools.

Anyone who has used the Google short URL service knows, for example, what is the short url of my website http://justjavac.com? The http://goo.gl/JMQJ8,Google also provides a qr code representation that only needs to be added after. qr example? Http://goo.gl/jmqj8.qr.

Taourl also provides a convenient function. for example, if you want to view the URL? Http://taourl.com/7c1ug? You only need to add a + number at the end of the URL.

In short,No matter what extension is used, the same resource will be returned, but the format is different.. This is what is often said?Data + template = output.

What if there is no extension? Return HTML document?

Do not forget the http request Accept. Set the request header?Accept: application/x-excel? We can still get a workbook.

Even when we access a user ,?http://justjavac.com/user/justjavacCan we use?Accept: text/x-vcardIf you don't know what it means, Google it yourself.

The following describes the design mode. an adapter mode can be used for this function,Select different adapters based on different extensions, execute different functions, and finally provide the same interface.

3. multi-language support

@ TODO? Url structure design supported by multiple languages

4. make full use of HTTP

What about request-related errors and other important status information?

Simple: use the HTTP status code! By using the HTTP status code, you do not need to come up with an error/success rule for your interface. it is ready for you.

For example, if a consumer submits data (POST)?/api/users,

  • You need to return a successfully Created message. at this time, you can simply send a 201 status code (201 = Created ).
  • If a failure occurs, the server sends a 500 error (500 = Internal server error ),
  • If the request is interrupted, a 400 (400 = error request) is sent ).
  • Maybe they will try to submit data to an interface that does not accept POST requests, and you can send a 501 error (not executed ).
  • Or if your MySQL server fails, the interface will be temporarily interrupted and a 503 error will be sent (the service is unavailable ).

Fortunately, you already know this. if you want to know

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.