Lập Trình Ứng Dụng Web Hiệu Quả Với PHP 7

Khám phá các bản thiết kế lập trình PHP 7, cung cấp hướng dẫn chi tiết và ví dụ thực tiễn cho lập trình viên. Nâng cao kỹ năng lập trình hiệu quả.

Trường đại học

Packt Publishing

Chuyên ngành

Lập Trình Web

Người đăng

Ẩn danh

Thể loại

sách

2016

316
2
0

Phí lưu trữ

75 Point

Mục lục chi tiết

Preface

1. Chapter 1: Create a User Profile System and use the Null Coalesce Operator

1.1. The null coalesce operator

1.2. Separation of Concerns

1.3. Creating views

1.4. Create a profile input form

1.5. Admin system

1.6. Summary

2. Chapter 2: Build a Database Class and Simple Shopping Cart

2.1. Building the database abstraction class

2.2. Raw query method

2.3. Create method

2.4. Read method

2.5. Select all method

2.6. Delete method

2.7. Update method

2.8. first_of method

2.9. last_of method

2.10. iterate_over method

2.11. searchString method

2.12. Using the convert_to_json method to implement a simple API

2.13. Shopping Cart

2.14. Building the shopping items list

2.15. Item template rendering function

2.16. Adding checkboxes to the Shopping List page

2.17. Cookies in PHP

2.18. Building the Checkout page

2.19. Thank you page

2.20. Installing TCPDF

2.21. Admin for managing purchases

2.22. Summary

3. Chapter 3: Building a Social Newsletter Service

3.1. Authentication system

3.2. Creating a social login for members

3.3. Member dashboard

3.4. Marketers dashboard

3.5. Administration system for managing marketers

3.6. Custom template for our newsletter

3.7. Link tracking

3.8. AJAX socket chat for support

3.9. Introduction to socket.io

3.10. Summary

4. Chapter 4: Build a Simple Blog with Search Capability using Elasticsearch

4.1. Creating the CRUD and admin system

4.2. Seeding the post table

4.3. What is Elasticsearch?

4.4. Installing Elasticsearch and the PHP client

4.5. Building a PHP Elasticsearch tool

4.6. Adding documents to our Elasticsearch

4.7. Querying Elasticsearch

4.8. Installing Logstash

4.9. Setting up the Logstash configuration

4.10. Installing PHP Redis

4.11. Encoding and decoding JSON messages

4.12. Storing Apache logs in Elasticsearch

4.13. Getting filtered data to display with Highcharts

4.14. Dashboard app for viewing Elasticsearch logs

4.15. Simple search engine with result caching

4.16. Cache basics

4.17. Cache invalidation of Redis data

4.18. Using browser localStorage as cache

4.19. Working with streams

4.20. Storing and searching XML documents using PHP

4.21. Using Elasticsearch to search a social network database

4.22. Displaying randomized search engine results

4.23. Summary

5. Chapter 5: Creating a RESTful Web Service

5.1. RESTful basics

5.2. REST architectures

5.3. Common HTTP methods and response codes

5.4. First steps with the Slim framework

5.5. Installing Slim

5.6. A small sample application

5.7. Accepting URL parameters

5.8. Accepting HTTP requests with a message body

5.9. The PSR-7 standard

5.10. Middleware

5.11. Implementing the REST service

5.12. Designing the service

5.13. Bootstrapping the project

5.14. Building the persistence layer with MongoDB

5.15. Adding and retrieving users

5.16. Listing and searching users

5.17. Deleting profiles

5.18. Validating input

5.19. Streams and large files

5.20. Profile image upload

5.21. Using GridFS storage

5.22. Summary

6. Chapter 6: Building a Chat Application

6.1. The WebSocket protocol

6.2. First steps with Ratchet

6.3. Architectural considerations

6.4. Getting started

6.5. Testing WebSocket applications

6.6. Playing with the event loop

6.7. Implementing the chat application

6.8. Bootstrapping the project server-side

6.9. Bootstrapping the HTML user interface

6.10. Building a simple chat application

6.11. Receiving messages

6.12. Sending messages

6.13. Testing the application

6.14. Keeping the connection from timing out

6.15. Deployment options

6.16. Bridging Ratchet and PSR-7 applications

6.17. Accessing your application via the web server

6.18. Adding authentication

6.19. Creating the login form

6.20. Checking the authorization

6.21. Connecting users and messages

6.22. Summary

7. Chapter 7: Building an Asynchronous Microservice Architecture

7.1. The target architecture

7.2. ZeroMQ patterns

