0

I'm new to Yii2 and their URL ruling is kinda tricky. I have a SiteController with an action like this

public function actionSuccessStories($slug = null)
{
    // some codes
}

in my config i have this

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'enableStrictParsing' => true,
    'rules' => [
        // Default routes
        '<controller:\w+>/<id:\d+>' => '<controller>/view',
        '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
        '<controller:\w+>/<action:\w+>' => '<controller>/<action>',

        // page routes
        'success-stories/<slug:\w+>' => 'site/success-stories',

        // Remove 'site' parameter from URL
        '<action:(.*)>' => 'site/<action>',
    ],
],

in my view i have this to generate my url

Url::to(['site/success-stories', 'slug' => 'slug_value'], true);

my problem is Url::to(); creates success-stories?slug=slug_value

instead of

success-stories/slug/slug_value am i doing this right? What i want to accomplish is the second format.

I've read this question related to mine but it covers only modules Yii2 url manager don't parse urls with get parameter

5
  • 1
    Youre missing - sign, try: 'success-stories/<slug:\w+>' => 'site/success-stories', Commented Aug 11, 2017 at 5:50
  • 'success-stories/<slug:\w+>' => 'site/success-stories' tried it but its still the same it still produce this success-stories?slug=slug_value and if i try to type it like this in my browser success-stories/slug/slug_value i get a 404 error Commented Aug 11, 2017 at 6:06
  • Can you share urlManager config? Commented Aug 11, 2017 at 6:07
  • But i need rules ;) Update your question with full config ;) Commented Aug 11, 2017 at 6:12
  • oh sorry about that Commented Aug 11, 2017 at 6:13

1 Answer 1

3

Change your rules to:

'rules' => [
    // page routes
    'success-stories/<slug:\w+>' => 'site/success-stories',

        // Default routes
    '<controller:\w+>/<id:\d+>' => '<controller>/view',
    '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
    '<controller:\w+>/<action:\w+>' => '<controller>/<action>', 

    // Remove 'site' parameter from URL
    '<action:(.*)>' => 'site/<action>',
],

Order of rules is important, Yii2 will try to match rule one by one, if it fits - it will use it.

Sign up to request clarification or add additional context in comments.

4 Comments

'success-stories/<slug>' - how about this? Maybe your slugs contains inappropriate characters.
Great! 'success-stories/<slug>' => 'site/success-stories' works. but my question is this . My slug value is like this-value maybe that's the reason <slug:\w+> is not working?
this-value contains - sign, by \w+ you allow only "word characters"
Thanks you're a great help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.