Codeigniter 我的辅助函数

发布于 2024-12-18 11:33:49 字数 382 浏览 2 评论 0原文

首先,很抱歉,如果这是一个愚蠢的问题,但我是初学者,我想听听更有经验的开发人员的意见。

我正在使用 codeigniter 构建一个房地产网站,并且我有城市名称,这些字符是重音字符,但由于搜索和其他验证,我在数据库中发送这些非重音字符。

因此,当我选择这些字符时,它们是非重音字符,我创建了一个帮助器来将这些字符转换为后重音字符。

我的问题是,如果我在我看来运行这个助手可以吗?

我的意思是,助手是自动加载的,因为我需要在 4 个视图文件中使用它,并且我在视图中像这样调用它的函数,

echo city_accent($e->city);

所以如果我在视图文件中像这样运行它,会出现问题吗?

谢谢

first of all sorry if its a stupid question but I'm a beginner, and I would like the opinion of a more experienced developer.

I'm building a real estate site with codeigniter, and I have city names what are accented characters, but I'm sending these in the database non accent because of the search, and because of the other validations.

So when I select these they are non accented chars, I created a helper what converts these back accented.

My question is, is it ok if I run this helper in my view?

I mean like, the helper is auto-loaded because I will need this in 4 view files, and I call its function like this in the view

echo city_accent($e->city);

so is it a problem if I run it like this in my view file?

Thank you

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

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

发布评论

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

评论(2

凉世弥音 2024-12-25 11:33:49

可以在视图中调用辅助函数,但根据约定更好的方法是在控制器中调用它并将其传递给视图表单控制器。
喜欢:

$data['someVar'] = city_accent($e->city);
$this->load->view("your_view", $data);

希望有帮助

Calling a helper function in view can be done, but better way as per the convention would be to call it in controller and pass it to view form controller.
like:

$data['someVar'] = city_accent($e->city);
$this->load->view("your_view", $data);

Hope it helps

猫七 2024-12-25 11:33:49

从技术上讲,你可以做到这一点。只要该助手加载到调用视图的控制器中即可。

不过我不会把它放在视图中。在MVC环境中布局和逻辑需要分离。我将在控制器中使用 city_accent() 函数,并通过参数将结果传递到视图中。

Technically, you can do that. As long as that helper is loaded in the Controller that calls the View.

I wouldn't put it in the View however. Layout and logic need to be separated in an MVC environment. I would use the city_accent() function in my Controller and pass the result into the View by parameter.

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