
Yii2 By Example Develop complete web applications from scratch through practical examples and tips for beginners and more advanced users Fabrizio Caldarelli BIRMINGHAM - MUMBAI Yii2 By Example Copyright © 2015 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 author, 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: September 2015 Production reference: 1230915 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78528-741-1 www.packtpub.com Credits Author Project Coordinator Fabrizio Caldarelli Mary Alex Reviewers Proofreader Tristan Bendixen Safis Editing Samuel Liew Indexer Acquisition Editor Tejal Soni Vivek Anantharaman Production Coordinator Content Development Editor Manu Joseph Anand Singh Cover Work Technical Editor Manu Joseph Vivek Arora Copy Editors Ameesha Smith-Green Laxmi Subramanian About the Author Fabrizio Caldarelli is an Italian programmer who started his professional career in his youth by programming with desktop-oriented languages, the first being Visual Basic. From the year 2000 onward, he spent 5 years developing software to manage radio broadcasts. During that period, he studied C#.NET to make porting of all software versus this new platform. During the same period, he learned web programming, HTML, and ASP, and in 2003, he began to develop software using PHP as the default programming language for web pages. During those years, he collaborated as a teacher for PHP programming courses with http://www.html.it/, an important online reference for developers in Italy. In 2008, he added new skills to his experience by starting to develop mobile projects for Nokia devices with Symbian C++, and a few years later, he started working on projects for iOS, Android, and naturally Windows phone. After many PHP-based web projects, in late 2012, he moved on to the Yii framework as his primary framework for developing web applications. Since then, he has built many important projects based on Yii 1 and later on Yii 2, day by day discovering the powerful improvement that Yii provides to getting work done. Now he lives in Sacrofano, a small town near Rome, with his wife, Serena. I want to thank Erika Accili for supporting me during the writing and organization of this book. I also want to thank my wife, Serena, for sustaining me during all the work, and for the rest of her life indeed! About the Reviewers Tristan Bendixen is currently pursuing a master's degree as a software engineer, having been passionate about programming for most of his life. He has worked as a developer on diverse projects, ranging from commercial and corporate websites to mobile phone apps and desktop applications. He continues to work as a software developer alongside his studies, on paid projects, as well as some open source ones, which he helps with when time permits. I would like to thank my beloved mother and younger brother for their love and support in my constant endeavors to become a better developer, and my friends at Aalborg University for being awesome sparring partners on projects and classes alike. Samuel Liew is a full-stack web developer who enjoys producing solutions with interesting and challenging requirements. He has experience of developing a diverse range of websites, such as governmental sites, public utilities, real estate, investor relations, contests, touchscreen kiosks, iPad feedback apps, blogs and magazines, and media news. He has also been involved with creating two proprietary content management systems using C#.NET/MongoDB and PHP/Yii/MySQL. His latest accomplishment is the development of a microstock photography website (http://vivistock.com) using the Yii Framework, which involves e-commerce transactions and implements heavy business logic. www.PacktPub.com Support files, eBooks, discount offers, and more For support files and downloads related to your book, please visit www.PacktPub.com. 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.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.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. TM https://www2.packtpub.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 Free access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access. Table of Contents Preface vii Chapter 1: Starting with Yii2 1 Requirements and tools 2 Installing Yii2 with Composer 2 Application structure 3 Application properties 6 Common application components 7 Handling application events 9 The MVC pattern in Yii2 10 Naming convention 10 Configuring the debug toolbar 11 Using the logger 14 Example – Hello world from scratch with the Yii basic template and bootstrap template 14 Summary 17 Chapter 2: Creating a Simple News Reader 19 Creating Controller and Action 20 Creating a view to display a news list 23 How the controller sends data to view 24 Example – create a controller to display the static news items list and details using the bootstrap template 25 Splitting the common view content into reusable views 29 Example – render partial in view 30 Creating static pages 31 Example – add a contact page 32 Sharing data between views and layout 34 Example – change the layout background based on a URL parameter 35 [ i ] Table of Contents Layout with dynamic block 36 Example – add a dynamic box to display advertising info 37 Using multiple layouts 38 Example – using different layouts to create responsive and nonresponsive content layout for the same view 39 Summary 40 Chapter 3: Making Pretty URLs 41 Using pretty URLs 41 Custom URL rules 43 Example – list news items by year or category 44 Default parameters in rules 47 Example – the index page to display the links list 48 The complete URL rule parameters 49 The URL pattern to support the multilanguage view 50 Creating the rule class 52 Summary 55 Chapter 4: Creating a Room through Forms 57 Creating a Model 57 Example – a Model to store room data 58 Using ActiveForm 61 Example – creating a new room from the HTML form 62 Format date, time, and numbers 65 Uploading files 67 Example – uploading an image of a room 67 Summary 71 Chapter 5: Developing a Reservation System 73 Configuring a DB connection 74 Example – creating rooms, customers, and reservations tables 76 Example – test connection and executing the SQL query 78 Using Gii to create room, customer, and reservation models 81 Using ActiveRecord to manipulate data 88 Example – query rooms list with ActiveRecord 91 Working with relationships 96 Example – using a relationship to connect rooms, reservations, and customers 103 How to save a model from a form 109 Example – creating and updating a room from a form 112 [ ii ] Table of Contents Setting up the GMT time zone 118 Using multiple database connections 120 Example – configuring a second DB connection to export data to a local SQLite DB 121 Summary 125 Chapter 6: Using a Grid for Data and Relations 127 Introduction 128 DataProvider for grids 128 Using a grid 130 Custom columns in a grid 131 Example – displaying a reservations list by clicking on a customer grid row 131 Filters in GridView 137 Displaying and filtering ActiveRecord relational data in a grid's column 140 A summarized footer row in a grid 144 Example – extending GridView to customize the footer row in a grid 147 Multiple grids on one page 148 Example: managing the reservations and rooms grids in the same view 149 Summary 152 Chapter 7: Working on the User Interface 153 Using Gii to generate CRUD 154 Example – using CRUD to manage rooms, reservations, and customers using Gii 155 Customize JavaScript and CSS 158 Example – using JavaScript and CSS to display advertising columns that disappear if not enough space is available 160 Using AJAX 163 Example – reservation details loaded from the customers' drop-down lists 165 Using the Bootstrap widget 169 Example: using datepicker 170 Multiple models in the same view 175 Example – saving multiple customers at the same time 176 Saving linked models in the same view 179 Example – creating
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages344 Page
-
File Size-