简体   繁体   中英

urlManager in yii

'urlManager'=>array(
            'urlFormat'=>'path',
            'rules'=>array(
               'ongSpcl/<category:.*?>'=>'ongSpcl/index',
            ),
        ),

The above code reads the url "abc.co/ongSpcl/sp1" as "abc.co/ongSpcl?category=sp1 It works! But my issue is I want to read all the url in the above format except for "abc.co/ongSpcl/ index "

Put a rule for 'ongSpcl/index'=>ongSpcl/index (change the right side to whatever route you want) prior to your existing rule. Yii will execute the first match it finds.

Also, fyi, you can use <category:\\w+> to match any non-null strings of alphanumeric characters if that's your intention.

Removing trailing index from Yii Url, change show category action from ongSpcl#index to something else, then add '/ongSpcl' => 'ongSpcl/index' ,

'urlManager'=>array(
  'urlFormat'=>'path',
  'rules'=>array(
    '/ongSpcl' => 'ongSpcl/index',
    '/ongSpcl/<category:.*?>'=>'ongSpcl/show',
  ),
),

Stub for testing routes

class RoutingTest extends CTestCase {
  public function createUrl($route, $params=array()) {
    $url = explode('phpunit', Yii::app()->createUrl($route, $params));
    return $url[1];
  }

  public function testCorrectRoutes() {
    $this->assertEquals('/ongSpcl', $this->createUrl('ongSpcl/index'));
    $this->assertEquals('/ongSpcl/sp1', $this->createUrl('ongSpcl/show', array('category' => 'sp1'));
    //
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM