Php 如何返回格式正确的标题,以便在yii2的URI中使用?
在Yi2框架中,哪个函数与Kohana中的Php 如何返回格式正确的标题,以便在yii2的URI中使用?,php,yii2,Php,Yii2,在Yi2框架中,哪个函数与Kohana中的url::title()类似?例如 $input_title=''Eclçtic_uuuuuuuuuuuuuuuuuuuuuuutitle'由crázed用户输入-?>' echo url::title($input_title,'.'''.'输入') 生成: 疯狂用户输入的折衷标题将对您有所帮助 代码示例: $slugify->activateRuleset('esperanto'); echo $slugify->slugify('ser
url::title()
类似?例如
$input_title=''Eclçtic_uuuuuuuuuuuuuuuuuuuuuuutitle'由crázed用户输入-?>'代码>
echo url::title($input_title,'.'''.'输入')代码>
生成:
疯狂用户输入的折衷标题将对您有所帮助
代码示例:
$slugify->activateRuleset('esperanto');
echo $slugify->slugify('serĉi manĝi'); // sercxi-mangxi
因为我没有收到任何关于Yii2方法使用的建议,所以我在我的定制助手上写了这篇文章。建议的Slugify软件包也不错,但一个功能对我来说就足够了
public static function title($title, $separator = '-')
{
$separator = ($separator === '') ? '' : '-';
// Remove all characters that are not the separator, a-z, 0-9, or whitespace and arabic lang
$title = preg_replace ( "/&([\x{0600}-\x{06FF}a-zA-Z])(uml|acute|grave|circ|tilde|ring),/u", "", $title );
$title = preg_replace ( "/[^\x{0600}-\x{06FF}a-zA-Z0-9_ .-]/u", "", $title );
// Remove all characters that are not the separator, a-z, 0-9, or whitespace
// $title = preg_replace('/[^'.$separator.'a-z0-9\s]+/', '', strtolower($title));
// Replace all separator characters and whitespace by a single separator
$title = preg_replace('/['.$separator.'\s]+/', $separator, $title);
// Trim separators from the beginning and end
return trim($title, $separator);
}
嗯,你的yii中确实有可攻击的行为
在模型中,您必须更新行为:
use yii\behaviors\SluggableBehavior;
public function behaviors()
{
return [
[
'class' => SluggableBehavior::className(),
'attribute' => 'title',
],
];
}
我总是使用id作为slug的一部分,以确保唯一性
'urlManager' => [
'rules' => [
'<id:\d+>/<slug:[A-Za-z0-9 -_.]+>' => '<controller>/view',
]
]
'urlManager'=>[
“规则”=>[
“/”=>“/视图”,
]
]
请更好地解释标题是什么意思?路线?网址?或者其他?只需检查示例。基本上我指的是文本。谢谢,我希望在Yii2中会有一些内置的东西,但我想我必须编写自己的函数。你可以通过composer简单地将这个包添加到Yii2中。您需要代码示例吗?