Toma Tomov
Toma Tomov

Reputation: 1654

Yii2 admin and frontend separated

I red a lot of posts but still can't realize how should i separate them. I am talking about : www.mysite.com(frontend) and www.mysite.com/admin(backend). Tried also the way the documentation says it like

<VirtualHost *:80>
        ServerName frontend.dev
        DocumentRoot "/path/to/yii-application/frontend/web/"

        <Directory "/path/to/yii-application/frontend/web/">
            # use mod_rewrite for pretty URL support
            RewriteEngine on
            # If a directory or a file exists, use the request directly
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            # Otherwise forward the request to index.php
            RewriteRule . index.php

            # use index.php as index file
            DirectoryIndex index.php

            # ...other settings...
            # Apache 2.4
            Require all granted

            ## Apache 2.2
            # Order allow,deny
            # Allow from all
        </Directory>
    </VirtualHost>

    <VirtualHost *:80>
        ServerName backend.dev
        DocumentRoot "/path/to/yii-application/backend/web/"

        <Directory "/path/to/yii-application/backend/web/">
            # use mod_rewrite for pretty URL support
            RewriteEngine on
            # If a directory or a file exists, use the request directly
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            # Otherwise forward the request to index.php
            RewriteRule . index.php

            # use index.php as index file
            DirectoryIndex index.php

            # ...other settings...
            # Apache 2.4
            Require all granted

            ## Apache 2.2
            # Order allow,deny
            # Allow from all
        </Directory>
    </VirtualHost>

but from all i red i think it is not the right way. Bagging you for help i am struggling from few hours already without result. Thank you in advance!

Upvotes: 0

Views: 186

Answers (1)

Lakhan Singh
Lakhan Singh

Reputation: 69

No need to write .htaccess , You can achieve this by changing your location of index.php from "frontend/web/index.php" to "/frontend/index.php" and code for index.php should.

<?php
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__ . '/config/bootstrap.php');

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

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

Same changes you can do in admin folder. Remember you need to rename your backend folder as "admin". Also create a "Assets" folder parallel to index.php file . In our case there is already "assets" folder inside "frontend".So no need to create.

Upvotes: 2

Related Questions