Yii使用控制器

在Web应用程序中控制器应该从 yii\web\Controller 或其子类扩展。在控制台应用程序,它们应该从 yii\console\Controller 或其子类扩展。
让我们在 Controllers 文件夹创建一个控制器实例。
第1步 - 在 Controllers 文件夹中,创建一个名为 ExampleController.php,使用下面的代码。
<?php 
   namespace app\controllers; 
   use yii\web\Controller; 
   class ExampleController extends Controller { 
      public function actionIndex() { 
         $message = "index action of the ExampleController"; 
         return $this->render("example",[ 
            'message' => $message 
         ]); 
      } 
   } 
?> 

第2步 - 在 views/example 文件夹中创建一个 example 视图。在该文件夹内,创建一个名为 example.php 视图文件,使用下面的代码文件。

<?php 
   echo $message; 
?> 

每个应用程序都有一个默认的控制器。对于Web应用程序,site是默认的控制器,而控制台应用程序则是help。因此,当URL=>http://localhost:8080/index.php 被打开时,site 控制器将处理请求。当然你也可以更改应用程序配置默认的控制器。

考虑给定的代码 -
'defaultRoute' => 'main' 

步骤3 - 将上述的代码添加到下面文件 : config/web.php.

<?php 
   $params = require(__DIR__ . '/params.php'); 
   $config = [ 
      'id' => 'basic', 
      'basePath' => dirname(__DIR__), 
      'bootstrap' => ['log'], 
      'components' => [ 
         'request' => [ 
            // !!! insert a secret key in the following (if it is empty) - this is
               //required by cookie validation 
            'cookieValidationKey' => 'xuhuhu.com', 
         ], 
         '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. 
            'useFileTransport' => true, 
         ], 
         'log' => [ 
            'traceLevel' => YII_DEBUG ? 3 : 0, 
            'targets' => [ 
               [ 
                  'class' => 'yii\log\FileTarget',
                  'levels' => ['error', 'warning'], 
               ], 
            ], 
         ], 
         'db' => require(__DIR__ . '/db.php'), 
      ], 
      //changing the default controller 'defaultRoute' => 'example',  'params' => $params, 
   ]; 
   if (YII_ENV_DEV) { 
      // configuration adjustments for 'dev' environment 
      $config['bootstrap'][] = 'debug'; 
      $config['modules']['debug'] = [ 
         'class' => 'yii\debug\Module', 
      ]; 
      $config['bootstrap'][] = 'gii'; 
      $config['modules']['gii'] = [ 
         'class' => 'yii\gii\Module', 
      ]; 
   } 
   return $config; 
?> 						  
第4步 - 在Web浏览器的地址栏中输入 http://localhost:8080/index.php,会看到默认的控制器是 example 控制器。
Yii使用控制器
注 - 控制器ID应包含小写字母,数字,斜线,连字符英文字母和下划线。
要将控制器ID转换控制器类的名字,应该做到以下几点 -
  • 以连字符分隔所有单词的第一个字母,把它变成大写
  • 删除连字符
  • 替换反向斜线
  • 添加Controller后缀
  • 前面加上控制器命名空间

示例

  • page 变成 app\controllers\PageController.

  • post-article 变成 app\controllers\PostArticleController.

  • user/post-article 变成 app\controllers\user\PostArticleController.

  • userBlogs/post-article 变成 app\controllers\userBlogs\PostArticleController.


上一篇: Yii控制器 下一篇: Yii使用动作