Contents
Yii
A fast framework with Active Record, the Gii code generator and ready-made grid and form widgets.
1.Getting started
- Meet YiiWhat Yii 2 is, how to install a project, which folders it has and how to make the first page.
- Entry script and configurationHow a request gets into the application: the web/index.php file, the config/web.php settings and the Yii::$app components.
- Controllers and actionsWhere Yii controllers live, how to name the class and the action, how an action receives parameters from the URL.
- Routing and pretty URLsHow to enable URLs without index.php, describe rules in urlManager and build links with Url::to.
- Requests and responsesHow to read request data through Yii::$app->request and return a redirect, JSON or a 404 error.
- ViewsWhere Yii views live, how to render a view from a controller, pass data to it and show the data safely.
- Layouts and asset bundlesThe common frame of pages in views/layouts/main.php and adding styles and scripts through the AppAsset class.
2.Database
- Database connectionHow to create a database, put the connection settings into config/db.php and run the first query.
- MigrationsHow to create a table in code: the migrate/create command, the safeUp and safeDown methods, column types, running and rolling back.
- Active RecordA model class for a table: where it lives and how to find, create, change and delete a record.
- Building queriesWhere conditions, sorting and limiting, the Query builder, parameters in SQL and transactions.
- Relations between modelsThe hasOne and hasMany relations in Yii models, accessing related records and eager loading with the with method.
- Pagination and sortingThe Pagination and Sort classes, the LinkPager widget and the ActiveDataProvider data provider.
- The Gii code generatorHow to open Gii, generate a model from a table and ready-made pages for managing records.
3.Forms and data
- Models and validation rulesThe rules method in a Yii model: built-in validators, custom messages, your own rule and showing errors.
- Forms with ActiveFormHow to build a form from a model with the ActiveForm widget, accept it in a controller and show errors.
- Data widgetsThe ready-made GridView, ListView and DetailView widgets: a sortable table, a list of cards and a single record page.
- Sessions and flash messagesHow to keep visitor data between requests with Yii::$app->session and show a one-time message.
- File uploadsHow to accept a file from a form: the UploadedFile class, the file rule, the fileInput field and saving to web/uploads.
- Behaviors and filtersThe behaviors method: TimestampBehavior for dates in a model and VerbFilter for limiting request methods in a controller.
4.Users and access
- AuthenticationUser login and logout: the IdentityInterface interface, a User model with a table, the password hash and the Yii::$app->user component.
- Authorization: the AccessControl filterHow to close controller actions to guests with the AccessControl filter and check that a post is edited by its author.
- Roles and permissions (RBAC)RBAC basics in Yii: the authManager component, creating roles and permissions, checking with Yii::$app->user->can.
5.Growing the project
- REST APIHow to serve posts as JSON: the ActiveController class, the UrlRule rule and choosing the response fields.
- Console commandsHow to write your own command for php yii: the commands folder, arguments and options, running on a schedule.
- CachingThe Yii::$app->cache component: how to store the result of a heavy query, cache database queries and a part of a view.
- Sending mailThe Yii::$app->mailer component: configuration, an email from a view in the mail folder and checking emails without sending.
- Translations and languagesTranslating the interface with Yii::t and files in the messages folder, inserting values and printing dates by language rules.
- Logging and error handlingWriting messages with Yii::error and Yii::info, configuring the log component and your own error page.
- DeploymentHow to move a Yii project to a server: prod mode, the YII_DEBUG and YII_ENV constants, the web root and folder permissions.