P
P
Philip2014-06-21 23:16:04
Yii
Philip, 2014-06-21 23:16:04

CNC Yii2, passing GET parameters, how to arrange?

In Yii, you used to write like this:

'urlManager'=>array(
      'showScriptName'=>false,
      'urlFormat' => 'path',
    ),

And then, when requesting /site/index/my_get/2 , in the index action, I received the get parameter my_get with a value of 2.
I want to do the same in Yii2, I write:
'urlManager'=>[
            'class'=>'yii\web\UrlManager',
            'showScriptName'=>false,
            'enablePrettyUrl' => true,
        ],

As a result, the behavior is completely different, I get "Unable to resolve the request".
Please tell me how to achieve the same result as in Yii as
easy as possible >2]

Answer the question

In order to leave comments, you need to log in

4 answer(s)
P
Philip, 2014-06-22
@shcherbanich

Decision:

'rules' => [
                ['class' => 'app\components\CarUrlRule', 'connectionID' => 'db'],
            ],

Class code:
<?php

namespace app\components;

use yii\web\UrlRule;

class CarUrlRule extends UrlRule
{
    public $connectionID = 'db';
    public $pattern = 'car';
    public $route = 'car';

    public function createUrl($manager, $route, $params)
    {
        if ($route === 'car/index') {
            if (isset($params['manufacturer'], $params['model'])) {
                return $params['manufacturer'] . '/' . $params['model'];
            } elseif (isset($params['manufacturer'])) {
                return $params['manufacturer'];
            }
        }
        return false;  // this rule does not apply
    }

    public function parseRequest($manager, $request)
    {
        $pathInfo = $request->getPathInfo();

        $segs=explode('/',$pathInfo.'/');

        $data = array();
           
        $n=count($segs);       
    
        for($i=0;$i<$n-1;$i+=2){

            if($segs[$i]==='') 
                continue;

            $value = $segs[$i+1];

            if($m=preg_match_all('/\[(.*?)\]/',$segs[$i],$matches)){
                
                $name=substr($segs[$i],0,$pos);

                for($j=$m-1;$j>=0;--$j)
                    if($matches[1][$j]==='')
                        $value=array($value);
                    else
                        $value=array($matches[1][$j]=>$value);

                $data[$name]=$value;
            }
            else
                $data[$segs[$i]]=$value;
        }
        return ['site/index', $data];
    }
}

P
phpnt, 2015-04-19
@phpnt

Made a video on UrlManager

V
Vladimir Golubev, 2015-03-18
@wladyspb

There is a simpler solution, in the url manager config it looks something like this:

'urlManager' => [
      'enablePrettyUrl' => true,
      'showScriptName' => false,
      'rules' => [
        '<controller>/<action>' => '<controller>/<action>',
        'site/hello/<name:\w+>' => 'site/hello',
      ]
    ],

After that, the site/hello/someName call is passed to the site controller, the hello action with the get parameter $name = SomeName

I
Igor Vasiliev, 2019-07-05
@Isolution666

EXAMPLE:
go to /site/index/c/1/b/2

'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                '' => 'site/index', // сделает главную страницу сайтконтроллера чистой без слэшей и экшенов
               'site/index/<c:\d+>/<b:\d+>' => 'site/index', // если верхнее правило оставить можно вот так обработать http://example.ru/1/2 - d+ - digital - обрабатывает только цифры
...
],

RESULT:
we are in the index action of the site controller, getting an array GET [c=>1,b=>2]

Here I see only two options. if you want as `/site/index/c/1/b/2`
'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                '' => 'site/index', 
               'site/index/<param:\w+>/<c:\d+>/<params:\w+>/<b:\d+>' => 'site/index', 
               'site/index/<param:\w+>/<c:\d+>/<params:\w+>/<b:\d+>.html' => 'site/index',  // с преффиксом .html
// analog -> http://example.ru/site/index?param=c&c=1&params=b&b=2
// analog -> http://example.ru/site/index/c/1/b/2
// preffix -> http://example.ru/site/index/c/1/b/2.html
...
],

Everything is much easier :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question