Php 拆分为两部分后无法解析站点/索引”;“模块”;

Php 拆分为两部分后无法解析站点/索引”;“模块”;,php,yii2,Php,Yii2,我想拆分管理器和前端: root/manager/controllers/SiteController.php namespace manager\controllers; use Yii; use yii\filters\AccessControl; use yii\web\Controller; use yii\filters\VerbFilter; use app\models\LoginForm; use app\models\ContactForm; class SiteContr

我想拆分管理器和前端:

root/manager/controllers/SiteController.php
namespace manager\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;

class SiteController extends Controller
{

    public function actionIndex()
    {
        echo 'hallo';
        //return $this->render('index');
    }
}
$params = require(__DIR__ . '/params.php');

$config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'controllerNamespace' => 'manager\controllers',
    'bootstrap' => ['log'],
    'modules' => [
        'manager' => [
            'class' => 'manager\Module',
        ],
    ],
    'components' => [
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => 'X',
        ],
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'user' => [
            'identityClass' => 'app\models\User',
            'enableAutoLogin' => true,
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => require(__DIR__ . '/db.php'),
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                '' => 'site/index',
                'fragebogen/erstellung/<id>' => 'questionary/creation',
                'fragebogen/erstellung' => 'questionary/creation',
                'auftraege-importieren' => 'upload/jobs',
                'auftraege-erfolgreich-importiert' => 'upload/jobssuccess',
            ],
        ],
    ],
    'params' => $params,
];

if (YII_ENV_DEV) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = 'yii\debug\Module';

    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = 'yii\gii\Module';
}

return $config;
root/manager/config/web.php
namespace manager\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;

class SiteController extends Controller
{

    public function actionIndex()
    {
        echo 'hallo';
        //return $this->render('index');
    }
}
$params = require(__DIR__ . '/params.php');

$config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'controllerNamespace' => 'manager\controllers',
    'bootstrap' => ['log'],
    'modules' => [
        'manager' => [
            'class' => 'manager\Module',
        ],
    ],
    'components' => [
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => 'X',
        ],
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'user' => [
            'identityClass' => 'app\models\User',
            'enableAutoLogin' => true,
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => require(__DIR__ . '/db.php'),
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                '' => 'site/index',
                'fragebogen/erstellung/<id>' => 'questionary/creation',
                'fragebogen/erstellung' => 'questionary/creation',
                'auftraege-importieren' => 'upload/jobs',
                'auftraege-erfolgreich-importiert' => 'upload/jobssuccess',
            ],
        ],
    ],
    'params' => $params,
];

if (YII_ENV_DEV) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = 'yii\debug\Module';

    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = 'yii\gii\Module';
}

return $config;

尝试删除url规则,您可能必须定义默认规则以包含至少一个的模块名。看到这个了吗

编辑
namespace manager\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;

class SiteController extends Controller
{

    public function actionIndex()
    {
        echo 'hallo';
        //return $this->render('index');
    }
}
$params = require(__DIR__ . '/params.php');

$config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'controllerNamespace' => 'manager\controllers',
    'bootstrap' => ['log'],
    'modules' => [
        'manager' => [
            'class' => 'manager\Module',
        ],
    ],
    'components' => [
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => 'X',
        ],
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'user' => [
            'identityClass' => 'app\models\User',
            'enableAutoLogin' => true,
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => require(__DIR__ . '/db.php'),
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                '' => 'site/index',
                'fragebogen/erstellung/<id>' => 'questionary/creation',
                'fragebogen/erstellung' => 'questionary/creation',
                'auftraege-importieren' => 'upload/jobs',
                'auftraege-erfolgreich-importiert' => 'upload/jobssuccess',
            ],
        ],
    ],
    'params' => $params,
];

if (YII_ENV_DEV) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = 'yii\debug\Module';

    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = 'yii\gii\Module';
}

return $config;
请查看我的模块安装说明

简而言之,如果不确定controllerNamespace在那里做什么,您应该删除它。我在这里做了类似的事情:

此后

“类”=>“管理器\模块”

我认为这行不通,经理的名字空间在哪里?我怎么知道要找呢?我使用composer在自动加载程序中添加名称空间,您可能应该手动执行类似操作。告诉Yii manager是指你的manager文件夹,之后它会找到控制器


