Book Image

Yii2 By Example

By : Fabrizio Caldarelli
Book Image

Yii2 By Example

By: Fabrizio Caldarelli

Overview of this book

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.
Table of Contents (20 chapters)
Yii2 By Example
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

This book covers the use of the Yii2 framework from scratch up to build a complete web application.

Yii is a high-performance PHP framework that is best for developing Web 2.0 applications that provide fast, secure, and professional features to rapidly 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 about the use of these technologies.

So, walking through practical examples will help you understand how these concepts must be used and realize a successful application.

What this book covers

Chapter 1, Starting with Yii2, provides basic knowledge about the Yii2 framework, starting from requirements to explain every single functionality. Then, we will use debugging and logging tools to trace our code and provides find errors. Finally, we will write our first project based on the basic template.

Chapter 2, Creating a Simple News Reader, creates our first controllers and relative views. We will explore static and dynamic views, learn how to render views in layout and pass data from controller to view, and then look at reusing views through partial views and blocks.

Chapter 3, Making Pretty URLs, shows how to implement pretty URLs, which is useful for search engine optimization. We will also create examples where we used custom rules to parse and create the URL. Finally, we will learn how to build more customized URL rules through Rule classes.

Chapter 4, Creating a Room through Forms, shows how to build a Model class from scratch and send data from view to controller using form, which is created using the Yii2 ActiveForm widget. We will also look at commonly used methods to format data and send files from the form.

Chapter 5, Developing a Reservation System, explains how to configure a database connection and execute SQL queries from scratch with DAO support for the framework. Next, we will find out how to use Gii and get to know about the advantages it has in creating models from the database table structure. Gii creates models that extend the ActiveRecord class, and, through its use, we will finally learn how to manipulate data.

Chapter 6, Using a Grid for Data and Relations, presents the GridView widget for displaying data, directly or relationed. A fundamental topic inside GridView is Data Provider, the way to provide data to GridView. We will learn how to get Data Provider from ActiveRecord, Array, or SQL, based on the available sources.

Chapter 7, Working on the User Interface, discusses the User Interface and how Yii helps us with its core functionalities.

Chapter 8, Log in to the App, shows how to apply user authentication and authorization to an app. The first step is to create authenticated access to the application. For this purpose, we will create a database table to manage users and associate it to the Yii user component through a user model that extends IdentityInterface.

Chapter 9, Frontend to Display Rooms to Everyone, explains how to use Yii to build a modern web project based on frontend and backend applications. We will find out the differences between basic and advanced templates, installing our first advanced project based on advanced templates.

Chapter 10, Localize the App, shows how to configure multiple languages in our app. We will discover that there are two storage options to handle internationalization: files and databases.

Chapter 11, Creating an API for Use in a Mobile App, creates an API for use in mobile apps through the use of powerful tools provided by Yii. We will adopt the approach of creating a new application in order to distribute RESTful Web Services, instead of mixing web and API controllers.

Chapter 12, Create a Console Application to Automate the Periodic Task, explains how to write a console application and allows you to discover the main differences between web and console apps.

Chapter 13, Final Refactoring, helps you to reuse code using widgets and components. We will create some practical examples on how to use them.

What you need for this book

The minimum requirements for this book are: a host on the Web, local or remote, based on the PHP 5.4 environment and having a MySQL database server installed (no specific version for it).

For writing code, it is enough to have a simple highlighted syntax editor, such as block notes, TextEdit, Notepad++, PSPad, Aptana, and so on.

Who this book is for

This book is intended for anyone who wants to discover the Yii Framework or master its practical concepts. Beginner-level users will find some introductive theory in every chapter that explains the topics treated, with a lot of code showing all their practical aspects. Advanced users will find many examples with special cases illustrated and common mistakes solved.

Basic programming experience with PHP and object-oriented programming is required.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Now, create the view with this content in basic/views/my-authentication/login.php."

A block of code is set as follows:

<?php
return [
    2 => [
        'operator',
    ],
    1 => [
        'admin',
    ],
];

Any command-line input or output is written as follows:

$ curl -H "Accept: application/json" http://hostname/yiiadv/api/web/test-rest/index

[{"id":1,"name":"Albert","surname":"Einstein"},{"id":2,"name":"Enzo","surname":"Ferrari"},{"id":4,"name":"Mario","surname":"Bros"}]

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.