I want to store global settings in the database, that then will be editable from cPanel. I have a model Setting, which has only 1 item with multiple columns such as siteName, language, siteDescription and etc. I want to make these accessible in every view, so do I need to do
$settings = \MyApp\Setting::find(1);
for every single controller? Is there a way to define this variable somewhere globally and have it accessible in every single controller / view ?
Or perhaps this is not the correct way to do it?
May be it's better to write that code snippet into BaseController.
or
Create a trait and write into it. use that trait in anywhere you want.
Related
I am working on a fairly large CodeIgniter project. There will be more pages than I can count. Right now, I have a few different headers, depending on what page the user is visiting and also depending on if the user is logged in. For example, if the user is not logged in (no session variable stored) then I want to display header1 which shows some basic stuff like "Signup, Login". If the user IS already logged in, I want to show different menu items like "logout". That's a very basic example. The changes can be pretty extensive, so it wouldn't make sense for me to have one header file with control flow logic in it.
I'm looking for a way to include the appropriate header without having to write $this->load->view('header') in every method I have in my controllers. It looks like another option is to write that line in every view that I have. To make this dynamic I created a MY_Controller file and included something like this in it:
public function get_header()
{
if ($this->session->userdata('user_id')) {
$this->load->view('headers/logged-header');
} else {
$this->load->view('headers/header-home');
}
}
Then I include <?php MY_CONTROLLER::get_header(); ?> in places that I need it (i.e inside methods or views.)
Is there a way to "hook" this in somehow, where I can simply write this code once, and then the application process it every time it loads one of my methods that call up a page?
This is a very typical need that could be easily handled by using the OOP, which CI is using in all of its controllers.
Since all of your self-defined controllers must inherit from CI_Controller, you can just write a base controller(class) that inherits from CI_Controller, which do some very basic things like :
do some basic operation in its __construct that all of its subclass may need(e.g. auto login logic, and you can put get_header here)
provide methods that all of its subclass may need like right check, pagination widget generator...
Probably the easiest way would be to call that function in the constructor method of all your controllers. That way its called every time for every method in the class. Just make sure you call the parent constructor first.
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 created a page using the MVC structure called 'sections' ( view is located in the app/views/sections folder, model in the model folder and the controller in the controller folder) when i request the variable $test, it works fine without any errors..
When i want to request this variable in my home.ctp, it provides me with an error, saying that the variable is undefined..
Is there any way in cakePHP to request this variable on any page you want it to?
Thnx in advance!
In the MVC stack, you need to set variables with data in your controller, and then pass them out to your view.
So in your example, you'll want to $this->set('myvar',$item); in your SectionsController, then in your view, you will be able to echo $myvar.
Be sure to set this in the home() method of your Sections controller, otherwise it won't be available in your home view.
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.
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')));
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