Installing the framework

Yii2 is a modern PHP framework provided as a Composer package. In this recipe, we will install the framework via the Composer package manager and configure the database connection for our application.

Getting ready

First of all, install the Composer package manager on your system.

Note

Note: If you use the OpenServer application on Windows, than the composer command already exists in the OpenServer terminal.

In Mac or Linux download the installer from https://getcomposer.org/download/ and install it globally by using the following command:

sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer

In Windows without OpenServer download and run Composer-Setup.exe from the https://getcomposer.org/doc/00-intro.md page.

If you do not have administrative privileges on the system then as an alternative you can just download the https://getcomposer.org/composer.phar raw file and use the php composer.phar call instead of single the composer command.

After installation run in your terminal:

composer

Or (if you just download archive) its alternative:

php composer.phar

When the installation succeeds you will see the following response:

   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ / __ '__ / __ / __ / ___/ _ / ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.2.0 2016-07-18 11:27:19

Right now you can install any package from the https://packagist.org repository.

How to do it…

You can install basic or advanced application templates. In order to learn about the differences between the templates see the Application templates recipe.

Note

Note that during installation the Composer package manager gets a lot of information from the GitHub site. GitHub may limit requests for anonymous users. In this case Composer asks you to input your access token. You should just register the https://github.com site and generate a new token via the https://github.com/blog/1509-personal-api-tokens guide.

Installing a basic project template

Carry out the following steps for installing basic project template:

  1. As the first step open your terminal and install Bower-to-Composer adapter:
    composer global require "fxp/composer-asset-plugin:^1.2.0"
    

    It provides a simple way to load related non-PHP packages (JavaScript and CSS) from the Bower repository.

  2. Create a new application in the new basic directory:
    composer create-project --prefer-dist yiisoft/yii2-app-basic basic
    
  3. Check that your PHP contains the required extensions:
    cd basic
    php requirements.php
    

    Note

    Note: PHP in command-mode and in web-interface mode can use different php.ini files with different configurations and different extensions.

  4. Create a new database (if it is needed for your project) and configure it in the config/db.php file.
  5. Try to run application via the following console command:
    php yii serve
    
  6. Check in your browser that the application works by the http://localhost:8080 address:
    Installing a basic project template

For permanent working create a new host in your server (Apache, Nginx, and so on) and set the web directory as a document root of the host.

Installing advanced project template

Carry out the following steps for installing advanced project template:

  1. As the first step open your terminal install Bower-to-Composer adapter:
    composer global require "fxp/composer-asset-plugin:^1.2.0"
    

    It provides a simple way to load related non-PHP packages (JavaScript and CSS) from the Bower repository.

  2. Create a new application in the new basic directory:
    composer create-project --prefer-dist yiisoft/yii2-app-advanced advanced
    
  3. The new application does not contains local configuration files and index.php entry scripts yet. To generate the files just init a working environment:
    cd advanced
    php init
    

    During initialization select the Development environment.

  4. Check that your PHP contains the required extensions:
    php requirements.php
    

    Note

    Note: PHP in command-line mode and in web-interface mode can use different php.ini files with different configuration and different extensions.

  5. Create a new database and configure it in the generated common/config/main-local.php file.
  6. Apply the application migrations:
    php yii migrate
    

    This command will automatically create a user table in your database.

  7. Try to run a frontend application by the following console command:
    php yii serve --docroot=@frontend/web --port=8080
    

    Then run the backend in an other terminal window:

    php yii serve --docroot=@backend/web --port=8090
    
  8. Check in your browser that the application works via the http://localhost:8080 and http://localhost:8090 addresses:
    Installing advanced project template

Create two new hosts for backend and frontend application in your server (Apache, Nginx, and so on) and set the backend/web and frontend/web directories as document roots of the hosts.

How it works…

First of all, we installed the Composer package manager and the Bower asset plugin.

After we installed the application via the composer create-project command, the command creates a new empty directory, clones the source code of application template and loads all its inner dependencies (framework and other components) into the vendor subdirectory.

If needed, we will initialize application configuration and set up a new database.

We can check system requirements via running the requirements.php script in console or browser mode.

And after cloning of the code we can configure our own PHP server to work with the web directories as the server's document roots.

See also

..................Content has been hidden....................

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