general filter by checking required paramters

Hi all, I’m new to Yii2. Here is my senario: we all know sometimes we should do check the required parameters before the action executing of a controller, for example:




class SiteController

{

    public action sayHi()

    {

        $name = Yii::$app->request->get('name');

        if(empty($name)) { // check the required name here

            echo "name is required";

        }


        echo "Hi $name";

    }

}



Now I want to implement this functionality by PHP builtin ReflectionMethod like following:




class BaseController : Controller

{

    private static $rRequire = '/require:\s*(.*)/';

    private static $seperator = ',';


    protected $params;


    public function beforeAction($action)

    {

        $className = $action->controller->id;

        $method = $action->actionMethod;

        $rm = new \ReflectionMethod($className, $method);

        $comment = $rm->getDocComment();

        if ($comment && preg_match($rRequire, $comment, $rawMatch)) {

            $this->params = array_map('trim', explode(self::$seperator, $rawMatch[1]));

            $this->checkRequire();

        }

    }


    protected function checkRequire()

    {

        foreach($this->params as $param) {

            if(Yii::$app->request($param)) {

                continue;

            } else {

                echo "`$param` is required";

                die;

            }

        }


    }

}


class SiteController : BaseController

{

    /**

     * require: name

     */

    public action sayHi()

    {

        $name = Yii::$app->request->get('name')

        echo "Hi $name";

    }

}



or by a general filter inheriting ActionFilter

So my question is: does Yii2 contain kind of filter like this?

Thanks all.

if you want any parameters as required, you can declare like this:




class SiteController

{

    public action sayHi($name)

    {

        echo "Hi $name";

    }

}



In a higher level, if it is a form submission, you can declare your variables in your model, and set your rules - required is one of the rules - which can be validated.

In Yii actions, you can get the GET query string by function parameters

Also, if you set the default value of that parameter, it means the query string is optional,

so you may do the following




public actionSayHi($name=null){

    if($name == null) { // check the required name here

        echo "name is required";

    }


    echo "Hi $name";

}



The above code tell the controller to get value of ‘name’ from query string.

if it is not present, just set it to null.

So, by checking whether $name is null, you can see if user have pass ‘name’ to the server in query string.

You can see more in Action Parameters

Yii has many built-in features in URL routing, you don’t need to handle those things yourself

Thanks for your replying first.

Maybe in my situation, there is no suitable Model or Form, I want to do this check in one place, so I don’t need to cheek in the business logic repeatly, because maybe there are many method should do this logic. And what if I want to return client a json object friendly?

This works json friendly




class SiteController

{

    public action sayHi($name)

    {

        Yii::$app->response->format='json';

        return "Hi $name";

    }

}



Thanks a lot.