我创建了一个模块,里面有一个默认 Controller 。现在我可以访问默认 Controller 中的索引操作(默认操作),如/mymodule/。对于所有其他操作,我需要在 url 中指定 Controller ID,例如/mymodule/default/register/。我想知道是否可以从模块中的默认 Controller 的 url 中删除 Controller ID。
我需要像这样设置 url 规则:
before beautify : www.example.com/index.php?r=mymodule/default/action/
after beautify : www.example.com/mymodule/action/
注意:我希望这只发生在默认 Controller 上。
谢谢
最佳答案
这有点棘手,因为操作部分可能被视为 Controller ,或者您可能指向现有 Controller 。但是你可以通过使用 Custom URL Rule Class 来解决这个问题.这是一个示例(我测试过它,它似乎运行良好):
class CustomURLRule extends CBaseUrlRule
{
const MODULE = 'mymodule';
const DEFAULT_CONTROLLER = 'default';
public function parseUrl($manager, $request, $pathInfo, $rawPathInfo)
{
if (preg_match('%^(\w+)(/(\w+))?$%', $pathInfo, $matches)) {
// Make sure the url has 2 or more segments (e.g. mymodule/action)
// and the path is under our target module.
if (count($matches) != 4 || !isset($matches[1]) || !isset($matches[3]) || $matches[1] != self::MODULE)
return false;
// check first if the route already exists
if (($controller = Yii::app()->createController($pathInfo))) {
// Route exists, don't handle it since it is probably pointing to another controller
// besides the default.
return false;
} else {
// Route does not exist, return our new path using the default controller.
$path = $matches[1] . '/' . self::DEFAULT_CONTROLLER . '/' . $matches[3];
return $path;
}
}
return false;
}
public function createUrl($manager, $route, $params, $ampersand)
{
// @todo: implement
return false;
}
}
关于php - Yii - 从 URL 中删除模块的默认 Controller ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9496869/