How to set paths (aliases) when directory structure changed?

Hi. Thank you for releasing Yii3.

I am used to have separated public and private files into two directories: public and private.

I made fresh Yii3 application installation:
cd /path/to/directory
composer create-project yiisoft/app private
cd private
mv public ..

So I have:

> private
>> config
>> src
>> vendor
>> yii
>> (...)
> public
>> index.php
>> (...)

Then I changed config/common/aliases.php to correspond new directory structure.

From this:

return [
    '@root' => dirname(__DIR__, 2),
    '@src' => '@root/src',
    '@assets' => '@root/public/assets',
    '@assetsUrl' => '@baseUrl/assets',
    '@assetsSource' => '@root/assets',
    '@baseUrl' => '/',
    '@public' => '@root/public',
    '@runtime' => '@root/runtime',
    '@vendor' => '@root/vendor',
];

To this:

return [
    '@root' => dirname(__DIR__, 3),
    '@src' => '@root/private/src',
    '@assets' => '@root/public/assets',
    '@assetsUrl' => '@baseUrl/assets',
    '@assetsSource' => '@root/private/assets',
    '@baseUrl' => '/',
    '@public' => '@root/public',
    '@runtime' => '@root/private/runtime',
    '@vendor' => '@root/private/vendor',
];

As suggested in common/configuration.php, I ran command to update merge plan:
composer yii-config-rebuild

Then I started PHP built in server:
APP_ENV=dev ./yii serve --port=8080

And got warning and error:

[WARNING] Default router "public/index.php" does not exist. Serving without router. URLs with dots may fail.
[ERROR] Document root "/path/to/directory/private/public" does not exist.

What am I doing wrong? How to correctly set paths to communicate private and public directories with each other?

You’ll need to add the options --docroot and --router to .the yii serve command in order to find index.php in the public dir.

And you’l also need to change the docroot in the index,php fiile
$root = dirname(__DIR__) . '/private';

(It looks like it will work.)

Did it work with your aliases?

I actually changed them like this (because I could’nt navigate another level upwards):

return [
    '@root' => dirname(__DIR__, 2),
    '@src' => '@root/src',
    '@assets' => '@root/../public/assets',
    '@assetsUrl' => '@baseUrl/assets',
    '@assetsSource' => '@root/assets',
    '@baseUrl' => '/',
    '@public' => '@root/../public',
    '@runtime' => '@root/runtime',
    '@vendor' => '@root/vendor',
];

Thank you for you answers. I have messed up my local environment, so I have to try it on my shared webhosting.

I changed config/common/aliases.php as you proposed.

I changed $root in index.php:

$root = dirname(__DIR__);
require_once $root . '/private/src/autoload.php';

I enabled showing errors and after accessing index.php from web browser I have got an error:

Fatal error: Uncaught RuntimeException: APP_ENV environment variable is empty. Valid values are "dev", "test", "prod". in /path/to/directory/private/src/Environment.php:92 Stack trace: #0 /path/to/directory/private/src/Environment.php(28): App\Environment::setEnvironment() #1 /path/to/directory/private/src/autoload.php(9): App\Environment::prepare() #2 /path/to/directory/public/index.php(19): require_once('...') #3 {main} thrown in path/to/directory/private/src/Environment.php on line 92

It looks like paths are OK, but I do not know how to set up an environment. I have putted .env with “APP_ENV=prod” into private directory, but nothing changed.

I started yii serve from the app/private directory with the command line from your first post plus more options. When started without APP_ENV=dev I got a similar error about missing environment variable,

I’m not familiar with that syntax on the command line, have to look it up later. I don’t know why adding APP_ENV to .env didn’t work.

I reinstalled my computer and tried it again the same as in my first comment, but with your aliases.php and index.php suggestions. I have got the same result:

[WARNING] Default router "public/index.php" does not exist. Serving without router. URLs with dots may fail.
[ERROR] Document root "/path/to/directory/private/public" does not exist.

Add options to the command line

suse@suse:~> APP_ENV=dev ./yii serve --port=8080  --docroot=../public --router=../public/index.php

I actually used this command line to allow remote access

suse@suse:~> APP_ENV=dev ./yii serve --port=8000  --docroot=../public --router=../public/index.php 0.0.0.0