7.3. Request/reply pattern

7.4. Publish/subscribe pattern

7.5. Push/pull pattern

7.6. Bootstrapping the project

7.7. Building the inventory service

7.8. Getting started with ZeroMQ REQ/REP sockets

7.9. Using JsonRPC for communication

7.10. Making the inventory service multithreaded

7.11. Building the checkout service

7.12. Using react/zmq

7.13. Working with promises

7.14. Building the mailing service

7.15. Building the shipping service

7.16. PUSH/PULL for beginners

7.17. Fan-out/fan-in

7.18. Bridging ZeroMQ and HTTP

7.19. Summary

8. Chapter 8: Building a Parser and Interpreter for a Custom Language

8.1. How interpreters and compilers work

8.2. Languages and grammars

8.3. Your first PEG parser

8.4. Evaluating expressions

8.5. Building an Abstract Syntax Tree

8.6. Building a better interface

8.7. Evaluating variables

8.8. Adding logical expressions

8.9. Comparisons

8.10. The “and” and “or” operators

8.11. Conditions

8.12. Working with structured data

8.13. Working with objects

8.14. Optimizing the interpreter by adding a compiler

8.15. Verifying performance improvements

8.16. Summary

9. Chapter 9: Reactive Extensions in PHP

9.1. An introduction to observables

9.2. Introduction to event loop and ReactiveX

9.3. delay

9.4. defer

9.5. Scheduler

9.6. recursive-scheduler

9.7. map and flatMap

9.8. reduce

9.9. toArray

9.10. merge

9.11. do

9.12. scan

9.13. zip

9.14. Parsing logs through a Reactive scheduler

9.15. Event queues with ReactiveX

9.16. Summary

Index

Tóm tắt

I. Giới thiệu về lập trình ứng dụng web với PHP 7

PHP 7 là phiên bản mới nhất của ngôn ngữ lập trình PHP, mang lại nhiều cải tiến về hiệu suất và tốc độ. Việc sử dụng PHP 7 trong phát triển ứng dụng web giúp tối ưu hóa quy trình lập trình và nâng cao trải nghiệm người dùng. Bài viết này sẽ cung cấp cái nhìn tổng quan về PHP 7 và cách nó có thể được áp dụng trong lập trình ứng dụng web.

1.1. Tại sao chọn PHP 7 cho phát triển web

PHP 7 cung cấp hiệu suất vượt trội so với các phiên bản trước, giúp giảm thời gian tải trang và cải thiện trải nghiệm người dùng. Các tính năng mới như cải thiện quản lý bộ nhớ và hỗ trợ các framework PHP hiện đại làm cho PHP 7 trở thành lựa chọn hàng đầu cho lập trình web.

1.2. Các tính năng nổi bật của PHP 7

PHP 7 giới thiệu nhiều tính năng mới như toán tử null coalesce, cải tiến về xử lý lỗi và hỗ trợ các kiểu dữ liệu mới. Những tính năng này không chỉ giúp lập trình viên viết mã dễ dàng hơn mà còn nâng cao hiệu suất của ứng dụng.

II. Thách thức trong lập trình ứng dụng web với PHP 7

Mặc dù PHP 7 mang lại nhiều lợi ích, nhưng cũng tồn tại một số thách thức trong quá trình phát triển ứng dụng web. Việc chuyển đổi từ các phiên bản cũ sang PHP 7 có thể gặp khó khăn, đặc biệt là với các mã nguồn cũ không tương thích.

2.1. Vấn đề tương thích mã nguồn cũ

Nhiều ứng dụng được phát triển trên các phiên bản PHP cũ có thể không hoạt động đúng trên PHP 7. Điều này đòi hỏi lập trình viên phải kiểm tra và cập nhật mã nguồn để đảm bảo tính tương thích.

2.2. Khó khăn trong việc học và áp dụng các tính năng mới

Các lập trình viên mới có thể gặp khó khăn trong việc làm quen với các tính năng mới của PHP 7. Việc nắm vững các khái niệm như toán tử null coalesce và các cải tiến về hiệu suất là rất quan trọng để phát triển ứng dụng hiệu quả.

III. Phương pháp lập trình ứng dụng web hiệu quả với PHP 7

Để phát triển ứng dụng web hiệu quả với PHP 7, lập trình viên cần áp dụng các phương pháp và công cụ phù hợp. Việc sử dụng các framework PHP như Laravel hoặc CodeIgniter có thể giúp tăng tốc quá trình phát triển.

3.1. Sử dụng framework PHP để tối ưu hóa phát triển

