I have just started to learn Yii, where I have created one PostController Controller. In this controller I am having one requirement of using Sessions.
So I have created one constructor method and its code is as follows
public $session;
public function __construct() {
$this->session = new CHttpSession;
$this->session->open();
}
But after creating this constructor the controller was not working and gives error. And after deleting this code my controller was working perfectly. I have written this code inside constructor to not initialize the Session in each method for actionCreate and actionUpdate.
So my question is how can we create constructor in Yii?
Thanks
You simply forgot to call parent constructor :
public function __construct()
{
.....
parent::__construct();
}
You could use beforeAction instead of overriding __construct.
And Sergey is right, by default Yii will start session (autoStart), you just have to use Yii::app()->session, e.g. :
Yii::app()->session['var'] = 'value';
public function __construct()
{
parent::__construct($this->id, $this->module);
}
I use init() for that, but found what people think __construct is better.
Related
I need to be able to run a child method from the parent class.
I am calling the child function $this->getPlan, but it is not calling the child plan, but only the parent one. I thought an extended class over-rides the function.
Here is my code
class logon{
public function getPlan(){}
public function __construct(){
}
public function checkWorks(){
$this->getPlan();
}
}
class plan extends logon{
function __construct(){
parent::__construct();
}
public function getPlan(){
echo "test";
}
}
$logon = new logon();
$plan = new plan();
$logon->checkWorks();
However if I call the code from the parent constructor like this, it calls my child method.
public function __construct(){
$this->getPlan();
}
I don't want to use an abstract class to do this, as there is a good reason for this.
Can anyone tell me why my parent method is not being over-ridden by the child class?
You are calling logon method:
$logon->checkWorks();
but getPlan in logon class is empty. You need to call child class (Plan):
$plan->checkWorks();
I have solved the problem a different way.
The initial problem was this. I needed to include logon class in plan so that I could access the database. The problem was that logon needed to run a method in plan, and I couldn't include the plan class as that would have created a cyclic problem.
My solution was halfway there, but I have a proper solution. By making the plan class a child solved the problem of not including logon to access the database, but I still could call a method in plan. Now I am not including logon in plan I can include plan in logon which has solved the problem, and it works. Here is my solution. Now I can access my method from both classes. I knew I would find a way if I just tried hard enough.
class logon{
public function __construct(){
}
public function checkWorks(){
$p= new plan();
$p->getPlan();
}
}
class plan extends logon{
public function __construct(){
parent::__construct();
}
public function getPlan(){
echo "test";
}
}
$logon = new logon();
$logon->checkWorks();
There's a part of a code (query) that will be required in all controllers, they will be passed into views for display.
Can I know is there anyway to declare them in just a single file so that I can reference them directly from my view? Without declaring them in each controller's _construct.
I'm using codeigniter3, here's a sample code:
MainController.php
public function index(){
$data['userCampaign'] = $this->Usermodel->getCampaign();
}
Create default controller in your project which extends CI_Controller and your all controller extends new controller and in __construct(); function of your new controller you can add this code.
Declare this function as protected in parent controller class.
No. I don't know there is method like that.
You want call the function in __construct() or you have to declare function in controller and call it back. $this->check_session()
So for example, i want to access config on CI from my class library.
Class A {
funcion x() {
$this->config->load('my_config'); // accessing my_config
}
}
that obviously won't work unless you extends and then call parent::__construct().
As far as i know, it can only be done from classes that extend CI_Controller or CI_Model. How to access CI stuff (config, helper, model, etc) on non-CI class ?
Thanks.
How about initiating the class from the construct?
include ("CI_Page.php");
class A {
protected $_CIInstance1;
protected $_CIInstance2;
public function __construct(){
$this->_CIInstance1 = new xxxx();
$this->_CIInstance2 = new yyyy();
}
public function x(){
$this->_CIInstance1->load('my_config');
}
}
You probably want to access the instance of CI as if it were the super variable, $this. In reality what you need is the ability to access the same functionality as $this and in order to do this, you'll need to use $CI =& get_instance();
You can find it directly in the documentation for Creating Libraries
I have been using CI for two years now. One thing that really annoys me is the use of &get_instance(). Although it is halpful while we are inside library , helper , presenters , model etc. But everytime loading it is cumborsome. If you forget loading it somewhere and simply use $this->blah->blah() instead of $CI->blah->blah() this makes too much trouble and if you are working online you face the client who is complaining that he sees the error. I have seen in the laravel that you does not need to load the instance anywhere throughout the application. This is because laravel is autoloading all the libraries and models and both are available anywhere in the application. But this seems to me disadvantage why loading classes that are not required in some particular places. This tells me Codeigniter is flexible but still i want an alternative where i dont want to use &get_instance(). Any idea or suggestion ? Please.
In your model or Core model or library
//class MY_Model extends CI_Model
//class SomeLibrary
class Some_model extends CI_Model {
private $_CI;
public function __construct() {
parent::__construct(); //for model or core model
$this->_CI =& get_instance();
}
//if you called attributs who does not exist in that class or parent class
public function __get($key)
{
return $this->_CI->$key;
}
//if you called methods who does not exist in that class or parent class
public function __call($method, $arguments)
{
call_user_func_array(array($this->_CI, $method), $arguments );
}
public function test() {
var_dump($this->some_controller_key);
var_dump($this->some_lib_loaded);
}
}
*NOT TESTED YET
Inspired by an piece of code from the awesome Flexi Auth
//from a Model to keep access of CI_Controller attributs
public function &__get($key)
{
$CI =& get_instance();
return $CI->$key;
}
I was shocked when I saw that ^^
To explain the &__get, i think when you will call this magic method a second time PHP will do not execute it again, but will take his result from the first call.
I am having trouble trying to call CodeIgniter methods in my static functions, just using $this doesn't work because its not in object context, the static keyword doesn't work either. This an example of the code in my core model, the $table variable is successfully defined from another model like posts.
class MY_Model extends CI_Model {
protected static $table;
public function __construct() {
parent::__construct();
}
public static function find_all() {
$this->db->select('*');
$sql = $this->db->get(static::$table);
return $sql->result();
}
}
If $this doesn't work you can get around this like this:
$CI =& get_instance();
$CI->db->...
The codeigniter built in loader class automatically instantiates the class. There is no support to use classes without instantiating. You can manually include your file in the model file then you can use it. For more details check out this thread:
http://codeigniter.com/forums/viewthread/73583/
What you want is a refence to the static var in the class, so use:
class MY_Model extends CI_Model {
protected static $table;
public function __construct() {
parent::__construct();
}
public static function find_all() {
$this->db->select('*');
$sql = $this->db->get(self::$table);
return $sql->result();
}
}
And, of course, $table does need to have a value!
Codeigniter doesn't generally support static methods I believe and they are really an attempt to shoehorn procedural code into object oriented code.
Anyway, I think your best bet is to either use the class without static methods, or turn your code into a "helper". A helper is just an old school function library. You would create it under the helpers folder and it can be loaded with $this->load->helper('helper_name'). You can then call the function as in normal procedural code, in other words 'find_all()'
Hope this helps.
First time contributor :)