পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Wednesday, March 22, 2017

Yii - Using Actions

To create an action in a controller class, you should define a public method whose name starts with the word action. The return data of an action represents the response to be sent to the end user.
Step 1 − Let us define the hello-world action in our ExampleController.
<?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 
         ]); 
      } 
      public function actionHelloWorld() { 
         return "Hello world!"; 
      } 
   } 
?>
Step 2 − Type http://localhost:8080/index.php?r=example/hello-world in the address bar of the web browser. You will see the following.
Hello World Action Action IDs are usually verbs, such as create, update, delete and so on. This is because actions are often designed to perform a particular change if a resource.
Action IDs should contain only these characters − English letters in lower case, digits, hyphens, and underscores.
There are two types of actions: inline and standalone.
Inline actions are defined in the controller class. The names of the actions are derived from action IDs this way −
  • Turn the first letter in all words of the action ID into uppercase.
  • Remove hyphens.
  • Add the action prefix.
Examples
  • index becomes actionIndex.
  • hello-world(as in the example above) becomes actionHelloWorld.
If you plan to reuse the same action in different places, you should define it as a standalone action.

Create a Standalone Action Class

To create a standalone action class, you should extend yii\base\Action or a child class, and implement a run() method.
Step 1 − Create a components folder inside your project root. Inside that folder create a file called GreetingAction.php with the following code.
<?php 
   namespace app\components;
   use yii\base\Action;
   class GreetingAction extends Action {
      public function run() {
         return "Greeting";
      }
   }
?>
We have just created a reusable action. To use it in our ExampleController, we should declare our action in the action map by overriding the actions() method.
Step 2 − Modify the ExampleController.php file this way.
<?php
   namespace app\controllers;
   use yii\web\Controller;
   class ExampleController extends Controller {
      public function actions() {
         return [
            'greeting' => 'app\components\GreetingAction',
         ];
      }
      public function actionIndex() {
         $message = "index action of the ExampleController";
         
         return $this->render("example",[
            'message' => $message
         ]);
      }
      public function actionHelloWorld() {
         return "Hello world!";
      }
   }
?>
The actions() method returns an array whose values are class names and keys are action IDs.
Step 3 − Go to http://localhost:8080/index.php?r=example/greeting. You will see the following output.
Greeting Example Step 4 − You can also use actions to redirect users to other URLs. Add the following action to the ExampleController.php.
public function actionOpenGoogle() {
   // redirect the user browser to http://google.com
   return $this->redirect('http://google.com');
} 
Now, if you open http://localhost:8080/index.php?r=example/open-google, you will be redirected to http://google.com.
The action methods can take parameters, called action parameters. Their values are retrieved from $_GET using the parameter name as the key.
Step 5 − Add the following action to our example controller.
public function actionTestParams($first, $second) {
   return "$first $second";
}
Step 6 − Type the URL http://localhost:8080/index.php?r=example/testparams&first=hello&second=world in the address bar of your web browser, you will see the following output.
Run Hello World Example Each controller has a default action. When a route contains the controller ID only, it means that the default action is requested. By default, the action is index. You can easily override this property in the controller.
Step 7 − Modify our ExampleController this way.
<?php
   namespace app\controllers;
   use yii\web\Controller;
   class ExampleController extends Controller {
      public $defaultAction = "hello-world";
      /* other actions */
   }
?>
Step 8 − Now, if you go to http://localhost:8080/index.php?r=example, you will see the following.
Run Hello World Example1 To fulfill the request, the controller will undergo the following lifecycle −
  • The yii\base\Controller:init() method is called.
  • The controller creates an action based on the action ID.
  • The controller sequentially calls the beforeAction() method of the web application, module, and the controller.
  • The controller runs the action.
  • The controller sequentially calls the afterAction() method of the web application, module, and the controller.
  • The application assigns action result to the response.

Important Points

The Controllers should −
  • Be very thin. Each action should contain only a few lines of code.
  • Use Views for responses.
  • Not embed HTML.
  • Access the request data.
  • Call methods of models.
  • Not process the request data. These should be processed in the model.

No comments:

Post a Comment