maks757/yii2-friendly-url
最新稳定版本:v1.0.0
Composer 安装命令:
composer require maks757/yii2-friendly-url
包简介
Friendly url for your library or module
README 文档
README
Friendly url for your library or module
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist maks757/yii2-friendly-url "*"
or add
"maks757/yii2-friendly-url": "*"
to the require section of your composer.json file.
Usage
Model
// 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; } }
Configuration
'components' => [ //... 'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, 'rules' => [ 'news' => 'product/show', [ 'class' => \maks757\friendly\UrlRules::className(), 'action' => 'product/show', // View 'product/show' or news 'controller_and_action' => 'product/show', // Action news show //param: (action_key) - Action param get product id //param: (url_key) - // View set product id 'routes' => [ ['model' => \common\models\ProductGroupModel::class, 'url_key' => 'group_id', 'action_key' => 'group',], ['model' => \common\models\ProductModel::class, 'url_key' => 'product_id', 'action_key' => 'product',], ] ], ], ], //... ],
View
<a href="Url::toRoute(['/product/show', 'group_id' => $group, 'product_id' => $product->id])">Go to product</a>
example url: https://tise/product/show/water/colla
water = group seo url
colla = product seo url
Action from Product Controller
public function actionShow($group, $product) { //... }
统计信息
- 总下载量: 323
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: BSD-4-Clause
- 更新时间: 2017-05-30