这也可能会有所帮助,为该文件夹创建一个别名

我认为在Yi2中可能不推荐controllerNamespace

你能在配置中替换它,并使用controllerMap吗

'controllerMap' => [
    'site' => 'manager\controllers\SiteController',
],

首先,我觉得有些配置设置有问题

'modules' => [
    'manager' => [
        'class' => 'manager\Module',
    ],
],
由于您已创建了单独的应用程序,因此应删除此设置。从你的截图上看 manager应用程序中也没有模块

对于调试,只需将
enablePrettyUrl
URL设置为
false
,并在不带任何代码的情况下运行后重新启用即可 很好的网址

您可能还需要在
bootstrap.php
中为
manager
设置一个别名,对此不确定,但值得一试

从您的错误消息中,我想说应用程序根本无法加载
SiteController
, 因为
站点/错误
也会失败

插件:就个人而言,我建议每个项目只使用一个应用程序,但我知道这是一个有争议的话题。 在我看来,就像你有了它,出于某种原因切换到了两个应用程序如果您只是想
namespace manager\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;

class SiteController extends Controller
{

    public function actionIndex()
    {
        echo 'hallo';
        //return $this->render('index');
    }
}
$params = require(__DIR__ . '/params.php');

$config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'controllerNamespace' => 'manager\controllers',
    'bootstrap' => ['log'],
    'modules' => [
        'manager' => [
            'class' => 'manager\Module',
        ],
    ],
    'components' => [
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => 'X',
        ],
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'user' => [
            'identityClass' => 'app\models\User',
            'enableAutoLogin' => true,
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => require(__DIR__ . '/db.php'),
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                '' => 'site/index',
                'fragebogen/erstellung/<id>' => 'questionary/creation',
                'fragebogen/erstellung' => 'questionary/creation',
                'auftraege-importieren' => 'upload/jobs',
                'auftraege-erfolgreich-importiert' => 'upload/jobssuccess',
            ],
        ],
    ],
    'params' => $params,
];

if (YII_ENV_DEV) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = 'yii\debug\Module';

    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = 'yii\gii\Module';
}

return $config;
将自定义主题应用于经理,您可以通过

我想你的应用程序文件夹没有别名。可能自动加载组件找不到您的控制器文件。您可以通过以下操作在配置文件中添加别名:

\Yii::setAlias(’@manager’, dirname(__FILE__).'/..');
但我认为最好的方法是为manager创建另一个模块,而不是划分为不同的名称空间。

这个主题解决了吗? 如果没有,请在common/config/bootstrap.php中检查引导文件。 如果您的应用程序尝试使用路径别名,但无法解决,则会出现以下异常

异常“yii\base\InvalidRouteException”,消息“无法解析请求”站点/错误

示例
namespace manager\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;

class SiteController extends Controller
{

    public function actionIndex()
    {
        echo 'hallo';
        //return $this->render('index');
    }
}
$params = require(__DIR__ . '/params.php');

$config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'controllerNamespace' => 'manager\controllers',
    'bootstrap' => ['log'],
    'modules' => [
        'manager' => [
            'class' => 'manager\Module',
        ],
    ],
    'components' => [
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => 'X',
        ],
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'user' => [
            'identityClass' => 'app\models\User',
            'enableAutoLogin' => true,
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => require(__DIR__ . '/db.php'),
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                '' => 'site/index',
                'fragebogen/erstellung/<id>' => 'questionary/creation',
                'fragebogen/erstellung' => 'questionary/creation',
                'auftraege-importieren' => 'upload/jobs',
                'auftraege-erfolgreich-importiert' => 'upload/jobssuccess',
            ],
        ],
    ],
    'params' => $params,
];

if (YII_ENV_DEV) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = 'yii\debug\Module';

    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = 'yii\gii\Module';
}

