Chapter 9. Frontend to Display Rooms to Everyone

This chapter will cover topics about using templates to have multiple apps in the same context.

Yii, indeed, allows you to have an advanced installation able to contain multiple instances of an Yii application. Therefore, every folder in the project is actually a new Yii application.

We will see how to install and configure the project, share data between them, and finally customize the URL to make them pretty for the search engine.

We will cover the following topics in this chapter:

  • Using an advanced template to split frontend and backend
  • Configuring an application using init
    • Example – creating frontend for public access
  • Sharing ActiveRecord models among applications
    • Example – displaying available rooms in frontend site
  • Customizing a URL in an advanced template
    • Example – using advanced templates in the same domain
  • How to use advanced templates in shared hosting

Using an advanced template to split frontend and backend

Until now, we have seen simple applications with only one single entry point to access. However, a single entry point isn't enough for more general applications. In advanced web applications, in fact, we have not just a single entry point but often three: frontend, backend, and a common area used as shared zone for every entry point.

The frontend entry point is a public access that is available to all users without restrictions.

On the other hand, the backend entry point is a restricted access available only for authenticated users that have administration roles for managing content in the web application.

Finally, the common entry point is used to share data between entry points.

Think about a reservation system, where frontend is the website displaying room availability and prices, while backend is the administration area, where operators can manage rooms.

In the same way, another example of frontend and backend could be a newspaper website that comprises a frontend area with news publically visible to all users, and a backend area where journalists can insert news.

Now that we know the differences between frontend and backend and their aim, we will create an advanced Yii application.

The steps to install an advanced template of the Yii application are similar to the ones to install basic templates.

Note

It is highly recommended, at this point, to have a console access the host, where we can put files.

Locate the web hosting document root folder in the web hosting. Starting from it, we will launch commands to create the advanced application in a new subfolder named yiiadv, which stands for Yii installation with the advanced template.

We will install the Yii advanced template using Composer as it is the most recommended way. If we have not installed Composer as the global application yet, we can install it now in the yiiadv folder.

The following are the instructions to install Yii advanced template starting from document root folder:

$ curl -sS https://getcomposer.org/installer | php
$ php composer.phar global require "fxp/composer-asset-plugin:~1.0.0"
$ php composer.phar create-project --prefer-dist yiisoft/yii2-app-advanced yiiadv

By opening the yiiadv subfolder, we can see some new folders beside the basic template, which are as follows:

  • backend: This folder is the entry point for the backend application of the project
  • common: This folder is the entry point for the application containing common data for the other applications in the project
  • console: This folder is the entry point for the console application of the project
  • frontend: This folder is the entry point for the frontend application of the project

This structure is the result of the experience on developing the web application. Backend and frontend entry points have been formerly discussed; the common entry point is an area where to put data (common models, components, and so on) shared among all the other applications in the project.

Note

Every application in the project (backend, frontend, common, and console) is considered as a single namespace in the web application. So, when we refer to RoomsController in the frontend, the complete class namespace will be frontend/controllers/RoomsController.

This installation is still raw and requires an initialization using the init command. However, if we try to open any of these applications, we can recognize the same basic template structure with assets, config, controllers, models, runtime, views, and web subfolders. So, a basic template application can be considered the only unique application in an advanced template one.

Finally, in the advanced template properties, every application starting point is always in web/index.php. For example, for the frontend application, the starting point is frontend/web/index.php.

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

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