Book Image

Yii2 Application Development Cookbook - Third Edition

By : Sergey Ivanov, Andrew Bogdanov, Dmitry Eliseev
Book Image

Yii2 Application Development Cookbook - Third Edition

By: Sergey Ivanov, Andrew Bogdanov, Dmitry Eliseev

Overview of this book

Yii is a free, open source web application development framework written in PHP5 that promotes clean DRY design and encourages rapid development. It works to streamline your application development time and helps to ensure an extremely efficient, extensible, and maintainable end product. Being extremely performance optimized, Yii is a perfect choice for any size project. However, it has been built with sophisticated, enterprise applications in mind. You have full control over the configuration from head-to-toe (presentation-to-persistence) to conform to your enterprise development guidelines. It comes packaged with tools to help test and debug your application, and has clear and comprehensive documentation. This book is a collection of Yii2 recipes. Each recipe is represented as a full and independent item, which showcases solutions from real web-applications. So you can easily reproduce them in your environment and learn Yii2 fast and without tears. All recipes are explained with step-by-step code examples and clear screenshots. Yii2 is like a suit that looks great off the rack, but is also very easy to tailor to fit your needs. Virtually every component of the framework is extensible. This book will show how to use official extensions, extend any component, or write a new one. This book will help you create modern web applications quickly, and make sure they perform well using examples and business logic from real life. You will deal with the Yii command line, migrations, and assets. You will learn about role-based access, security, and deployment. We’ll show you how to easily get started, configure your environment, and be ready to write web applications efficiently and quickly.
Table of Contents (19 chapters)
Yii2 Application Development Cookbook Third Edition
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Preface
Index

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 needle 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:

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:

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