return $config;
走错了路 Yii::setAlias('backend',dirname(dirname(DIR)。'backend');
namespace manager\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;

class SiteController extends Controller
{

    public function actionIndex()
    {
        echo 'hallo';
        //return $this->render('index');
    }
}
$params = require(__DIR__ . '/params.php');

$config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'controllerNamespace' => 'manager\controllers',
    'bootstrap' => ['log'],
    'modules' => [
        'manager' => [
            'class' => 'manager\Module',
        ],
    ],
    'components' => [
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => 'X',
        ],
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'user' => [
            'identityClass' => 'app\models\User',
            'enableAutoLogin' => true,
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => require(__DIR__ . '/db.php'),
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                '' => 'site/index',
                'fragebogen/erstellung/<id>' => 'questionary/creation',
                'fragebogen/erstellung' => 'questionary/creation',
                'auftraege-importieren' => 'upload/jobs',
                'auftraege-erfolgreich-importiert' => 'upload/jobssuccess',
            ],
        ],
    ],
    'params' => $params,
];

if (YII_ENV_DEV) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = 'yii\debug\Module';

    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = 'yii\gii\Module';
}

return $config;
正确的路径是
Yii::setAlias('backend',dirname(dirname(DIR)。/backend')

我观察到,此错误通常是由于配置错误或类和名称空间的大小写敏感名称引起的。在我的例子中,错误是由于ii8n组件的配置造成的。我使用了以下配置

namespace manager\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;

class SiteController extends Controller
{

    public function actionIndex()
    {
        echo 'hallo';
        //return $this->render('index');
    }
}
$params = require(__DIR__ . '/params.php');

$config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'controllerNamespace' => 'manager\controllers',
    'bootstrap' => ['log'],
    'modules' => [
        'manager' => [
            'class' => 'manager\Module',
        ],
    ],
    'components' => [
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => 'X',
        ],
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'user' => [
            'identityClass' => 'app\models\User',
            'enableAutoLogin' => true,
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => require(__DIR__ . '/db.php'),
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                '' => 'site/index',
                'fragebogen/erstellung/<id>' => 'questionary/creation',
                'fragebogen/erstellung' => 'questionary/creation',
                'auftraege-importieren' => 'upload/jobs',
                'auftraege-erfolgreich-importiert' => 'upload/jobssuccess',
            ],
        ],
    ],
    'params' => $params,
];

if (YII_ENV_DEV) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = 'yii\debug\Module';

    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = 'yii\gii\Module';
}

return $config;
    'i18n' => [
        'translations' => [
            '*' => [
                'class' => 'yii\i18n\PhpMessageSource',
                'basePath' => '@backend/messages',
                'sourceLanguage' => 'en-US',
                'forceTranslation'=> true,
            ],
        ],
    ],

问题在我删除配置后得到解决

我想您没有manager文件夹的别名

    'i18n' => [
        'translations' => [
            '*' => [
                'class' => 'yii\i18n\PhpMessageSource',
                'basePath' => '@backend/messages',
                'sourceLanguage' => 'en-US',
                'forceTranslation'=> true,
            ],
        ],
    ],
common\config\bootstrap.php
中添加别名

Yii::setAlias('@manager', dirname(dirname(__DIR__)) . '/manager');
因此,您的
common\config\bootstrap.php
文件应该如下所示:

Yii::setAlias('@common', dirname(__DIR__));
Yii::setAlias('@frontend', dirname(dirname(__DIR__)) . '/frontend');
Yii::setAlias('@backend', dirname(dirname(__DIR__)) . '/backend');
Yii::setAlias('@console', dirname(dirname(__DIR__)) . '/console');
Yii::setAlias('@manager', dirname(dirname(__DIR__)) . '/manager');

为什么不用这个?它已经为你分开了。您正在尝试更改基本模板,使其与高级模板类似。我可以使用它,但我想使用自己的命名空间,可能需要使用两个以上的模块。请显示整个文件夹结构,frontend和manager文件夹的根文件夹是什么。这叫做根?我也没有得到你想要的。分模块还是分应用?您提到了模块,但它看起来像应用程序。根文件夹不是根文件夹。。我写root只是为了你的信息。。在屏幕截图上,你看到了整个文件结构……删除url规则没有帮助。它在Yii2中肯定可用,应该在OP的场景中设置。请参见我设置的。。但我还是犯了这个错误<代码>无法解决请求“站点/错误”