我有一个名为article的模块。它存在于以下文件夹中:
- protected
- modules
- article
- controllers
ArticleController.php
- views
-article
articleModule.php
既然我在做CMS。我选择不使用到控制器或操作的直接链接。这意味着调用example.com/article/article/index。行不通的。因为CMS将url请求视为页面。(所以它寻找一个名为"index"的页面,它的父页是"article"answers"article"。此页面不存在。
CMS为一个有效的页面加载块,对于其中一个块,我想执行文章模块。我试着用
Yii::app()->runController($route).
然而,为此你需要一个路由。
是否有可能运行这样一个模块,使模块的工作方式与去url example.com/article/article相同?
如果不可能的话。我如何设置Url管理器,使其可以处理页面?干净的url。下面是一些例子:
example.com/join_us
example.com/join_us/contact
example.com/news/
example.com/countries/us/healthcare
以上是用户可以创建和更改的页面。我为此纠结了很长时间。通常这是件容易的事。但是Yii让它变得非常困难。主要是因为控制器或模块被链接到路由。
把这个放到你的主配置中:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'caseSensitive'=>false,
'rules'=>array(
'gii'=>'gii',
'<controller:'w+>/<id:'d+>'=>'<controller>/view',
'<controller:'w+>/<action:'w+>/<id:'d+>'=>'<controller>/<action>',
'<controller:'w+>/<action:'w+>'=>'<controller>/<action>',
'<action>'=>'site/<action>',
),
),
在你的.htaccess
文件中:
如果存在目录或文件,直接使用:
<>之前rewritecsecond %{REQUEST_FILENAME}rewritecsecond %{REQUEST_FILENAME}之前…否则转发给index.php
:
您可以将所有内容路由到article控制器。Yii按照从上到下的顺序使用规则,所以首先定义一些特定的规则,最后定义一些笼统的规则。这应该可以工作:
// Url manager rules
// Some of example non-article rules
'login' => 'userModule/user/login'
'logout' => 'userModule/user/login'
// Below regex rule catch any string and pass it to $_GET['ariticlePath']
// and executes action `article` of controller `article` of module `article`
'<ariticlePath:.+>' => 'article/article/article'
在文章动作中,你可以对通过的$_GET['ariticlePath']
做任何你想做的事情。
边注:为了避免内容的双重索引,我建议使用元canonical
,所以一些可能伪造的url与查询字符串或任何一些用户可能输入,将不会被视为重复的内容由谷歌。
EDIT:正如soju所建议的,articlePath
也可以作为动作参数绑定:
...
public function actionArticle($articlePath)
...