Yii2 By Example

Book description

Develop complete web applications from scratch through practical examples and tips for beginners and more advanced users

About This Book

  • Improve your programming experience and become a full stack developer
  • Master real-life web applications, and create and manage four different projects
  • Step-by-step guidance to develop real-world web applications smoothly

Who This Book Is For

This book is for anyone who wants to discover and explore Yii Framework. Basic programming experience with PHP and object oriented programming is assumed.

What You Will Learn

  • Understand basic concepts, along with the installation and customization of Yii
  • Discover models, controllers, and views-concepts applied in a web context-and how they are employed in Yii
  • Use ActiveRecord to manipulate a database
  • Add access control to your web application through authentication and authorization
  • Install and customize an advanced template for multiple applications in the same project
  • Create a RESTful Web Service to allow remote access to data
  • Develop a console application to launch a command in the console as an automated task (cron job)
  • Make code reusable through widgets and components and localize text messages to make a multilanguage web app

In Detail

Yii is a high-performance PHP framework best for developing Web 2.0 applications. It provides fast, secure, and professional features to create robust projects, however, this rapid development requires the ability to organize common tasks together to build a complete application. It's all too easy to get confused; this is where this book comes in.

This book contains a series of practical project examples for developers starting from scratch. Each section contains the most relevant theories for every topic as you walk through developing each project, focusing on key aspects that commonly confuse users.

The book starts with all the framework's basic concepts, such as controllers and views, to introduce you to Yii and creating your first application, a simple news reader. You will be learn to configure URL rules to make a pretty URL, essential for search engine optimization. Next, you will walk through Model and ActiveRecord, key concepts in database interaction.

The second application you will develop is a reservation system that allows you to manage rooms, customers, and reservations. For this, you will use database connection through SQL and ActiveRecord. More complex than the first one, this application will introduce you to the advanced template of Yii 2, splitting the app into two parts: a frontend for all visitors and a backend for the admin. Finally, you will move on to the last two applications: one that allows connections from remote clients, through RESTful components of Yii 2, and another that creates and organizes automatic tasks using the console application structure of Yii 2.

Style and approach

This is a step-by-step guide with each topic introduced in the context of real-world applications, highlighting common cases where users may encounter difficulties.

