Yii2 url规则为多个控制器使用slug,例如domain.com/slug

Yii2 url规则为多个控制器使用slug,例如domain.com/slug,yii2,url-routing,Yii2,Url Routing,我想为如下内容设置url规则:www.domain.com/slug-in-url用于多个控制器。我有一个模型“Content”,在“Content”模型中有一个名为Content type的列。例如,content\u type=1表示竞赛,content\u type=2表示发布,content\u type=3表示产品等。我已经为content\u type、contestController、postController和productController中的每一个创建了一个控制器 我想

我想为如下内容设置url规则:www.domain.com/slug-in-url用于多个控制器。我有一个模型“Content”,在“Content”模型中有一个名为Content type的列。例如,content\u type=1表示竞赛,content\u type=2表示发布,content\u type=3表示产品等。我已经为content\u type、contestController、postController和productController中的每一个创建了一个控制器

我想知道是否有一种方法可以将几个不同控制器的URL保持为以下格式:

'<slug:.*?>'=>'contest/view', // content type contest - contestController
'<slug:.*?>'=>'post/view', // content type post - postController
'<slug:.*?>'=>'product/view', // content type product - productController
'=>“竞赛/视图”,//内容类型竞赛-竞赛控制器
''=>'post/view',//内容类型post-postController
''=>'产品/视图',//内容类型产品-产品控制器

根据yii文档,它只适用于上述一条规则。

不是直接答案,但您是否考虑过其他方法

  • 将所有内容发送到1控制器

    '=>'post/view',

    • 在前端部件上,用户将不知道实际使用的是哪个控制器,但您可以在通过slug找到模型后检测类型
  • 按控制器拆分路由器

    'post/'=>'post/view',

    • 您应该知道,当放置指向该页面的链接时,使用的是哪种类型
  • 按类型拆分路由器标志(未测试)

    '/post'=>'post/view',

    ”/product'=>“产品/视图”,


  • 除此之外,我不知道它是如何工作的。

    谢谢@Ripper,我确实尝试了这两种方法,它们都很好,我希望有任何其他方法来解决它,只使用slug