You are previewing PHP and MongoDB Web Development.
O'Reilly logo
PHP and MongoDB Web Development

Book Description

"

Combine the power of PHP and MongoDB to build dynamic web 2.0 applications

  • Learn to build PHP-powered dynamic web applications using MongoDB as the data backend

  • Handle user sessions, store real-time site analytics, build location-aware web apps, and much more, all using MongoDB and PHP

  • Full of step-by-step instructions and practical examples, along with challenges to test and improve your knowledge

In Detail

With the rise of Web 2.0, the need for a highly scalable database, capable of storing diverse user-generated content is increasing. MongoDB, an open-source, non-relational database has stepped up to meet this demand and is being used in some of the most popular websites in the world. MongoDB is one of the NoSQL databases which is gaining popularity for developing PHP Web 2.0 applications.

PHP and MongoDB Web Development Beginner’s Guide is a fast-paced, hands-on guide to get started with web application development using PHP and MongoDB. The book follows a “Code first, explain later” approach, using practical examples in PHP to demonstrate unique features of MongoDB. It does not overwhelm you with information (or starve you of it), but gives you enough to get a solid practical grasp on the concepts.

The book starts by introducing the underlying concepts of MongoDB. Each chapter contains practical examples in PHP that teache specific features of the database.

The book teaches you to build a blogging application, handle user sessions and authentication, and perform aggregation with MapReduce. You will learn unique MongoDB features and solve interesting problems like real-time analytics, location-aware web apps etc. You will be guided to use MongoDB alongside MySQL to build a diverse data back-end.

With its concise coverage of concepts and numerous practical examples, PHP and MongoDB Web Development Beginner’s Guide is the right choice for the PHP developer to get started with learning MongoDB.

"

