Url manager and search

hi all!
advise me please how can i do this search url’s with pretty url:

myhost/search/yii2 url manager

‘yii2 url manager’ and ‘yii2%manager’ in this case is any search string (named as src parameter)

myhost/search/yii2 - works fine
my rule is ‘search/src:\w+’ => ‘category/search’,

error: yii\base\InvalidRouteException: Unable to resolve the request “search/yii2 url manager”.

Hi micpan,

I don’t understand what you are trying to do.
Would you please elaborate your problem a little bit more clearly?

What is your controller that you want to call? How do you define it as an action method? What parameter does that action accept? And where and how do you want to call it? I mean, from another controller or from a browser’s address bar?

it is search ajax service on my site.
i have search field like this:

form id=“search_products” class=“form-inline” action="/search/src">
input id=“src_field” class=“form-control mr-sm-1” type=“search” placeholder=“search” aria-label=“Search”>
button id=“src_button” type=“submit” class=“btn btn-outline-success m-0”>

and some js:

 $("#search_products").on("submit", function() {
     var src = $('#src_field').val();
     if (src.length < 5) {
         return false;
     document.title = 'search: ' + src;
     $url = $('#search_products').attr('action').replace('src', $('#src_field').val());

$('#content>div').fadeOut(400, function() {
        function onAjaxSuccess(data) {
     return false;

i want to get url’s to search result as well. Like this: ‘myhost/search/yii2 url manager’
how to write rigth rule and controller for this?

I still don’t understand.

How do you define your “search/src” (i.e., “category/search”) ? I mean, what is the signature of CategoryController::actionSearch method?

OK, it’s just a guess, but I would do something like the following.


public function actionSearch($q)
    $models = SomeModel::find()->where(['like', 'some_column', $q)->all();

The url rule:

     'search/src/<q:\w+>' => 'category/search',

And I think you may need to url encode the query string in your javascript.


$url = ‘/search/’+$(’#src_field’).val(); //any query string from user input field
$.get($url, //and so on

‘search/<src:\w+>’ => ‘category/search’,// here is a key point

when i enter one word in search field - all works fine
when two words (‘yii2 url’ for example) =
GET http://mysite/search/yii2%20url
yii\base\InvalidRouteException: Unable to resolve the request “search/yii2 url”. in M:\OSPanel\domains\mysite\vendor\yiisoft\yii2\base\Module.php:537

Maybe this ?

'search/src/<q:.*>' => 'category/search',

‘search/<src>’ it was so simply :slight_smile:

1 Like