CakePHP 是一个可以在任何地方使用的类吗?

发布于 2024-11-03 19:47:21 字数 492 浏览 1 评论 0原文

我有一个多域网站。根据域的不同,站点需要采取相应的行为。

我创建了一个名为 CompanyInfo 的帮助程序,它具有 name()phone()email() 等方法等等。

根据您所在的域,它会返回正确的信息。

因此,例如,如果我需要显示用户拨打的电话号码,我将使用 $this->CompanyInfo->phone() ,它将为用户显示正确的电话号码,具体取决于在域上。

好吧,这一切都很好,但并不真正相关。真正的问题是,我需要的不仅仅是视图中的这些信息。不过,助手只是为了观看。如果我想从控制器访问此信息,我需要创建一个组件来执行此操作。

我真的不想让助手和组件做同样的事情。我宁愿让一个类来处理它,而不是复制和粘贴逻辑。

那么,拥有一个可以从控制器、视图甚至模型访问的方法的类的最佳方式是什么?

I have a multi-domain site. Depending on the domain the site needs to behave accordingly.

I created a helper called CompanyInfo it has methods such as name(), phone(), email(), etc.

Depending on what domain you are on it returns the correct information.

So for example if I need to display the phone number for a user to call I would use $this->CompanyInfo->phone() and it will display the correct phone number for the user depending on the domain.

Ok, this is all good, but not really relevant. The real issue is, I need this information in more than just the view. Helpers are just for views though. If I want to access this information from a controller I need to create a component to do it.

I really don't want to have a Helper and a Component doing the same thing. I would rather have one class handle it rather than copy and paste logic.

So whats the best way to have a class with methods that can be accessed from the controller or view or even model?

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

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

发布评论

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

评论(4

§普罗旺斯的薰衣草 2024-11-10 19:47:21

您需要显示的只是这种静态信息(姓名、电话号码、电子邮件等)吗?为什么不将它们添加到 core.php 的配置中呢?

然后

# in core.php
Configuration::write('Company.name', 'Acme Corp.');
Configuration::write('Company.email', '[email protected]');

您可以在任何需要的地方使用此信息

Configuration::read('Company.name');

Is it just this kind of static information (name, phonenumber, email, etc) what you need to display? Why not just add them to your configuration in core.php?

Something like

# in core.php
Configuration::write('Company.name', 'Acme Corp.');
Configuration::write('Company.email', '[email protected]');

You can then get this info anywhere you need using

Configuration::read('Company.name');
韬韬不绝 2024-11-10 19:47:21

您可以在 app_controller 中定义此变量,然后在任何控制器中轻松使用这些变量,因为仅从那里设置这些变量。
在您的构造类中调用此函数。
我认为这会解决你的问题。

You can define this variable in your app_controller and then use these variable easily in any of your controller as set these variables from there only.
Call this function in your construct class.
i think that will solve your problem.

-柠檬树下少年和吉他 2024-11-10 19:47:21

您可以通过以下方式从任何地方访问模型类:
<代码>
$companyInfoModel = ClassRegistry::init('CompanyInfo');
$phone = $companyInfoModel->phone();

You can access model classes from any place this way :

$companyInfoModel = ClassRegistry::init('CompanyInfo');
$phone = $companyInfoModel->phone();

一身软味 2024-11-10 19:47:21

a) 您可以使用 cake1.3 中的库

b) 可以将内容传递给的静态模型方法,该方法将返回预期值

echo Model::phone($data)

a) you can use libs in cake1.3 for that

b) static model methods which you can pass the content to and which will return the expected value

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