Shorten URL by removing empty GET variables and simplifying variable names

后端 未结 4 869
温柔的废话
温柔的废话 2021-01-25 18:19

I am working on a website where an URL is composed after submitting a GET form. The form values are passed as an array of variables of which at least one must b

4条回答
  •  佛祖请我去吃肉
    2021-01-25 18:53

    I recommend you following solution: First, you need to define html form with POST method:

    Second, you need to define getSearchTerms action in your ExampleController:

    public function actionGetSearchTerms()
    {
        $this->render(Yii::app()->baseUrl.'/example/search/'.$_POST['name']);
    }
    

    Then, you need to define main search action:

    public function search($name)
    {
        //do search operation here.
    }
    

    Finally, you need to add a url-manager rule:

    "example/search/"=>"example/search"
    

    In this solution, getSearchTerms action is responsible for receiving user entered text, and then pass values to search action. Now your url can be http://localhost/example/search/sampleText . Note you can skip adding url-manager rule if you like. In this case, your url must be like http://localhost/example/search/name/sampleText. In fact, we can remove "name" part from url by adding url-manager rule.

提交回复
热议问题