Yii2中的默認路由爲@ app/controllers/SiteController。 但如果我從頭開始構建。Yii2將默認路由更改爲另一個命名空間
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
SiteController.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命名空間的應用程序/控制器它的工作原理,但一旦我將它改回到回購/控制器它說,找不到頁面。
我將其添加在配置文件中,但仍獲得404 – tyan
@tyan請出示應用程序配置,文件夾結構和控制器代碼。 – arogachev
更新問題@argoachev – tyan