简体   繁体   中英

Application file re-organization in Yii 2

I have set up new Yii2 project. Now I want to reorganize folder structure in two folders "public" and "app" (which actually represents the protected files).

All the code from framework goes in "app" folder. In "public" folder I have only one script named "index.php" from where I'm calling the app. The code there looks like the following (I have only modified only the paths from the original "index.php").

app/index.php

<?php

// comment out the following two lines when deployed to production
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

require(__DIR__ . '/../app/vendor/autoload.php');
require(__DIR__ . '/../app/vendor/yiisoft/yii2/Yii.php');

$config = require(__DIR__ . '/../app/config/web.php');

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

?>

This same approach was working in the Yii framework where we got different syntax of the commands. But now in Yii 2 I got the following issue:

在此输入图像描述

So it seems like all the paths are pointing to the right files (I have check that), but I still got the error. So what I'm missing here? Why the basePath still recognize the "public" folder?

UPDATE 1:

After following the comment from @marche and add the following code to the config file:

'components' => [
    ...
    'assetManager' => [
        'basePath' => 'my/path/to/assets',
    ],
    ...
],

I'm not getting the error with the screen anymore, but now the content is loaded without any CSS styling and without JS files (they are still pulling URL from the wrong places).

If you want to change the assets directory you need to configure the AssetsManager Component on your config file web.php :

'components' => [
    ...
    'assetManager' => [
        'basePath' => 'my/path/to/assets',
    ],
    ...
],

I found the solution. In the "public" folder instead of just having the "index.php" script, I have moved the complete "web" folder from the "protected" folder (where actually all the assets and scripts were located).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM