CakePHP1.3上非模型请求的更好解决方案
有一些参数无需中继模型。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,您当前的路由与
/ctrl/action/modea/5010
不匹配,因此它将落入另一条路由,很可能是标准路由,该路由会将请求分派给Ctrl_controller ::action('modea', 5010)
.即你基本上得到相同的结果。最好的办法可能就是跳过这条路线,因为它实际上不会将任何东西路由到任何它不会去的地方。只需在使用值之前在控制器操作中进行验证:
或:
或其某种组合。除非
$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 toCtrl_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:
Or:
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.