phpmoduleyii2yii2-urlmanager

Yii2 - Rules of urlManager with UUID as id


I'm try create rules in the urlManager to use UUID as id. The browser sends a next URL:

https://localhost/profiles/delete/e1028ae1-ce79-11e8-a22d-00163e9c1798

I'm have the follow settings:

main.php

'urlManager' => [
     'class' => 'yii\web\UrlManager',
     'enablePrettyUrl' => true,
     'showScriptName' => false,
     'rules' => [
         '' => 'site/index',
         '<action:(login|logout|about|contact)>' => 'site/<action>',
         //profiles
         '<module:(profiles)>/<id:\d+>' => '<module>/default/view',
         '<module:(profiles)>/<action:(index|delete|new)>/<id:\w+>' => '<module>/default/<action>',
         '<module:(profiles)>/<action:(index|delete|new)>' => '<module>/default/<action>',
     ]
]

Part of my DefaultController.php (/common/modules/profiles/coontrollers/DefaultController.php)

<?php

namespace common\modules\profiles\controllers;
...

class DefaultController extends Controller
{
    ...
    public function actionDelete($id)
    {
        die($id);
    }
    ...
}

I'm use AJAX to send the param to the action.

let action = 'profiles/delete'
let id = 'e1028ae1-ce79-11e8-a22d-00163e9c1798'
$.ajax({
    async: false,
    url: 'https://localhost/' + action + '/' + id,
    type: 'POST',
    dataType: 'json',
    success: (response) => {
        console.log(response)
    }
})

My Problem: I access the index action and new action of module without problems. But when I call the delete action I receive a 404 error.

I don't know if I have wrong the rules in then urlManager or I'm send the params wrong via AJAX. I have static params because I'm try make first the implementation.


Solution

  • Your pattern for id param is incorrect - \w does not allow hyphen, so it will not match IDs containing -. You need to change this rule:

    '<module:(profiles)>/<action:(index|delete|new)>/<id:\w+>' => '<module>/default/<action>',
    

    into this:

    '<module:(profiles)>/<action:(index|delete|new)>/<id:[\w-]+>' => '<module>/default/<action>',