php - Yii - 从 URL 中删除模块的默认 Controller ID

标签 php url yii

我创建了一个模块,里面有一个默认 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/

相关文章:

python - 确定转发的 URL

java - 如何在 java 中使用代理获取 URL 连接?

url - 从 CakePHP 2.1.2 控制台 Shell 创建完整 url

javascript - 使用 jQuery 和 PHP 加载语言环境变量

php - 如何在 Yii2 中使用 Yii::$app->security->encryptByKey() 保存加密数据

php - 在 Laravel 中创建一个多语言站点

php - 将动态数据传递给javascript

php - Yii 中 registerCssFile 的正确位置是什么?

php - Yii clean url 带有 id 和 name

php - 从我的 Drupal 获取所有分类法