开发者

CakePHP a class that can be used anywhere?

开发者 https://www.devze.com 2023-03-02 04:45 出处:网络
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.

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 a开发者_如何学编程ll 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?


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', 'joe@acme.com');

You can then get this info anywhere you need using

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


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.


You can access model classes from any place this way : $companyInfoModel = ClassRegistry::init('CompanyInfo'); $phone = $companyInfoModel->phone();


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消