1. Go to this page and download the library: Download maks757/yii2-friendly-url library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
maks757 / yii2-friendly-url example snippets
// implements IUrlRules !!!
class ProductModel extends \yii\db\ActiveRecord implements maks757\friendly\components\IUrlRules
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'products';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
//...
// seo data
[['seoUrl', 'seoTitle', 'seoDescription', 'seoKeywords'], 'string']
];
}
// Exemple: $key = 'my-first-product' -> return id
/**
* @param mixed $key
* @return boolean|integer model id
*/
public function fiendKey($key)
{
$model = ProductModel::findOne(['seoUrl' => $key]);
return empty($model) ? false : $model->id;
}
// Exemple: $id = 10 -> return seoUrl
/**
* @param integer $id
* @return string
*/
public function seoUrl($id)
{
return ProductModel::findOne($id)->seoUrl;
}
}