M
M
Mikhail2019-08-27 06:08:04
Yii
Mikhail, 2019-08-27 06:08:04

How to properly configure Yii2 routing?

There is a form

echo Html::beginForm(['main/search'], 'GET');
        echo Html::input('text', 'request');        
        echo Html::submitButton('Поиск);
        echo Html::endForm();

And the route When you enter for example "123" gives out And I want it to give out In this case, if you modify the form (for verification)
'search/<request:\w+>' => 'main/search'
http://test.com/main/search?request=123
http://test.com/search/123
echo Html::beginForm(['main/search', 'request' => '123' ], 'GET');   
        echo Html::submitButton('Поиск);
        echo Html::endForm();

then everything works.
Please tell me what could be the problem.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Michael, 2019-08-28
@Medved_1989

I figured it out, if anyone needs to skin the code in the future
1. Install dmirogin/yii2-js-urlmanager
2. Next, we need to add an id to the form and the input field

echo Html::beginForm(['main/search'], 'GET', $options = ['id' => 'searchForm']);
        echo Html::input('text', 'request', '', $options = ['id' => 'searchinput']);
        echo Html::submitButton('Поиск');
        echo Html::endForm();

3. Write the script (before the closing body tag)
$("#searchForm").submit(function (e) {
            e.preventDefault();
            var url = UrlManager.createUrl('main/search', {request: $("#searchinput").val()});
            window.location.replace(url);
        });

preventDefault();- to change the url and send your own, you must definitely prevent the standard form submission, which this function does
UrlManager.createUrl('main/search', {request: $("#searchinput").val()});
- this is where we form our url using the dmirogin / yii2-js-urlmanager extension, and
window.location.replace(url);here we are already redirecting the user to the previously generated url
With the prescribed route 'search/<request:\w+>' => 'main/search'
, we are successfully sent to the Url of the form http://test.com/search/123
didn't give a direct solution, but pointed me in the right direction.

L
Lander, 2019-08-27
@usdglander

That's right. What you wrote, he does to you. The browser handles the form submission. How it does it:
1. At submitthe form event, the browser takes a parameter action
2. Takes all the fields enclosed between the tags <form></form>
3. Takes it from each field nameand value(for textarea type fields, I think it’s clear that it’s not value)
4. Forms from these fields request of the form (in the case of a GET request): name1=value1&name2=value2&...
5. Assigns a request from point 4 to actionpoint 1 through a question mark ?
6. Redirects to the received url
What should be done to change this behavior? Everything is very simple: you just need to intercept the submitforms and write your own url generation logic.

D
Dmitry, 2019-08-27
@slo_nik

Good afternoon.
I recommend reading the documentation and watching this video .
If you have a search form, then describing the options in the configuration will be tricky.
This is because the amount of data in the query string can change.
If the amount of data you have is fixed, then you can start with this.

'rules' => [
  // если main у Вас контролер, а search действие
  '' => 'main/index',
 '<_a:(search)>/<request:\w+>' => 'main/search'
]

These links will help you too.
https://yiiframework.ru/forum/viewtopic.php?f=19&t...
https://yiiframework.ru/forum/viewtopic.php?f=19&t...
https://habr.com/ru/ post/308948/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question