3

I'm new to using Yii2 and have been using the urlManager, I have the following code, it works fine however I think that this should be shorter. I have a couple rules as follows :-

    'rules' => [        
        'gifts/<subjectone:[\s\S]+>/<subjecttwo:[\s\S]+>' => 'gifts/index',
            'gifts/<subjectone:[\s\S]+>/<subjecttwo:[\s\S]+>/' => 'gifts/index',
            'gifts/<subjectone:[\s\S]+>' => 'gifts/index',
            'gifts/<subjectone:[\s\S]+>/' => 'gifts/index',
     ];

As you can see I've added the 4 rules all to go to the same page to handle different situations.

I've had to add the same Url's twice, once with the / and one without to stop 404's. Please advise of a better way to handle this.

ahervin
  • 461
  • 2
  • 15

1 Answers1

1

You can add +|(\/?) at trailing of your rule. Take a look:

 'gifts/<subjectone:[\s\S]+>+|(\/?)' => 'gifts/index',

So there is no need to write your rules twice.

Ali MasudianPour
  • 14,329
  • 3
  • 60
  • 62