【发布时间】:2018-08-25 22:17:44
【问题描述】:
Yii2 的默认路由是@app/controllers/SiteController。 但如果我从头开始构建。
composer require yiisoft/yii2
然后我创建自己的 index.php 并为应用设置配置
(new yii\web\Application($config))->run();
并且应用程序总是尝试在应用程序命名空间中本地化 defaultContoller。但是如果我将 SiteController 放在另一个命名空间中。它给了我 404 错误,
index.php
require(__DIR__ . '/vendor/autoload.php');
require(__DIR__ . '/vendor/yiisoft/yii2/Yii.php');
$config = require(__DIR__ . '/repo/config/web.php');
(new yii\web\Application($config))->run();
web.php
'id' => 'repo',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'repo\\controllers',
'defaultRoute' => 'site/index',
文件夹结构
/vendor
-yiisoft
/repo
-config
---web.php
-controllers
---SiteController.php
站点控制器.php
<?php
namespace repo\controllers;
use Yii;
use yii\web\Controller;
class SiteController extends Controller{
public function actionIndex(){
echo 'welcome to the site';
}
}
?>
如果我给 SiteController.php 命名空间 app/controllers 它可以工作,但是一旦我将它改回 repo/controllers 它说找不到页面。
【问题讨论】: