0

Is there any way to implement routing based on the request params? For example,

/v1/articles - action `serve_articles`
/v1/articles?type=list - action `server_filtered_by_list`
2

1 Answer 1

2

The simplest way form me is this

you can add to you (eg: index) action the param you need for you purpose

public function actionIndex($type, $param2, $param3)
{
     ... then your code inclus the call for action in controller or
     .... redirect or
     .... render

    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
}
Sign up to request clarification or add additional context in comments.

3 Comments

thanks, wouldn't it be considered a bad code? Do you know any other framework that allows distinguishing by param inside router definitions?
I agree that this is not good code, but what you are trying to accomplish is not good pratice at all. If you are trying to access different action change your request instead of adding parameters and later on redirecting user. If you could provide case which made you came up with such solution it would be easier to help you.
@xReprisal I agree, that this is not a good practice, but it is an answer to the question that was asked. It seems clear that the way forward is to organize the action and access it in proper way but the question of how it was done seems to me that calling for a solution of this type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.