Table of contents

  1. Yii2 By Example
    1. Table of Contents
    2. Yii2 By Example
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. Starting with Yii2
      1. Requirements and tools
      2. Installing Yii2 with Composer
      3. Application structure
      4. Application properties
        1. Common application components
        2. Handling application events
        3. The MVC pattern in Yii2
      5. Naming convention
        1. Configuring the debug toolbar
        2. Using the logger
        3. Example – Hello world from scratch with the Yii basic template and bootstrap template
      6. Summary
    9. 2. Creating a Simple News Reader
      1. Creating Controller and Action
      2. Creating a view to display a news list
      3. How the controller sends data to view
        1. Example – create a controller to display the static news items list and details using the bootstrap template
      4. Splitting the common view content into reusable views
        1. Example – render partial in view
      5. Creating static pages
        1. Example – add a contact page
      6. Sharing data between views and layout
        1. Example – change the layout background based on a URL parameter
      7. Layout with dynamic block
        1. Example – add a dynamic box to display advertising info
      8. Using multiple layouts
        1. Example – using different layouts to create responsive and nonresponsive content layout for the same view
      9. Summary
    10. 3. Making Pretty URLs
      1. Using pretty URLs
      2. Custom URL rules
        1. Example – list news items by year or category
      3. Default parameters in rules
        1. Example – the index page to display the links list
      4. The complete URL rule parameters
      5. The URL pattern to support the multilanguage view
      6. Creating the rule class
      7. Summary
    11. 4. Creating a Room through Forms
      1. Creating a Model
        1. Example – a Model to store room data
      2. Using ActiveForm
        1. Example – creating a new room from the HTML form
      3. Format date, time, and numbers
      4. Uploading files
        1. Example – uploading an image of a room
      5. Summary
    12. 5. Developing a Reservation System
      1. Configuring a DB connection
        1. Example – creating rooms, customers, and reservations tables
        2. Example – test connection and executing the SQL query
      2. Using Gii to create room, customer, and reservation models
      3. Using ActiveRecord to manipulate data
        1. Example – query rooms list with ActiveRecord
      4. Working with relationships
        1. Example – using a relationship to connect rooms, reservations, and customers
      5. How to save a model from a form
        1. Example – creating and updating a room from a form
      6. Setting up the GMT time zone
      7. Using multiple database connections
        1. Example – configuring a second DB connection to export data to a local SQLite DB
      8. Summary
    13. 6. Using a Grid for Data and Relations
      1. Introduction
      2. DataProvider for grids
      3. Using a grid
      4. Custom columns in a grid
        1. Example – displaying a reservations list by clicking on a customer grid row
      5. Filters in GridView
      6. Displaying and filtering ActiveRecord relational data in a grid's column
      7. A summarized footer row in a grid
        1. Example – extending GridView to customize the footer row in a grid
      8. Multiple grids on one page
        1. Example: managing the reservations and rooms grids in the same view
      9. Summary
    14. 7. Working on the User Interface
      1. Using Gii to generate CRUD
        1. Example – using CRUD to manage rooms, reservations, and customers using Gii
      2. Customize JavaScript and CSS
        1. Example – using JavaScript and CSS to display advertising columns that disappear if not enough space is available
      3. Using AJAX
        1. Example – reservation details loaded from the customers' drop-down lists
      4. Using the Bootstrap widget
        1. Example: using datepicker
      5. Multiple models in the same view
        1. Example – saving multiple customers at the same time
      6. Saving linked models in the same view
        1. Example – creating a customer and reservation in the same view
      7. Summary
    15. 8. Log in to the App
      1. Creating a user login
        1. Example – a login form to access
      2. Configuring user authorization
        1. Example – creating an ACF to authorize the users
        2. RBAC
          1. Example – configuring RBAC to set permissions for users
      3. Mixing ACF and RBAC
        1. Example – managing users' roles to access rooms, reservations, and customers
      4. Summary
    16. 9. Frontend to Display Rooms to Everyone
      1. Using an advanced template to split frontend and backend
      2. Configuring an application using init
        1. Example – creating frontend for public access
      3. Sharing ActiveRecord models among applications
        1. Example – displaying available rooms in the frontend site
      4. Customizing a URL in the advanced template
        1. Example – using the advanced template in the same domain
      5. How to use the advanced template in the shared hosting
      6. Summary
    17. 10. Localize the App
      1. Setting the default language
      2. File-based translations
        1. Example – using file-based translation for the entire website
      3. Placeholders formatting
      4. DB-based translations
        1. Example – translating room descriptions using DB
      5. Summary
    18. 11. Creating an API for Use in a Mobile App
      1. Configuring a REST app in the advanced template
      2. Creating a controller
        1. Example – creating a controller to manage rooms
      3. Authentication
        1. Example – using authentication to get a customers list
      4. New controller action
        1. Example – getting a rooms list for a reservation
      5. Customizing authentication and response
        1. Example – status response node in data received
      6. Other forms of export – RSS
        1. Example – creating an RSS with a list of available rooms
      7. Summary
    19. 12. Create a Console Application to Automate the Periodic Task
      1. Interacting with console applications
      2. Creating a console controller
        1. Example – setting an alarm flag for expired reservation
      3. Formatting the output from the console
      4. Implementing and executing cron jobs
        1. Example – sending an e-mail with new reservations of the day
      5. Summary
    20. 13. Final Refactoring
      1. Creating widgets
        1. Example – creating a widget with a carousel
      2. Creating components
        1. Example – creating a component that creates a backup of the MySQL database and sends an e-mail to the administrator
      3. Creating modules
      4. Generating an API documentation
        1. Example – using an API documentation to generate a doc of app and services
      5. Summary
    21. Index

Product information

  • Title: Yii2 By Example
  • Author(s): Fabrizio Caldarelli
  • Release date: September 2015
  • Publisher(s): Packt Publishing
  • ISBN: 9781785287411