CakePHP1.3上非模型请求的更好解决方案

发布于 2024-10-05 16:45:14 字数 273 浏览 1 评论 0原文

有一些参数无需中继模型。

Router::connect("/ctrl/action/:mode/:year"      ,
    array('controller' => 'ctrl', 'action' => 'action'),
    array('mode' => 'modea|modeb', 'year' => '[12][0-9]{3}'));

如何验证和清理?

我应该创建虚拟模型吗? 或在组件上实现?

there are some parameter withought relayting a model.

Router::connect("/ctrl/action/:mode/:year"      ,
    array('controller' => 'ctrl', 'action' => 'action'),
    array('mode' => 'modea|modeb', 'year' => '[12][0-9]{3}'));

How to validate and sanitize?

Should I create dummy model?
or Implement on a component?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独享拥抱 2024-10-12 16:45:15

好吧,您当前的路由与 /ctrl/action/modea/5010 不匹配,因此它将落入另一条路由,很可能是标准路由,该路由会将请求分派给 Ctrl_controller ::action('modea', 5010).即你基本上得到相同的结果。

最好的办法可能就是跳过这条路线,因为它实际上不会将任何东西路由到任何它不会去的地方。只需在使用值之前在控制器操作中进行验证:

function action($mode, $year) {
    if (!in_array($mode, array('modea', 'modeb')) || $year < 1900 || 3000 < $year) {
        $this->cakeError('error404');
    }
    ... business as usual ...
}

或:

function action($mode, $year) {
    switch ($mode) {
        case 'modea' :
            ...
            break;
        case 'modeb' :
            ...
            break;
        default :
            $this->cakeError('error404');
    }
}

或其某种组合。除非 $mode$year 变量与您的数据库/模型有任何关系,否则您不想为其创建模型。模型会进行验证,因为这是将内容放入数据库之前的重要步骤,但这并不意味着它们是应用程序中唯一可以或应该进行验证的部分。

Well, your current route wouldn't match /ctrl/action/modea/5010, so it would fall through to another route, most likely the standard route, which would dispatch the request to Ctrl_controller::action('modea', 5010). I.e. you basically have the same result.

The best thing to do is probably to just skip the route, since it doesn't really route anything anywhere it wouldn't go anyway. Just validate in your controller action before using the values:

function action($mode, $year) {
    if (!in_array($mode, array('modea', 'modeb')) || $year < 1900 || 3000 < $year) {
        $this->cakeError('error404');
    }
    ... business as usual ...
}

Or:

function action($mode, $year) {
    switch ($mode) {
        case 'modea' :
            ...
            break;
        case 'modeb' :
            ...
            break;
        default :
            $this->cakeError('error404');
    }
}

Or some combination thereof. Unless the $mode and $year variables have anything to do with your database/model, you do not want to make a model for it. Models do validation, because that's an important step before putting stuff into the database, but that doesn't mean they're the only part of the app that can or should do validation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文