English 中文(简体)
Yii Tutorial

Yii Useful Resources

Selected Reading

Yii - Using Controllers
  • 时间:2024-09-17

Yii - Using Controllers


Previous Page Next Page  

Controllers in web apppcations should extend from yiiwebController or its child classes. In console apppcations, they should extend from yiiconsoleController or its child classes.

Let us create an example controller in the controllers folder.

Step 1 − Inside the Controllers folder, create a file called ExampleController.php with the following code.

<?php 
   namespace appcontrollers; 
   use yiiwebController; 
   class ExampleController extends Controller { 
      pubpc function actionIndex() { 
         $message = "index action of the ExampleController"; 
         return $this->render("example",[ 
             message  => $message 
         ]); 
      } 
   } 
?>

Step 2 − Create an example view in the views/example folder. Inside that folder, create a file called example.php with the following code.

<?php 
   echo $message; 
?>

Each apppcation has a default controller. For web apppcations, the site is the controller, while for console apppcations it is help. Therefore, when the http://localhost:8080/index.php URL is opened, the site controller will handle the request. You can change the default controller in the apppcation configuration.

Consider the given code −

 defaultRoute  =>  main 

Step 3 − Add the above code to the following 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 vapdation 
             cookieVapdationKey  =>  ymoaYrebZHa8gURuopoHGlK8fLXCKjO , 
         ], 
          cache  => [ 
             class  =>  yiicachingFileCache , 
         ], 
          user  => [ 
             identityClass  =>  appmodelsUser , 
             enableAutoLogin  => true, 
         ], 
          errorHandler  => [ 
             errorAction  =>  site/error , 
         ], 
          mailer  => [ 
             class  =>  yiiswiftmailerMailer , 
            // 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  =>  yiilogFileTarget ,
                   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  =>  yiidebugModule , 
      ]; 
      $config[ bootstrap ][] =  gii ; 
      $config[ modules ][ gii ] = [ 
          class  =>  yiigiiModule , 
      ]; 
   } 
   return $config; 
?> 						  

Step 4 − Type http://localhost:8080/index.php in the address bar of the web browser, you will see that the default controller is the example controller.

Controller Example

Note − The Controller IDs should contain Engpsh letters in lower case, digits, forward slashes, hyphens, and underscores.

To convert the controller ID to the controller class name, you should do the following −

    Take the first letter from all words separated by hyphens and turn it into uppercase.

    Remove hyphens.

    Replace forward slashes with backward ones.

    Add the Controller suffix.

    Prepend the controller namespace.

Examples

    page becomes appcontrollersPageController.

    post-article becomes appcontrollersPostArticleController.

    user/post-article becomes appcontrollersuserPostArticleController.

    userBlogs/post-article becomes appcontrollersuserBlogsPostArticleController.

Advertisements