Welcome Guest, Not a member yet? Register   Sign In
Need urgent help - Common method for all controller
#1

[eluser]webdevron[/eluser]
I'm using CodeIgniter and want to store each page URL when a visitor visits them.
For example - if anybody visits to http://mysite.com/bolg then, I need to store "Blog". Again if anybody visits to http://mysite.com/contact, then I'll store "Contact". I have a method to do that.
But my problem is where to put the method & how to call it in each and every controller when loading view?
Please help me ASAP. Thanks in advance.
#2

[eluser]CroNiX[/eluser]
Create a MY_Controller, which is a base controller. This special controller will be called immediately before your normal controllers that extend it. All of your normal controllers that you want the logging enabled would extend MY_Controller instead of CI_Controller.
http://ellislab.com/codeigniter/user-gui...asses.html

/application/core/MY_Controller.php
Code:
class MY_Controller extends CI_Controller {
  pubic function __construct()
  {
    parent::__construct();

    $this->log_uri();
  }

  public function log_uri()
  {
    //your logging code here
  }
}

Then your other controllers that you want to do the automatic logging:

Code:
class Other_controller extends MY_Controller {
  public function index()
  {
    //regular stuff
  }
}

If you don't want something to be logged, then have that controller extend CI_Controller like normal.
#3

[eluser]webdevron[/eluser]
Thanks a lot. But sorry to say, still there in a problem. That code looks good and works on localserver (XAMPP). But whenever I uploaded it on my hosting server there is an error saying:
Fatal error: Class 'My_controller' not found in /home/user/public_html/app/controllers/home.php on line 3

Code snip:
/application/core/MY_Controller.php
Code:
class My_Controller extends CI_Controller {
        public function __construct(){
            parent::__construct();
            $this->load->model('Toolmodel');
            $this->Toolmodel->save_visitor($this->save_visitor());
        }

        public function save_visitor(){
            // Getting User Informations
        }
    }

And in my default controller (in this case 'home'):
Code:
class Home extends My_controller{
     public function index(){
            // View loading here
     }
}

So, could you please tell me what is the problem with Hosted CPanel?
#4

[eluser]CroNiX[/eluser]
It's MY_Controller. Same with the filename. Capitalization matters.
#5

[eluser]stevezissou[/eluser]
You need to include or require your parent controller within each file that contains a class which extends the parent.

Code:
require_once APPPATH . 'controllers/my_controller.php';
#6

[eluser]InsiteFX[/eluser]
The MY_Controller needs to be placed in ./application/core not in the controllers directory.
#7

[eluser]CroNiX[/eluser]
[quote author="stevezissou" date="1399375212"]You need to include or require your parent controller within each file that contains a class which extends the parent.

Code:
require_once APPPATH . 'controllers/my_controller.php';
[/quote]
No, you don't. CI will find it.
#8

[eluser]stevezissou[/eluser]
[quote author="CroNiX" date="1399384740"][quote author="stevezissou" date="1399375212"]You need to include or require your parent controller within each file that contains a class which extends the parent.

Code:
require_once APPPATH . 'controllers/my_controller.php';
[/quote]
No, you don't. CI will find it.[/quote]

The specific error message the OP was receiving was due to not requiring/including the parent controller file. If you follow InsiteFX's post, then yes CI will find it.

Personally, I don't like having parent controllers outside the core controllers folder (in which case you will need to remember to include the parent controller file).
#9

[eluser]CroNiX[/eluser]
Normally you'd be correct, but MY_Controller is a special case. See the userguide I linked to in my first post about extending the core of CI. Since he didn't capitalize it correctly, CI couldn't find it because it specifically looks for "MY_"

Just like whenever CI loads Input, CI looks to see if you extended it with MY_Input. It looks to see if you extended the router with MY_Router, etc.
#10

[eluser]CroNiX[/eluser]
The only time what I said wouldn't be true, is if you change the value of $config['subclass_prefix'], which defaults to "MY_".

The reason why it worked on the original posters development box but not his production box is because his development box's OS, presumably a windows or mac box, doesn't care about case sensitive filenames but his production box, probably linux, does.

About line 237 (CI 2.1.4) of /system/CodeIgniter.php, you can see it trying to determine if you extended Controller and if so, it requires it, so there is no need to manually include it and will actually cause an additional error if you do:
Code:
if (file_exists(APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php'))
{
require APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php';
}




Theme © iAndrew 2016 - Forum software by © MyBB