CakePHP 是一个可以在任何地方使用的类吗?
我有一个多域网站。根据域的不同,站点需要采取相应的行为。
我创建了一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要显示的只是这种静态信息(姓名、电话号码、电子邮件等)吗?为什么不将它们添加到 core.php 的配置中呢?
然后
您可以在任何需要的地方使用此信息
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
You can then get this info anywhere you need using
您可以在 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.
您可以通过以下方式从任何地方访问模型类:
<代码>
$companyInfoModel = ClassRegistry::init('CompanyInfo');
$phone = $companyInfoModel->phone();
You can access model classes from any place this way :
$companyInfoModel = ClassRegistry::init('CompanyInfo');
$phone = $companyInfoModel->phone();
a) 您可以使用 cake1.3 中的库
b) 可以将内容传递给的静态模型方法,该方法将返回预期值
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