Quiông xã summary↬ In the early days of PHP.. applications, “spaghetti code” was a familiar sight. In this article, we’ll cover how lớn separate the view of your PHP application from its other components.
We’ll look at why using such an architecture is useful và what tools we can use khổng lồ accomplish this. Here’s what we’ll cover:Learn some basic MVC concepts,đánh giá some popular templating libraries,Play around with a small custom-made view class.Explore the basics of using the Twig library.
Bạn đang xem: Code php mẫu
To fully benefit from this article, you should already know how khổng lồ write & run your own PHP scripts on a Web server (i.e. using Apache).
Further Reading on SmashingMag:
Meet Smashing E-Mail Newsletter with useful tips on front-end, thiết kế & UX. Subscribe & get “Smart Interface Design Checklists” — a free PDF deck with 150+ questions khổng lồ ask yourself when designing & building almost anything.
A Quichồng Introduction To The MVC Pattern
In the early days of PHP.. applications, “spaghetti code” was a familiar sight. Fragments of PHPhường code were mixed in with HTML mark-up. There were no frameworks, so Web applications were just a bunch of source files. As the PHP language matured, developers started khổng lồ think about the cleanliness & maintainability of their code. The model-view-controller (MVC) pattern was introduced.
User actions are passed (as HTTP requests, GET or POST methods) to lớn the controller. The controller is a piece of code that handles và processes user input & then reads and makes necessary changes lớn the model, which is responsible for the storage & modification of data. (In simple terms, the mã sản phẩm consists of the database structure và contents, và the code used lớn access it.) Then, the controller generates the proper view that will be sent & displayed to user.
Such separation of layers has many advantages…
Code Is Easier lớn Maintain
Because the Model is separated, changing internal data relations without changing the rest of the application is easier. For example, the model for the user could provide the is_active() method, returning a boolean variable. In the business lô ghích, it would be enough to lớn check what value is returned by this method, without any knowledge of its internals.
You can also check for various conditions (for example, whether the user has confirmed their registration, paid their monthly fee, etc.), và you can change these rules in one place, without having to lớn change anything in the other parts of the code. It also becomes easy to make a platform-independent application that allows for switching between database engines; this is often performed by ORM (object-relational mapper) libraries, which are a sub-layer of the Mã Sản Phẩm layer. ORMs add a cấp độ of abstraction to lớn the library that accesses the specific database directly.
The Same Content in Multiple Views
Separating the view allows a single result to be presented in different forms. For example, based on some súc tích, a news page could be displayed in normal, điện thoại và RSS versions, all using the same content returned from the controller.
Separating the layers forces the programmer lớn design better applications that are easier và cheaper khổng lồ maintain. Also, designers and front-kết thúc developers don’t have sầu khổng lồ work with the business-xúc tích code, because their task is only khổng lồ display the contents of the variables that are provided khổng lồ the view. This reduces the risk of the breaking some PHP code.
Today, every modern framework implements the MVC architecture. There are also some libraries that enable you to lớn use selected features of the architecture. In this article, we’ll go over the recipes for the view, and in future articles we’ll cover the Mã Sản Phẩm and some ORM libraries.
Overview Of PHPhường Template Engines
As you might expect, many templating libraries allow you lớn separate the view layer from the rest of the application. In the most basic scenario, you can implement such a library by yourself (which we’ll vị in the next section). You don’t even need a special library; sometimes it is enough just to lớn separate your view (i.e. the template’s HTML files) into a different directory, prepare some variables in the simple controller (usually the main PHP. file) và include the templates.
Every modern PHP Web application framework employs some kind of templating engine. Most of them use plain PHPhường by default (including Symfony 1.x, Zover Framework & CakePHP), but many standalone libraries can be plugged into lớn your favorite framework or custom application.
Smarty was one of the first và most advanced templating engines. It was developed as a sub-project of the main PHPhường project, but it has lost its popularity in recent years due khổng lồ poor performance and forced backward compatibility with PHPhường 4. It has also lacked many modern features, such as template inheritance. Now, it is rising baông xã to lớn power with version 3. Many other frameworks (such as Twig & Django) have sầu inherited some of the basic concepts laid down by Smarty.Info User Information: Name: capitalize Addr: $address Date: date_format:"%b %e, %Y"