Ben
Ben

Reputation: 99

Yii2 advanced template backend and frontend on same domain

I really admire that Yii2 advanced template divides backend and frontend to seperate directories, keeps it structurized, but what I do not get, is how should deploy it to a server. On the end of the day I will have to upload it to an apache server, and frontend will have to be accessed on http://domain.com/, and backend will have to be on something like http://domain.com/admin/. Server is based on apache.

How this chould be achieved?

Thank you!

Upvotes: 1

Views: 3999

Answers (3)

user3410311
user3410311

Reputation: 622

Copy all files to the root folder of site. Create admin folder in the root of site. Copy content of frontend/web to the site root and the content of backend/web to the siteroot/admin folder. And change the content of root/index.php to:

defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

require(__DIR__ . '/vendor/autoload.php');
require(__DIR__ . '/vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/common/config/bootstrap.php');
require(__DIR__ . '/frontend/config/bootstrap.php');

$config = yii\helpers\ArrayHelper::merge(
    require(__DIR__ . '/common/config/main.php'),
    require(__DIR__ . '/common/config/main-local.php'),
    require(__DIR__ . '/frontend/config/main.php'),
    require(__DIR__ . '/frontend/config/main-local.php')
);

(new yii\web\Application($config))->run();

And change the content of root/admin/index.php to:

defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/../common/config/bootstrap.php');
require(__DIR__ . '/../backend/config/bootstrap.php');


$config = yii\helpers\ArrayHelper::merge(
    require(__DIR__ . '/../common/config/main.php'),
    require(__DIR__ . '/../common/config/main-local.php'),
    require(__DIR__ . '/../backend/config/main.php'),
    require(__DIR__ . '/../backend/config/main-local.php')
);

$application = new yii\web\Application($config);
$application->run();

Upvotes: 1

CyberPunkCodes
CyberPunkCodes

Reputation: 3777

In the root of your site, you will need to place an .htaccess file. If the URL contains "admin", you will redirect to the physical and real path to backend. Else, use the physical and real path to frontend. Also, you can remove the "/web/" from the URL, by placing another .htaccess in the directory of both frontend and backend. So root htaccess will redirect to frontend or backend directories, which will then pass to web. It is a daisy chain method, but it does work.

You could also use symlinks or setup virtual hosts (vhosts). Honestly, the vhost method would be the best case. However, this gets the job done as well. Unless you have a lot of traffic, it shouldn't affect performance too much.

root of your site's .htaccess:

RewriteEngine on
RewriteRule ^admin/(.*)$ backend/$1 [L]
RewriteRule ^(.*)$ frontend/$1 [L]

I use the first rule to achieve site.com/admin -> maps to site.com/backend.. Then backend has htaccess that maps to web. I just added the 2nd rule for you, I haven't tested it. So if url path is "admin", pass to backend, else everything else gets passed to frontend.

.htaccess inside both "frontend" and "backend" directories (hides web from url):

Options -Indexes

RewriteEngine on
RewriteRule ^(.*)$ web/$1 [L]

.htaccess inside both "frontend/web" and "backend/web" directories (as per pretty url):

RewriteEngine on

# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Otherwise forward it to index.php
RewriteRule . index.php

In backend/config/main.php and frontend/config/main.php , add this:

'components' => [
    'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'enableStrictParsing' => false,
        'rules' => [
            // ...
        ],
    ],
],

That will enable pretty URLs.


I would place your entire project above the "public_html" directory, or whatever your public web root is. This way, none of the files are accessible. Then physically copy the contents of the web directories where you want them. ie: copy the contents of "frontend/web" into the root of your site. Then create a folder named "admin", and place the contents of "backend/web" in it. Then edit the index.php files to adjust the path to yii.


You should really look into cloud hosts like Heroku, CloudControl, and OpenShift. I personally like OpenShift. You then upload your changes using "git push", instead of messing with old-school FTP.

Upvotes: 0

Troy
Troy

Reputation: 141

To have access to the backend application in frontend you can use a symlink:

In a linux command shell

ln -s project_dir/backend/web project_dir/frontend/web/admin

Or on windows

mklink /J project_dir\frontend\web\admin project_dir\backend\web

Just replace 'project_dir' with the path to your project

Upvotes: 2

Related Questions