I wish to load some global defaults in my CakePHP application. E.g. app/config/defaults.php
$config['site']['name']='FooBar';
$config['secuity']['somelimit']=-1;
I'm going to use (Configuration::read()) these values across various classes not just in a single controller.
Where is the proper place to call Configuration::load('defaults')? In the router? In the subclass of App?
To clarify my objective: I would like Configuration::load('defaults') for loading global defaults. But I would not like to do this in every Router, Helper etc. Is there a global hook/constructor for CakePHP applications that is executed before anything else?
I put that call into app/config/config.php
Related
I want to add implementation of array_column() for CakePHP app that is currently running on PHP 5.3 (array_column() has been introduced in PHP 5.5). What would be the best method of doing it without touching core files?
Please note that I don't want to use helper, I really want to define global function.
If you don't want to write a helper there are a few places where you can put global functions. If you're going to use the functions only in your controllers the best place is the AppController. In a similar way you could use the AppModel class for model functions and for the views you could put them within a layout.
But if you want to use the functions across the entire application, I would recommend putting them inside the app/Config/bootstrap.php file.
I am trying to use a pre_controller hook in Codeigniter to define commonly used variables throughout the stuff I am building. However I am trying to figure out if a controller is my best choice or should I use something else.
What I need is something that can access the DB, Sessions, Etc yet pass this information down to a view. I also need to in various controllers override a common default from these common variables.
Ideas?
Overall I am thinking controller level so I can play with $this->data[] entries. But I don't think it being a precontroller I can override that in another controller.
You can set variables into $this->config object
I have a several functions which are quite large, and are only used in one controller function each, and I'm wondering where to put these? They are not displaying any views, but instead crunching some numbers.
If I'm not wrong, there are 4 possible places where i could put these function: in my controller, in a helper, in a library or in a model. But none of these seem appropriate, since I don't want the code to be loaded every time a user uses the controller, and model should be used to do database stuff, and helpers and libraries should contain code that can be used over and over again.
If it is business logic, the best place to put it is in the controller as a private method, then you can call that method from within the controller.
Just as a note, helpers aren't always loaded unless you autoload them or load them in the constructor of your controller. So, as an alternative, you can make these methods of a helper then just load the helper in the controller action you wish to use them. That way they only get loaded when you need them.
CodeIgniter comes with helpers that you probably might not use (doesn't load unless you specify it in the application/config/config.php file) and I don't think its a problem having functions that you only use once stored there (application/helpers ). For example I might use a random password generator once only, but its still there and won't be loaded unless I call it.
$this->load->helper('my_string_generators');
When trying to adhere to established best practices like avoiding singletons, registry, static properties and base controllers, how I can populate my layout(and partials used by the layout) with data that is only used by the layout and common across all actions?
The typical scenario is menu which is built on variable data, like an database. Keeping separation of concerns in mind, the views/layout should never talk to an backend directly, but rather be told what to contain.
Using a front controller plugin is simply not possible without using the singleton "feature" in Zend_Layout. The plugin only knows the request and response object, neither has access to controllers, views or the layout.
Zend's action helpers have init/preDispatch/postDispatch methods. One can add action helpers to the HelperBroker (ex. using the bootstrap) and these will be executed in the normal application flow.
Using the init-method to inject data into the view is not possible since it's trigged before the controller/view is ready. preDispatch/postDispatch is possible, but not perfect, since these methods are always triggered when executing a controller action.
That means that all uses of the Zend_Controller_Action::_forward() will also execute the preDispatch/postDispatch in all action helpers. This doesn't have any big implications for the code except speed, I really do not want to be setting a view (or an view helper) variable several times. One can code around this issue using some sort of $firstRun variable, but I really don't want to track this in my own code.
Another method is doing this in the bootstrap, but in my opinion it really does not belong there.
So, how can I populate my layout/view helper with data from the database, doing it only once and still keep a good separation of concerns?
I've followed the ActionHelper approach, setting the view variables on the preDispatch method. It was the most logical place, in my opinion. In addition, my projects weren't using Zend_Controller_Action::_forward(), so I didn't have any speed concerns about multiple triggering of the helper.
Hope that helps,
You could go with Action Helpers as stated in this answer (take a look at the question too, its pretty much similar to yours)
I'd use action helpers too. Just fill the view variables and display them using plain PHP/partials/render/viewHelper (depending on complexity).
Your problem with multiple runs using preDispatch could be solved using
if (!($this->view->myVar)) { // or array_key_exist or isset - depending on your use case
$this->view->myVar = $someModel->getSomeData();
}
Which is just fine IMO.
I'm working on a web application, using the CAKEPHP framework. Herefor i need to request one variable on multiple pages (all pages have different controllers). it is oubvious that i get a error on several pages, since the variable isn't declared in all the different controllers.
Is there a workaround for this? i've already tried the app:: import to import a controller in another controller, but this doens't seem to work (still get a undefined variable error).
Thnx for your cooperation!
Regards,
Simon
Duplicate question, but I think it's phrased a bit better so I'll paste my answer here:
Standing on the shoulders of deceze's comment and DavidYell's answer, I think they've managed to scratch out a decent view of what you're trying to get to. Maybe. So with that loose understanding of what you're seeing and what you have...
By default, the PagesController::display() method generates the homepage view (home.ctp). I suspect that this is what you're talking about. That said, the variable you're setting in a method of your SectionsController won't be available to your homepage which is created by a different method in a different controller. If you want a variable available to all views there are several things you can do:
You can set the variable in your config/core.php file (not generally recommended)
You can set it in config/bootstrap.php if it's a constant. By that, I mean that it's a value you're going to hard code, not something dynamically generated. Whether you create the variable as a constant doesn't matter.
You can set in in your AppController in a beforeFilter() or beforeRender() method. All of your custom controllers (assuming you've followed protocol) inherit from the AppController. If you choose this path, make a copy of cake/libs/controller/app_controller.php and place it in your app/ directory.
Those are the ways that I think will best meet your needs as I understand them.
You can use the Configure.write... more info here
More on configure class
One way to make sure that the variable is available on all pages is to define it on the front controller (normally index.php) or any other always included file (such as global configs), another option might be to use the $_SESSION super global.
You can youse the beforeRender() or beforeFilter() callback methods from your AppController. :)
These'll be called on every page request. :)
If you to access a value from different controllers, you will need to save that value into a database record so it can be accessed by the different controller methods. Each controller call exists in its own context and can only share data that is stored external to the scripts.
In situations like this, I've created a preferences table (with fields like, id, name and value). Then add a $uses value to the app_controller to make it available to all controllers. Finally, just grab it with a find call. (ie. $foo = $this->Preferences->find( 'first', array('conditions'=>array('name'='foo')));