Framework PHP cung cấp các công cụ và thư viện sẵn có, giúp lập trình viên tiết kiệm thời gian và công sức. Laravel, với cú pháp rõ ràng và dễ sử dụng, là một trong những lựa chọn phổ biến nhất.

3.2. Tích hợp cơ sở dữ liệu MySQL với PHP 7

Việc kết hợp PHP 7 với cơ sở dữ liệu MySQL giúp quản lý dữ liệu hiệu quả hơn. Sử dụng PDO hoặc MySQLi để thực hiện các truy vấn an toàn và hiệu quả là rất quan trọng trong phát triển ứng dụng.

IV. Ứng dụng thực tiễn của PHP 7 trong phát triển web

PHP 7 đã được áp dụng rộng rãi trong nhiều dự án phát triển web, từ các trang thương mại điện tử đến các hệ thống quản lý nội dung. Các ứng dụng này không chỉ cải thiện hiệu suất mà còn nâng cao trải nghiệm người dùng.

4.1. Ví dụ về ứng dụng thương mại điện tử

Nhiều trang web thương mại điện tử hiện nay sử dụng PHP 7 để xử lý hàng triệu giao dịch mỗi ngày. Sự cải thiện về tốc độ và hiệu suất giúp tăng cường khả năng phục vụ khách hàng.

4.2. Hệ thống quản lý nội dung CMS với PHP 7

Các hệ thống CMS như WordPress đã cập nhật để hỗ trợ PHP 7, mang lại trải nghiệm mượt mà hơn cho người dùng và quản trị viên. Điều này cho phép người dùng dễ dàng quản lý nội dung mà không gặp phải vấn đề về hiệu suất.

V. Kết luận và tương lai của lập trình ứng dụng web với PHP 7

PHP 7 đã chứng minh được giá trị của mình trong phát triển ứng dụng web. Với những cải tiến liên tục và sự hỗ trợ từ cộng đồng, PHP 7 sẽ tiếp tục là một lựa chọn hàng đầu cho lập trình viên trong tương lai.

5.1. Tương lai của PHP và các phiên bản tiếp theo

PHP đang trong quá trình phát triển liên tục, với các phiên bản mới hứa hẹn mang lại nhiều tính năng và cải tiến hơn nữa. Điều này sẽ giúp lập trình viên dễ dàng hơn trong việc phát triển ứng dụng.

5.2. Tăng cường bảo mật ứng dụng web với PHP 7

Bảo mật là một yếu tố quan trọng trong phát triển ứng dụng web. PHP 7 cung cấp nhiều công cụ và phương pháp để bảo vệ ứng dụng khỏi các mối đe dọa, giúp lập trình viên xây dựng ứng dụng an toàn hơn.

17/07/2025

Trích đoạn nội dung tài liệu

com https://fb.com/tailieudientucntt PHP 7 Programming Blueprints Learn how to exploit the impressive power of PHP 7 with this collection of practical project blueprints – begin building better applications for the web today! Jose Palala Martin Helmich BIRMINGHAM - MUMBAI CuuDuongThanCong.com https://fb.com/tailieudientucntt PHP 7 Programming Blueprints Copyright © 2016 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied.

Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: October 2016 Production reference: 2061016 Published by Packt Publishing Ltd.

Livery Place 35 Livery Street Birmingham B3 2PB, UK.com https://fb.com/tailieudientucntt Credits Authors Copy Editor Jose Palala Safis Editing Martin Helmich Reviewer Project Coordinator Shuvankar Sarkar Ulhas Kambali Commissioning Editor Proofreader Kunal Parikh Safis Editing Acquisition Editor Indexer Chaitanya Nair Rekha Nair Content Development Editor Production Coordinator Onkar Wani Melwyn Dsa Technical Editor Cover Work Murtaza Tinwala Melwyn Dsa CuuDuongThanCong.com https://fb.com/tailieudientucntt About the Authors Jose Palala has been working professionally with PHP for at least 8 years. He has experience working with PHP frameworks such as Eden PHP, CodeIgniter, Laravel and Zend. He has worked for Philippine-based IT companies for at least 8 years, working on projects ranging from internal corporate systems and CMS websites. In his spare time, he regularly contributes back to the tech community in the Philippines.

I would like to thank everyone at Packt Publishing, it’s been great working with them since Day 1. Super thanks to all to my colleagues, friends and family who have helped me to become a better developer and have helped me become what I am today. Martin Helmich holds a Master's degree in Computer Science from the University of Applied Sciences in Osnabrück. He works as a software architect and specializes in building distributed applications using web technologies and Microservice Architectures.

