Connect Backend To Frontend

I created a news module (model, crud) in the advanced template backend.

Now I want to open my news items in the frontend, how do I do this?

Not sure if the Advanced template is ideal for your situation. The simplest solution for accessing a single set of application files from multiple applications is to move those files to the common/ folder.

The Advanced Template is designed for larger applications where the administrative features of a project, such as configuration, reporting, and statistics are created in the Backend application (backend/ folder hierarchy). The public content for a project, such as pages viewed by your end users are created in the Frontend application (frontend/ folder hierarchy). The Advanced Application Template also features common/ and vendor/ folders where you can put parts of the application accessible by both the Backend and Frontend applications. More about common/ and vendor/ later.

The Advanced Template concept allows the Frontend application to be accessed over the web from a domain (www.domain.com) and Backend from a separate domain (www.domain2.com) or subdomain (www.subdomain.domain.com).

The Backend and Frontend folder names are specified in the main.php configuration files for each application, so you can change the folder names if you want. The Advanced Template architecture is very extensible in the sense that you can add more applications across multiple subdomains or domains while sharing the same common/ and vendor/ code. For example, you can clone any number of Frontend applications to create more applications, modify each one as necessary, and access them over the web using subdomains.

The Frontend and Backend usually access the same database from a single database server, but in high-traffic or enterprise situations (e.g., where load balancing across multiple servers is required) could be configured to access separate databases stored across multiple database servers. As with the Basic Template and any other Template designed for Yii, multiple databases of the same database engine type or different database engine types can be accessed from each of these applications. For example, your application database tables might exist on a MYSQL database server and LDAP authentication services might exist on a MSSQL, Postgresql or Oracle server.

The common/ and vendor/ folders are intended to store code used by the Frontend, Backend, and any other applications you decide to add. The common/ folder is intended to store Yii2-specific and your application-specific extensions (i.e., those you created) in a common place. You can add more folders to common/ (e.g., a modules/ folder), including your News application if you have a mind to. The vendor/ folder should be the home of any third-party libraries or code you wish to share among applications. Earlier during Yii2 development, vendor/ was a place to manually create areas for storing third-party applications. With the advent of Composer, this chore has been automated. Composer also handles version and dependency updates (cases where one a specific version of a library might be dependent on one or more versions of other libraries). You can still manually add third-party libraries to vendor/ but library management is best handled by Composer.

All applications developed on Yii2, regardless of which template you might use, support a modules/ folder. The folder is usually create when you create your first module using Gii but you can create it manually. A modules folder can exist in backend/, frontend/, common/ or in the folder of any other application you decide to create. Modules help reduce the clutter and allow you to create more modular applications where you can mix and match modules for any project you might undertake.

Partially because of the addition of namespaces to PHP but more so because of the genius of Qiang, numerous templates can exist for Yii2. A search tour of ‘Yii2’ on Composer’s Packagist will reveal numerous alternate templates available for download on Git and other sites. Changing your application code to use an alternate template is usually a simple matter for updating your namespaces to reflect the template’s folder hierarchy.

Thanks for the detailed answer

I’m trying to make a small cms system

I need a backend and frontend, should I just move everything from backend to the common folder, or should I use the basic template?

Most likely doing something similar but not creating a CMS.

As I did, you might need something in between the Basic and Advanced templates. The part of the definitive guide regarding custom templates is still TBD last time I checked. An existing template may already exist under Packagist, but I rolled my own using a trial-and-error approach based on the Advanced Template. I needed a solution to access both the backend and frontend part of the application from shared server installations while preserving the concepts of the common/ and vendor/ folders for the widest possible deployment. I moved all of the folders under frontend/ and changed all of the namespaces and configuration files to reflect the changes. I moved the index.php and associated files under backend/web/ to a frontend/web/admin/ folder (i.e., Frontend is accessed via http://www.domain.com/index.php and Backend (now called Admin) is accessed via http://www.domain.com/admin/index.php). I changed the relative paths in the Admin’s index.php to reflect the change. This runs fine on localhost but I’m not ready to test on a production server yet (i.e., the folder structure is fine but the portal is not fully implemented yet). However, I don’t anticipate any major problems.