Currently i am using something like this:
//at bootstrap.php file
Configure::write('from', 'mymail#mydomain.com')
//at controllers or models files
$var = Configure::read('from')
The thing is, i would like to manage that variable through the database to be able to modify it in a simpler way.
I was thinking about doing it with AppModel but then it would only be accessible for Models and not controllers.
What should I do in this case?
Thanks.
You can create a separate model / plugin which will be mapped to a configuration table in your database. Then load it through $uses statement for controllers and App::import() for models.
class SystemSetting extends AppModel {
/**
* Return a list of all settings
*
* #access public
* #return array
*/
public function getSettings() {
return $this->find('all');
}
}
Then, in your controller:
class SomeController extends AppController {
var $uses = array('SystemSetting');
public function displaySettings() {
$settings = $this->SystemSetting->getSettings();
// .. your code
}
}
In model:
App::import('Model', 'SystemSettings.SystemSetting');
$settings = new SystemSetting();
$mySettings = $settings->getSettings();
This works just fine. Of course, you might also load settings in both AppController and AppModel to follow the DRY rule.
create the getSettings in your AppModel
in AppController you can write this method:
public function getSettings() {
return $this->{$this->modelClass}->getSettings();
}
this way the getSettings() method is available in any model and any controller
any model call:
$mysettings = $this->getSettings();
any controller call:
$mysettings = $this->MODELNAME->getSettings();
Related
I have queried on the User table inside the AppController as below
<?php
class AppController extends Controller {
public function beforeFilter() {
function beforeFilter() {
parent::beforeFilter();
if ($this->Session->read('Auth.User.id')) {
$userLoginInfo = $this->User->findByUserId($this->Session->read('Auth.User.id'));
$this->set('userLoginInfo', !empty($userLoginInfo) ? $userLoginInfo : NULL);
}
}
}
}
?>
The $userLoginInfo is available in all ctp files, but I want to access it in all other controller actions as well.
now you put this code in AppController
function beforeFilter(){
$this->set(‘accesstest’ , ‘abc’);
}
And We have to use it in Other controller file say anotherController.php
then we will use $this->viewVars.
here we will be used
$test = $this->viewVars[‘accesstest’];
$this->set('test',$test);
Your own answer is applicable if wanting to set a variable for all Views, but this was not what you were asking in your question.
If you are extending AppController correctly then you can create a property of the AppController class that would then be accessible from any controller that extends it:-
class AppController extends Controller {
public $accesstest = 'abc';
}
Then in any controller that extends AppController you can use:-
$test = $this->accesstest;
echo $test; // 'abc'
However, if you want to share a variable that you want accessible from all controllers that can be changed and you want the change remembering then use the Session:-
$this->Session->write('accesstest', 'abc');
$test = $this->Session->read('accesstest');
$userLoginInfo = $this->viewVars['userLoginInfo'];
My Code tries to fetch all Main categories of the posts into Appcontroller to show on the homepage:
namespace App\Controller;
use Cake\Core\App;
use Cake\Controller\Controller;
class AppController extends Controller
{
public $helpers = ['Html', 'Form', 'Session','Time','Tree'];
public function initialize()
{
parent::initialize();
$this->loadComponent('Flash');
$this->maincategories();
}
function maincategories(){
App::import('Controller','Postcategories');
$postcates = new PostcategoriesController;
$postcates = $postcategory->find('threaded');
}
}
Your maincategories() method is wrong. You need the model, not the controller to retrieve the data from. You need to use TableRegistry::get('Postcategories') to get the Postcategories model and then call the find on that:-
public function maincategories()
{
$Postcategories = TableRegistry::get('Postcategories');
$this->set('postcategories', $Postcategories->find('threaded'));
}
$this->set() is setting the categories as a view variable ($postcategories). You will need to make sure you include use Cake\ORM\TableRegistry; at the top of your AppController file.
Make sure you've fully read the docs on retrieving data.
Im trying to load one appmodel to many controllers. For example i have appmodel called Item and i want to get some items in HomeController and ContactController. When i execute this code:
class HomeController extends AppController {
public function index() {
$items = $this->Item->find('all');
$this->set('items', $items);
}
}
I got this error:
Call to a member function find() on a non-object
How can i get thing from database in many views in CakePHP?
you need to declare which model you want to use in your controller.
class HomesController extends AppController {
var $uses = array('Item', 'AnotherModel');
...
}
var $uses=array('Item');
If using this model in multiple controllers, put this in app controller.
Hye. I want to make a controller and model for a table which is named in uppercase and abbreviation.
For example, the table name is PS_DEPT_TBL
1) What would be the controller file name? Is it PSDEPTTBL_controller.php? The code below seems to be not working for the controller.
class PSDEPTTBLController extends AppController {
var $uses = 'PS_DEPT_TBL';
var $scaffold ;
}
2) I name the model file as PSDEPTTBL.php and code it as below.
class PSDEPTTBL extends AppModel {
var $useTable = 'PS_DEPT_TBL';
}
But the error shows that there isn't any controller of the table. I'm new to cakephp. Help me.
There is no need for your controller to be called the same as your table. The same goes for your model.
How about something this:
// ThingsController.php
class ThingsController extends AppController
{
var $uses = array('Thing');
}
// Thing.php
class Thing extends AppModel
{
var $useTable = 'PS_DEPT_TBL';
}
// Config/routes.php
Router::connect(
'/PSDEPTTBL/:action/*', array('controller' => 'things')
);
The key idea here is to make your code readable. If you have a legacy database, the best thing you can do for yourself is to hide it all in all the models.
I want to get my session variables in my model. Im not using Auth component. Is there any alternate way ?
Thanks
Binoy
You would need to use the Session helper to do,
$this->Session->write('key','value');
But as the comment states, you'll be wanting to set a variable in your model and then use the session to write the same value into that variable in the model, rather than accessing the session actually in the model.
Class MyModel Extends AppModel{
var $username;
var $password;
}
Then in your controller you could use something along the lines of,
$this->MyModel->username = $this->Session->read('User.id');
$this->MyModel->password = $this->Session->read('User.password');
Controller File : use Session helper
class AppController extends Controller {
var $helpers = array('Session');
function index()
{
$this->Session->write('answer', '10'); //$this->Session->write('key','value');
}
....
}
Now you can access this varibale in your model :
class AppModel extends Model {
function validateanswer(){
CakeSession::read('answer');/*Here u can get value of varibale answer which you set through controller*/
}
}