Besides programming in Go, PHP, Python and Node.JS, he also builds infrastructures using configuration management tools like SaltStack and container technologies like Docker. He is an open source enthusiast and likes to make fun of people who are not using Linux. In his free time, you'll probably find him coding on one of his open source pet projects, listening to music or reading science-fiction literature.com https://fb.com/tailieudientucntt About the Reviewer Shuvankar Sarkar is an IT analyst and experienced in C#, .NET, PHP and web development. He is a technology enthusiast and maintains his blog at http://shuvankar.

You can follow him on Twitter at @sonu041. He is interested in computer security as well. I would like to thank my family for making my life easier and full of happiness.com https://fb.com/tailieudientucntt www.com eBooks, discount offers, and more Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at customercare@packtpub.com for more details.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.com/books/subscription/packtlib Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library.

Here, you can search, access, and read Packt's entire library of books. Why subscribe? Fully searchable across every book published by Packt Copy and paste, print, and bookmark content On demand and accessible via a web browser CuuDuongThanCong.com https://fb.com/tailieudientucntt Table of Contents Preface 1 Chapter 1: Create a User Profile System and use the Null Coalesce Operator 6 The null coalesce operator 7 Separation of Concerns 8 Creating views 8 Create a profile input form 10 Admin system 13 Summary 17 Chapter 2: Build a Database Class and Simple Shopping Cart 18 Building the database abstraction class 19 Raw query method 19 Create method 19 Read method 20 Select all method 20 Delete method 21 Update method 21 first_of method 22 last_of method 22 iterate_over method 22 searchString method 22 Using the convert_to_json method to implement a simple API 23 Shopping Cart 23 Building the shopping items list 24 Item template rendering function 24 Adding checkboxes to the Shopping List page 27 Cookies in PHP 28 Building the Checkout page 29 Thank you page 32 Installing TCPDF 33 Admin for managing purchases 38 Summary 40 Chapter 3: Building a Social Newsletter Service 41 CuuDuongThanCong.com https://fb.com/tailieudientucntt Authentication system 41 Creating a social login for members 47 Member dashboard 52 Marketers dashboard 56 Administration system for managing marketers 57 Custom template for our newsletter 60 Link tracking 63 AJAX socket chat for support 66 Introduction to socket.io 66 Summary 73 Chapter 4: Build a Simple Blog with Search Capability using Elasticsearch 75 Creating the CRUD and admin system 75 Seeding the post table 80 What is Elasticsearch? 80 Installing Elasticsearch and the PHP client 81 Building a PHP Elasticsearch tool 85 Adding documents to our Elasticsearch 87 Querying Elasticsearch 88 Installing Logstash 88 Setting up the Logstash configuration 89 Installing PHP Redis 89 Encoding and decoding JSON messages 91 Storing Apache logs in Elasticsearch 92 Getting filtered data to display with Highcharts 93 Dashboard app for viewing Elasticsearch logs 99 Simple search engine with result caching 101 Cache basics 103 Cache invalidation of Redis data 103 Using browser localStorage as cache 104 Working with streams 106 Storing and searching XML documents using PHP 108 Using Elasticsearch to search a social network database 108 Displaying randomized search engine results 110 Summary 112 Chapter 5: Creating a RESTful Web Service 113 RESTful basics 114 REST architectures 114 Common HTTP methods and response codes 115 First steps with the Slim framework 116 [ ii ] CuuDuongThanCong.com https://fb.com/tailieudientucntt Installing Slim 116 A small sample application 117 Accepting URL parameters 118 Accepting HTTP requests with a message body 119 The PSR-7 standard 120 Middleware 122 Implementing the REST service 124 Designing the service 124 Bootstrapping the project 125 Building the persistence layer with MongoDB 127 Adding and retrieving users 132 Listing and searching users 137 Deleting profiles 140 Validating input 143 Streams and large files 145 Profile image upload 145 Using GridFS storage 149 Summary 152 Chapter 6: Building a Chat Application 153 The WebSocket protocol 153 First steps with Ratchet 154 Architectural considerations 154 Getting started 155 Testing WebSocket applications 158 Playing with the event loop 159 Implementing the chat application 162 Bootstrapping the project server-side 162 Bootstrapping the HTML user interface 164 Building a simple chat application 165 Receiving messages 170 Sending messages 171 Testing the application 171 Keeping the connection from timing out 172 Deployment options 173 Bridging Ratchet and PSR-7 applications 176 Accessing your application via the web server 182 Adding authentication 183 Creating the login form 184 Checking the authorization 188 [ iii ] CuuDuongThanCong.com https://fb.com/tailieudientucntt Connecting users and messages 190 Summary 192 Chapter 7: Building an Asynchronous Microservice Architecture 193 The target architecture 193 ZeroMQ patterns 195 Request/reply pattern 195 Publish/subscribe pattern 196 Push/pull pattern 196 Bootstrapping the project 197 Building the inventory service 198 Getting started with ZeroMQ REQ/REP sockets 198 Using JsonRPC for communication 200 Making the inventory service multithreaded 205 Building the checkout service 209 Using react/zmq 209 Working with promises 211 Building the mailing service 219 Building the shipping service 223 PUSH/PULL for beginners 223 Fan-out/fan-in 228 Bridging ZeroMQ and HTTP 230 Summary 236 Chapter 8: Building a Parser and Interpreter for a Custom Language 237 How interpreters and compilers work 238 Languages and grammars 239 Your first PEG parser 242 Evaluating expressions 246 Building an Abstract Syntax Tree 249 Building a better interface 254 Evaluating variables 256 Adding logical expressions 259 Comparisons 260 The “and” and “or” operators 263 Conditions 265 Working with structured data 268 Working with objects 271 Optimizing the interpreter by adding a compiler 273 Verifying performance improvements 281 [ iv ] CuuDuongThanCong.com https://fb.com/tailieudientucntt Summary 285 Chapter 9: Reactive Extensions in PHP 286 An introduction to observables 286 Introduction to event loop and ReactiveX 287 delay 289 defer 289 Scheduler 290 recursive-scheduler 290 map and flatMap 292 reduce 292 toArray 293 merge 294 do 294 scan 295 zip 296 Parsing logs through a Reactive scheduler 297 Event queues with ReactiveX 298 Summary 299 Index 300 [v] CuuDuongThanCong.com https://fb.com/tailieudientucntt Preface PHP is a great language for developing web applications. It is essentially a server-side scripting language that is also used for general-purpose programming. PHP 7 is the latest version, which provides major backward-compatibility breaks and focuses on providing improved performance and speed.