Table of Contents

  1. PHP and MongoDB Web Development
    1. PHP and MongoDB Web Development
    2. Credits
    3. About the Author
    4. About the Reviewers
    5. www.PacktPub.com
      1. Support files, eBooks, discount offers and more
        1. Why Subscribe?
        2. Free Access for Packt account holders
    6. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Time for action - heading
        1. What just happened?
        2. Pop Quiz - heading
        3. Have a go hero heading
      6. Reader feedback
      7. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    7. 1. Getting Started with MongoDB
      1. The NoSQL movement
        1. Types of NoSQL databases
      2. MongoDB A document-based NoSQL database
        1. Why MongoDB?
        2. Who is using MongoDB?
        3. MongoDB concepts—Databases, collections, and documents
          1. Anatomy of document
          2. BSON—The data exchange format for MongoDB
          3. Similarity with relational databases
      3. Downloading, installing, and running MongoDB
        1. System requirements
      4. Time for action - downloading and running MongoDB on Windows
        1. What just happened?
        2. Installing the 64-bit version
      5. Time for action - downloading and running MongoDB on Linux
        1. What just happened?
        2. Installing MongoDB on OS X
        3. Configuring MongoDB
          1. Command-line parameters
          2. File-based configuration
        4. Have a go hero configure MongoDB to run with non-default settings
        5. Stopping MongoDB
          1. Hitting Control + C
          2. From the mongo shell
          3. Sending INT or TERM signal in UNIX
      6. Creating databases, collections, and documents
      7. Time for action - creating databases, collections, and documents
        1. What just happened?
        2. Pop Quiz - configuring MongoDB
      8. Installing the PHP driver for MongoDB
      9. Time for action - installing PHP driver for MongoDB on Windows
        1. What just happened?
        2. Installing the PHP-MongoDB driver on Unix
      10. Connecting to the MongoDB server from PHP
        1. Creating a PHP-Mongo connection
      11. Time for action - creating a connection to the MongoDB server from PHP
        1. What just happened?
        2. Configuring the PHP-MongoDB connection
        3. Specifying timeout for the connection attempt
        4. Have a go hero connect to a MongoDB server on a networked computer
      12. Summary
    8. 2. Building your First MongoDB Powered Web App
      1. A MongoDB powered blog
      2. Have the MongoDB server running
      3. Inserting documents in MongoDB
      4. Time for action - building the Blog Post Creator
        1. What just happened?
        2. Creating databases and collections implicitly
        3. Performing 'safe' inserts
          1. Benefits of safe inserts
        4. Specifying a timeout on insert
        5. Setting the user generated _id
        6. The MongoDate object
        7. Have a go hero allow storing tags for an article
      5. Querying documents in a collection
      6. Time for action - retrieving articles from a database
        1. What just happened?
        2. The Mongo Query Language
          1. The MongoCursor object
          2. Conditional Queries
        3. Pop Quiz - what does this query do?
      7. Doing advanced queries in MongoDB
      8. Time for action - building the Blog Dashboard
        1. What just happened?
        2. Returning a subset of fields
        3. Sorting the query results
        4. Using count, skip, and limit
        5. Performing range queries on dates
        6. Have a go hero rewrite blogs.php
      9. Updating documents in MongoDB
      10. Time for action - building the Blog Editor
        1. What just happened?
        2. Optional arguments to the update method
          1. Performing 'upsert'
          2. Using update versus using save
          3. Using modifier operations
            1. Setting with $set
            2. Incrementing with $inc
            3. Deleting fields with $unset
            4. Renaming fields with $rename
          4. Have a go hero merge Blog editor and creator into a single module
      11. Deleting documents in MongoDB
      12. Time for action - deleting blog posts
        1. What just happened?
        2. Optional arguments to remove
      13. Managing relationships between documents
        1. Embedded documents
        2. Referenced documents
      14. Time for action - posting comments to blog posts
        1. What just happened?
        2. Embedded versus referenced Which one to use?
          1. Querying embedded objects
        3. Have a go hero get comments by username
      15. Summary
    9. 3. Building a Session Manager
      1. Understanding HTTP sessions
      2. Understanding PHP native session handling
      3. Time for action - testing native PHP session handling
        1. What just happened?
        2. Limitations of native PHP session handling
      4. Implementing session handling with MongoDB
        1. Extending session handling with session_set_save_handler
        2. The SessionManager class
      5. Time for action - building the SessionManager class
        1. What just happened?
        2. How the SessionManager works
          1. The constructor
          2. The open and close methods
          3. The read method
          4. The write method
          5. The destroy method
          6. The gc method
        3. Pop Quiz - what does session_destroy() do?
      6. Putting the SessionManager in action
      7. Time for action - putting SessionManager into action
        1. What just happened?
      8. Building the user authentication module
      9. Time for action - building the User class
        1. What just happened?
      10. Creating the login, logout, and user profile page
      11. Time for action - creating the login, logout, and profile page
        1. What just happened?
        2. Have a go hero implement user authentication in the blogging web app
      12. Using good session practices
        1. Setting low expiry times of session cookies
        2. Using session timeouts
        3. Setting proper domains for session cookies
        4. Checking for browser consistency
        5. Have a go hero store and verify User Agent in SessionManager
      13. Summary
    10. 4. Aggregation Queries
      1. Generating sample data
      2. Time for action - generating sample data
        1. What just happened?
      3. Understanding MapReduce
        1. Visualizing MapReduce
        2. Pop Quiz - MapReduce basics
      4. Performing MapReduce in MongoDB
      5. Time for action - counting the number of articles for each author
        1. What just happened?
        2. Defining the Map function
        3. Defining the Reduce function
        4. Applying the Map and Reduce
        5. Viewing the results
        6. Performing MapReduce on a subset of the collection
        7. Concurrency
      6. Performing MongoDB MapReduce within PHP
      7. Time for action - creating a tag cloud
        1. What just happened?
        2. Have a go hero repeat the earlier example with PHP
      8. Performing aggregation using group()
      9. Time for action - calculating the average rating per author
        1. What just happened?
        2. Grouping by custom keys
        3. MapReduce versus group()
        4. Have a go hero find the maximum and minimum rating for each author
        5. Pop Quiz - limitation of group()
      10. Listing distinct values for a field
      11. Time for action - listing distinct categories of articles
        1. What just happened?
        2. Using distinct() in mongo shell
      12. Counting documents with count()
      13. Summary
    11. 5. Web Analytics using MongoDB
      1. Why MongoDB is a good choice as a web analytics backend
      2. Logging with MongoDB
      3. Time for action - logging page visits with MongoDB
        1. What just happened?
        2. Capped collections
          1. Sorting in natural order
          2. Updating and deleting documents in a capped collection
          3. Specifying the size of a regular collection
          4. Convert a regular collection to a capped one
        3. Pop Quiz - capped collection
      4. Extracting analytics data with MapReduce
      5. Time for action - finding total views and average response time per blog post
        1. What just happened?
        2. The map, reduce, and finalize functions
        3. Displaying the result
          1. Running MapReduce in real time versus running it in the background
        4. Have a go hero find out usage share of browsers for the site
      6. Real-time analytics using MongoDB
      7. Time for action - building a real-time page visit counter
        1. What just happened?
        2. Have a go hero get unique page visits in real time
      8. Summary
    12. 6. Using MongoDB with Relational Databases
      1. The motivation behind using MongoDB and an RDBMS together
        1. Potential use cases
      2. Defining the relational model
      3. Time for action - creating the database in MySQL
        1. What just happened?
      4. Caching aggregation results in MongoDB
      5. Time for action - storing the daily sales history of products in MongoDB
        1. What just happened
      6. Benefits of caching queries in MongoDB
        1. Storing results of expensive JOINs
        2. Have a go hero replacing Views with MongoDB
      7. Using MongoDB for data archiving
      8. Time for action - archiving old sales records in MongoDB
        1. What just happened?
        2. Challenges in archiving and migration
          1. Dealing with foreign key constraints
          2. Preserving data types
      9. Storing metadata in MongoDB
      10. Time for action - using MongoDB to store customer metadata
        1. What just happened?
      11. Problems with using MongoDB and RDBMS together
      12. Summary
    13. 7. Handling Large Files with GridFS
      1. What is GridFS?
        1. The rationale of GridFS
        2. The specification
        3. Advantages over the filesystem
        4. Pop Quiz - what is the maximum size of BSON objects?
      2. Storing files in GridFS
      3. Time for action - uploading images to GridFS
        1. What just happened?
        2. Looking under the hood
        3. Have a go hero perform multiple file uploads in GridFS
      4. Serving files from GridFS
      5. Time for action - serving images from GridFS
        1. What just happened?
        2. Updating metdata of a file
        3. Deleting files
        4. Have a go hero create an image gallery with GridFS
      6. Reading files in chunks
      7. Time for action - reading images in chunks
        1. What just happened?
      8. When should you not use GridFS
      9. Summary
    14. 8. Building Location-aware Web Applications with MongoDB and PHP
      1. A geolocation primer
        1. Methods to determine location
        2. Pop Quiz - locating a smartphone
      2. Detecting the location of a web page visitor
        1. The W3C Geolocation API
          1. Browsers that support geolocation
      3. Time for action - detecting location with W3C API
        1. What just happened?
        2. The Geolocation object
          1. The getCurrentPosition() method
        3. Drawing the map using the Google Maps API
      4. Geospatial indexing
      5. Time for action - creating geospatial indexes
        1. What just happened?
        2. Geospatial indexing Important things to know
      6. Performing location queries
      7. Time for action - finding restaurants near your location
        1. What just happened?
        2. The geoNear() command
        3. Bounded queries
      8. Geospatial haystack indexing
      9. Time for action - finding nearby restaurants that serve burgers
        1. What just happened?
      10. Summary
    15. 9. Improving Security and Performance
      1. Enhancing query performance using indexes
      2. Time for action - creating an index on a MongoDB collection
        1. What just happened?
        2. The _id index
        3. Unique indexes
        4. Compound keys indexes
        5. Indexing embedded document fields
        6. Indexing array fields
        7. Deleting indexes
        8. When indexing cannot be used
        9. Indexing guidelines
          1. Choose the keys wisely
          2. Keep an eye on the index size
          3. Avoid using low-selectivity single key indexes
          4. Be aware of indexing costs
          5. On a live database, run indexing in the background
        10. Pop Quiz - the indexing MCQ test
        11. Have a go hero implement search in the blogging application
      3. Optimizing queries
        1. Explaining queries using explain()
          1. Optimization rules
        2. Have a go hero compare outputs of explain() for indexed and non-indexed queries
        3. Using hint()
        4. Profiling queries
          1. Understanding the output
          2. Optimization rules
      4. Securing MongoDB
      5. Time for action - adding user authentication in MongoDB
        1. What just happened?
        2. Creating an admin user
        3. Creating regular user
        4. Viewing, changing, and deleting user accounts
        5. User authentication through PHP driver
        6. Have a go hero modify DBConnection class to add user authentication
        7. Filtering user input
        8. Running MongoDB server in a secure environment
      6. Ensuring data durability
        1. Journaling
          1. Performance
          2. Using fsync
        2. Replication
        3. Pop Quiz - flushing data to disk
      7. Summary
    16. 10. Easy MongoDB Administration with RockMongo and phpMoAdmin
      1. Administering MongoDB with RockMongo
      2. Time for action - installing RockMongo on your computer
        1. What just happened?
        2. Exploring data with RockMongo
          1. Querying
          2. Updating, deleting, and creating documents
        3. Importing and exporting data
        4. Viewing stats
        5. Miscellaneous
      3. Using phpMoAdmin to administer MongoDB
      4. Time for action - installing phpMoAdmin on your computer
        1. What just happened?
        2. Viewing databases and collections
        3. Querying documents
        4. Saving and deleting objects
        5. Importing and exporting data
        6. Viewing stats
        7. Other features
      5. RockMongo versus phpMoAdmin
        1. The verdict
      6. Summary
    17. A. Pop Quiz Answers
      1. Chapter 1, Getting Started with MongoDB
      2. Chapter 2, Building your First MongoDB Powered Web App
      3. Chapter 3, Building a Session Manager
      4. Chapter 4, Aggregation Queries
      5. Chapter 5, Web Analytics using MongoDB
      6. Chapter 7, Handling Large Files with GridFS
      7. Chapter 8, Building Location-aware Web Applications with MongoDB and PHP
      8. Chapter 9, Improving Security and Performance