1. The table structure is designed to allow the alias to be null, defaults to null, and has a unique index added.
-- `official-website-management-system`.articles definition
CREATE TABLE `articles` (
`id` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL,
`alias` varchar(200) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `idx-alias` (`alias`),
KEY `idx-category_id` (`category_id`),
KEY `idx-status` (`status`),
KEY `idx-updated_at` (`updated_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
2. When submitting the edit form in the backend, empty strings are automatically converted to null. Set the conversion rule and validation in the model’s rules.
/**
* {@inheritdoc}
*/
public function rules()
{
return [
['alias', 'filter', 'filter' => function ($value) {
return $value === '' ? null : $value;
}],
['alias', 'match', 'pattern' => '/^[a-z0-9\-@_]+$/', 'message' => '别名只能包含小写字母、数字、中划线、@ 和下划线。'],
];
}
3. In the frontend view file, the link generation is implemented as follows:
frontend/views/article/index.php
<a class="item" href="<?= ArticleUrlHelper::detailUrl($article) ?>">
frontend/helpers/ArticleUrlHelper.php
public static function detailUrl($article): string
{
if (!empty($article['alias']) && UrlHelper::isValidAlias($article['alias'])) {
return Url::to(['article/view', 'alias' => $article['alias']]);
}
return Url::to(['article/view', 'id' => $article['id']]);
}
frontend/helpers/UrlHelper.php
public static function isValidAlias(string $alias): bool
{
return preg_match('/^[a-z0-9\-@_]+$/', $alias) === 1;
}
4. Configure urlManager in frontend/config/main.php as follows:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => true,
'normalizer' => [
'class' => yii\web\UrlNormalizer::class,
'collapseSlashes' => true,
'normalizeTrailingSlash' => true,
],
'rules' => [
'' => 'page/index',
// 资讯详情
'industry/detail-.html' => 'article/view',
'industry/.html' => 'article/view',
],
],
5. When the alias is @dd87-_s, the generated link is /industry/%40dd87-_s.html. See Figure 1.

6. The reason is that Url::to() automatically URL-encodes the parameters. The alias is @dd87-_s, where @ is a special character and gets encoded as %40. I decided not to apply additional processing for now.