With the rise in demand for high performance, this newest version contains everything you need to build efficient applications. PHP 7 provides improved engine execution, better memory usage, and a better set of tools allowing you to maintain high traffic on your websites with low-cost hardware and servers through a multithreading web server. What this book covers Chapter 1, Create a User Profile System and use the Null Coalesce Operator, we'll discover new PHP 7 features and build app for storing user profiles. Chapter 2, Build a Database Class and Simple Shopping Cart, we'll create a simple database layer library which will help us access our database.

We'll cover some tips on making our queries secure, and how to make our coding simpler and more succinct with PHP 7. Chapter 3, Building a Social Newsletter Service, we'll be building a social newsletter service, which will have a way for users to sign in using their social login and allow them to register to a newsletter. We'll also make a simple admin system for managing the newsletters. Chapter 4, Build a Simple Blog with Search Capability using Elasticsearch, you will learn how to create a blog system, experiment with ElasticSearch and how to apply it in your code.

Also,​ you will learn how to create a simple blog application and store data into MySQL. Chapter 5, Creating a RESTful Web Service, shows you how create a RESTful web service that can be used to manage user profiles. The service will be implemented using the Slim micro framework and use a MongoDB database for persistence. The chapter also covers the basics of RESTful web services, most importantly the common HTTP request and response methods, the PSR-7 standard and PHP 7’s new mongodb extension.com https://fb.com/tailieudientucntt Preface Chapter 6, Building a Chat Application, describes the implementation of a real-time chat application using WebSockets.

You will learn how to use the Ratchet framework to build stand-alone WebSocket and HTTP servers with PHP and how to connect to WebSocket servers in a JavaScript client application. We will also discuss how you can implement authentication for WebSocket applications and how to deploy them in a production environment. Chapter 7, Building an Asynchronous Microservice Architecture, covers the implementation of a (small) microservice architecture. Instead of RESTful web services, you will use ZeroMQ in this chapter for network communication, an alternative communication protocol that focuses on asynchronicity, loose coupling and high performance.

Chapter 8, Building a Parser and Interpreter for a Custom Language, describes how to use the PHP-PEG library to define a grammar and implement a parser for a custom expression language that can be used to add end-user development features to enterprise applications. Chapter 9, Reactive Extensions in PHP, here we'll look into the Reactive extensions library for PHP, and and try to build a simple scheduled app.

Nội dung được bảo vệ bản quyền — Tải xuống đầy đủ