Hướng dẫn dùng metodos trong PHP
Trong bài này mình sẽ giới thiệu với mọi người về magic methods trong PHP là gì và các magic methods hay dùng trong PHP. Show
Nội dung chính Show
1, Magic methods là gì.-Magic methods là các phương thức đặc biệt được tạo ra nhằm giải quyết các vấn đề về sự kiện trong chương trình (cụ thể là với class), và đối với PHP cũng thế. -Từ đầu series đến giờ thực ra các bạn cũng đã được giới thiệu qua về magic methods rồi, phương thức khởi tạo và phương thức hủy trong bài số 6 chính là magic methods. 2, Ưu nhược điểm của magic methods.-Bất kì một cái gì đó đều có ưu và nhược điểm cả (không có gì là hoàn hảo). Ưu điểm-Từ khái niệm trên thì các bạn chắc cũng có thể nhận ra các ưu điểm của nó rồi chứ? nó gồm có các ưu điểm như sau:
Nhược điểm
3, Các magic method trong PHP.-Trong PHP có hỗ trợ chúng ta 15 magic method với từng chức năng khác nhau: + + + + + + + + + + + + + + + - Hiện tại thì trên blog của mình đã có đầy đủ các bài hướng dẫn về từng magic methods, các bạn có thể tham khảo thêm ở đây. 3, Lời kết.-Qua bài này mình đã nêu ra được khai niệm của magic methods và danh sách các magic methods trong PHP rồi, nhưng hiện tại thì mình chưa có bài viết nào hướng dẫn cụ thể về cách sử dụng của từng hàm( sẽ viết sau) nên các bạn chịu khó đọc tài liệu tiếng anh nhé. Laravel provides several different approaches to validate your application's incoming data. It is most common to use the 20 method available on all incoming HTTP requests. However, we will discuss other approaches to validation as well.Laravel includes a wide variety of convenient validation rules that you may apply to data, even providing the ability to validate if values are unique in a given database table. We'll cover each of these validation rules in detail so that you are familiar with all of Laravel's validation features. Validation QuickstartTo learn about Laravel's powerful validation features, let's look at a complete example of validating a form and displaying the error messages back to the user. By reading this high-level overview, you'll be able to gain a good general understanding of how to validate incoming request data using Laravel: Defining The RoutesFirst, let's assume we have the following routes defined in our 21 file:
The 22 route will display a form for the user to create a new blog post, while the 23 route will store the new blog post in the database.Creating The ControllerNext, let's take a look at a simple controller that handles incoming requests to these routes. We'll leave the 24 method empty for now:
Writing The Validation LogicNow we are ready to fill in our 24 method with the logic to validate the new blog post. To do this, we will use the 20 method provided by the 27 object. If the validation rules pass, your code will keep executing normally; however, if validation fails, an 28 exception will be thrown and the proper error response will automatically be sent back to the user.If validation fails during a traditional HTTP request, a redirect response to the previous URL will be generated. If the incoming request is an XHR request, a JSON response containing the validation error messages will be returned. To get a better understanding of the 20 method, let's jump back into the 24 method:
As you can see, the validation rules are passed into the 20 method. Don't worry - all available validation rules are documented. Again, if the validation fails, the proper response will automatically be generated. If the validation passes, our controller will continue executing normally.Alternatively, validation rules may be specified as arrays of rules instead of a single 32 delimited string:
In addition, you may use the 33 method to validate a request and store any error messages within a named error bag:
Stopping On First Validation FailureSometimes you may wish to stop running validation rules on an attribute after the first validation failure. To do so, assign the 34 rule to the attribute:
In this example, if the 35 rule on the 36 attribute fails, the 37 rule will not be checked. Rules will be validated in the order they are assigned.A Note On Nested AttributesIf the incoming HTTP request contains "nested" field data, you may specify these fields in your validation rules using "dot" syntax:
On the other hand, if your field name contains a literal period, you can explicitly prevent this from being interpreted as "dot" syntax by escaping the period with a backslash:
Displaying The Validation ErrorsSo, what if the incoming request fields do not pass the given validation rules? As mentioned previously, Laravel will automatically redirect the user back to their previous location. In addition, all of the validation errors and request input will automatically be flashed to the session. An 38 variable is shared with all of your application's views by the 39 middleware, which is provided by the 40 middleware group. When this middleware is applied an 38 variable will always be available in your views, allowing you to conveniently assume the 38 variable is always defined and can be safely used. The 38 variable will be an instance of 44. For more information on working with this object, check out its documentation.So, in our example, the user will be redirected to our controller's 45 method when validation fails, allowing us to display the error messages in the view:
Customizing The Error MessagesLaravel's built-in validation rules each have an error message that is located in your application's 46 file. Within this file, you will find a translation entry for each validation rule. You are free to change or modify these messages based on the needs of your application.In addition, you may copy this file to another translation language directory to translate the messages for your application's language. To learn more about Laravel localization, check out the complete localization documentation. XHR Requests & ValidationIn this example, we used a traditional form to send data to the application. However, many applications receive XHR requests from a JavaScript powered frontend. When using the 20 method during an XHR request, Laravel will not generate a redirect response. Instead, Laravel generates a JSON response containing all of the validation errors. This JSON response will be sent with a 422 HTTP status code.The
|