Chapter 1. Getting Started

Let's see how we can set up a website with Yii 2 from scratch and with a minimum amount of effort. The goal is to learn about the installation procedure of the Yii application boilerplates offered by developers and the starting set of features provided in them.

A basic application

The most basic and straightforward way to get started with Yii 2 is to use the application boilerplate published by the Yii 2 team on their GitHub repository (https://github.com/yiisoft/yii2) and available through the Composer tool. In the prior versions of Yii, you had to manually download and extract the archive with the framework contents. While you can still do the same in Yii 2, this version is carefully crafted so that it's especially simple to install it using the Composer tool.

Installation of a basic application template

Find a suitable directory in your hard drive and fetch the Composer PHP archive (PHAR) into it in any way suitable for you, for example, using the following command:

$ curl -sS https://getcomposer.org/installer | php

Now run the following command to create a subdirectory called basic, and fill it with the basic application template:

$ php composer.phar create-project --prefer-dist --stability=dev 
yiisoft/yii2-app-basic basic

Note

Please note that Yii 2 specifies several system-wide dependencies for itself. You probably will need to consult the composer.json file inside their GitHub repository to learn about them beforehand (https://github.com/yiisoft/yii2). But in any case, Composer will tell you what you need to install to make Yii 2 workable. Yii 2 gets new updates quite often and its requirements are a moving target.

It's a line split into two lines for readability, with a slash denoting the overflow of the command line to the next line of text. Shell interpreters on Unix-like systems should understand this convention, so you can probably just copy and paste the code verbatim and it'll be executed correctly. You better check the documentation of Composer for the meaning of the beginning of the preceding command, but the part relevant to us is yiisoft/yii2-app-basic basic, which means "copy contents of the repo published at https://github.com/yiisoft/yii2-app-basic to our local folder named basic." The command will install the project skeleton in the form of a set of predefined folders, and among them the vendor subdirectory, which contains quite a lot of other Composer packages. Inside the basic folder will be your application root.

After Composer finishes installing the required packages, you can just issue the following command:

$ php -S localhost:8000 –t basic/web

Here, 8000 is the port number, which you can change to anything you want. This will launch the web server built into PHP.

Note

This is not the preferred setup for PHP-based web applications, of course. The built-in web server was used only as a "smoke test" to verify that everything in general works. It is suitable only for local development without a heavy load. The next chapter will deal with the real-world deployment of a Yii-based web application.

Point your web browser at the http://localhost:8000/ URL. You should see the welcome page for the Yii 2 application, which means that you're done setting things up.

Installation of a basic application template

Specifics of the basic application template

You can get a comprehensive overview of the various folders inside the basic template by reading the README file provided with the template (https://github.com/yiisoft/yii2/blob/master/apps/basic/README.md), or by reading the global Yii 2 documentation page describing basic applications (http://www.yiiframework.com/doc-2.0/guide-start-installation.html).

The most important thing you should understand is that the publicly available web root directory is just one folder in the overall code base. It's the web directory for our basic application. Every other folder is outside the web root directory, that is, out of reach for the web server.

As you have already seen in the installation description, given that you have PHP and, optionally, curl, this code base is ready to use right from the start; no specific environment is needed to be set up. All the dependencies are managed through the Composer tool.

A three-tier automatic testing harness is already set up in the basic template. It contains acceptance, functional, and unit tests covering most of the functionalities. The tests already included in the template are useful as examples that show how to utilize the testing framework used, which is Codeception (http://codeception.com/).

The template can be really useful to you if all you need is something like a news feed feature or a web tool spanning a couple of pages. However, the absence of separation by subsystems, such as the administrative backend and public frontend, will start to hinder you on a larger web application; probably, an application with more than just 10 unique routes.

Note that by reading the project dependencies in the composer.json file, Yii 2 has several important parts separated out as pluggable packages, and they are already included in your code base by Composer. These packages are listed as follows:

  • Gii, the code generator, which we will discuss in detail in Chapter 3, Automatically Generating the CRUD Code
  • The debug console that is already enabled on the basic application template
  • A wrapper around the Codeception testing framework
  • A wrapper around the SwiftMailer library (http://swiftmailer.org/), which can be found at https://github.com/yiisoft/yii2-swiftmailer
  • The Twitter Bootstrap UI library packaged as a Yii 2 asset bundle (it's practically ubiquitous nowadays, but here's the link anyway: http://getbootstrap.com/)

The first three are set up so that you get them only when you are developing the application, since they are useless and even harmful in the production environment. Most probably, you'll need all of these on any serious project though.

Note

A short overview of the basic application installation:

$ curl -sS https://getcomposer.org/installer | php
$ php composer.phar create-project --prefer-dist --stability=dev yiisoft/yii2-app-basic basic
$ php -S localhost:8000 -t basic/web
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.145.82