supplyhog / yii2-slug-parser
Requires
- php: >=5.4.0
- yiisoft/yii2: *
This package is not auto-updated.
Last update: 2024-09-26 00:30:36 UTC
README
#yii2-slug-parser
##安装 通过 composer 安装此扩展。将以下行添加到您项目的 composer.json 中
supplyhog/yii2-slug-parser” : “dev-master”
##它做什么?
允许您通过接口定义别名(允许使用通用的别名表),以便 url.com/post-title-slug 可以正确解析为 /post/view?id=23。使用 SecondSlugInterface
,您还可以有第二个别名 /john-doe/post-title-slug 解析为 /post/view?id=23,而 /sarah-jane/post-title-slug 解析为 /post/view?id=47。如果使用 SimpleSlugUrlRule
,它将处理表上的别名。
此外,别名可以附加动作和参数到友好的URL中,如示例所示。
###示例URL及其解析方式
- /post-title-slug -> /post/view?id=23
- /john-doe -> /author/view?id=51
- (^ 注意 您可以使用多个规则级联到另一个模型)
- /john-doe/draft/2 -> /author/view?id=51&draft=2
- (^ 注意 别名解析后的对会转换为查询参数)
- /post-title-slug/edit -> /post/edit?id=23
- (^ 注意 别名后的单个URL部分可以是不同的动作)
- /post-title-slug/edit?draft=1 -> /post/edit?id=23&draft=1
- /post-title-slug/edit/draft/1 -> /post/edit?id=23&draft=1
- (^ 注意 别名后的单个URL部分可以是不同的动作,并且仍然使用对作为查询参数)
- /john-doe/post-title-slug -> /post/view?id=23
- /sarah-jane/post-title-slug -> /post/view?id=47
- (^ 注意 使用二级别名,只要第一个别名对第二个模型是唯一的,它们可以相同)
- /john-doe/tag-slug -> /post/tag?id=82
- (^ 注意 您可以使用多个规则级联到另一个模型)
- /john-doe/post-title-slug/edit?draft=1 -> /post/edit?id=23&draft=1
- /john-doe/post-title-slug/edit/draft/1 -> /post/edit?id=23&draft=1
##它不做什么?
- 它不会为您创建URL。
- 它没有为您的slug模型,只有一个接口。
- 它不会做早餐。
为什么它不处理URL创建?这样,别名可以作为永久链接,允许多个别名指向同一个解析后的URL。
##设置
###配置
//add url rules in config/main.php for the appropriate application $config[‘urlManager’]['rules'][] = [ 'class' => 'supplyhog\SlugParser\CompositeSlugUrlRule', //'minLength' => 8 //Set the minimum require length of the slug default 8 'slugClass' => 'common\models\Slug', //The class that impliments the SlugInterface 'rules' => [ //Example Rule for a top level slug // http://site.com/post-title-slug [ 'class' => 'supplyhog\SlugParser\SlugUrlRule', 'modelClass' => 'common\models\Post', //The class for the slug 'controller' => 'frontend\controllers\PostController', //The controller the slug is pointing at 'route' => 'post', //The non-slug route //'defaultAction' = 'view', //The default action if nothing else is used. ], //Example Rule for a second-level slug // http://site.com/author-slug/post-title-slug [ 'class' => 'supplyhog\SlugParser\SecondSlugUrlRule', 'secondModelClass' => 'common\models\Post', //The class of second slug (post-title-slug) 'controller' => 'frontend\controllers\PostController', //The controller for the view 'firstModelClass' => 'common\models\Author', //This is the class for the 'route' => 'post', //The non-slug route //'defaultAction' = 'view', //The action used. ], //Additional example Rule for a top level slug that you might use in conjunction with the previous one // http://site.com/author-slug [ 'class' => 'supplyhog\SlugParser\SlugUrlRule', 'model' => 'common\models\Author', //The class for the slug 'controller' => 'frontend\controllers\AuthorController', //The controller the slug is pointing at 'route' => 'author', //The non-slug route ], ] ]; //SimpleSlugUrlRule For when the slug is not in a separate table $config[‘urlManager’]['rules'][] = [ 'class' => 'supplyhog\SlugParser\SimpleSlugUrlRule', //'minLength' => 8 //Set the minimum require length of the slug default 8 'modelClass' => 'common\models\Post', //The class that has the slug field 'controller' => 'frontend\controllers\PostController', //The controller the slug is pointing at 'route' => 'post', //The non-slug route //'defaultAction' = 'view', //The default action if nothing else is used. 'modelField' => 'slug', //The permalink/slug field on the model that can be found using ::find()->andWhere([modelField => slugValue]) 'modelKey' => 'id', //The field on the model that will be added to the params. Usually an id/primary key ];
###SlugInterface
建议的架构
- slug
- model_class
- model_key
- model_value
####public static function findBySlug($slug)
$slug
是第一个URL位置中的文本 返回 null 或实现 SlugInterface 的别名对象。
####public function getModelKey()
返回参数的键。所以如果正常URL是 /post/view?id=47,则应返回 "id"。
####public function getModelValue()
返回参数的值。所以如果正常URL是 /post/view?id=47,则应返回 47。
####public function getModelClass()
返回此别名对应的模型类。用于在规则链中验证正确的规则。
###SecondSlugInterface 扩展 SlugInterface
####public function getSecondSlugKeyValue($secondModelSlug, $secondModelClass)
$secondModelSlug
是第二个URL位置中的文本$secondModelClass
是指向的对象的类,用于验证多个规则 返回一个数组[key => value]
,该数组将被附加到查询参数。所以如果正常URL是 /post/view?id=47,则应返回[id => 47]
。