Welcome Guest, Not a member yet? Register   Sign In
Extending Controller class
#1

[eluser]reaktivo[/eluser]
I have searched the forum, and have not found a real solution, is there a way to extend system/libraries/Controller.php like other classes that use MY_Name_of_class without having to extend MY_Controller on the Controllers placed on application/controllers/whatever ?, can't I add functionality to a Controller without modifying every Controller I have coded already ?
#2

[eluser]xwero[/eluser]
Controllers are not extended like other classes. The MY_Controller file only provides you a way to 'autoload' extended controllers.
The extended controllers are only known to the controller that is called by the url when you extend it with a extended controller class.

Code:
// MY_Controller.php
class Extend1 extends Controller {}

class Extend2 extends Controller {}
// user.php
class User extends Controller {} // child of the controller class
// members.php
class Members extends Extend1 {} // grandchild of the controller class
#3

[eluser]wiredesignz[/eluser]
You may create application/libraries/Controller.php yourself.

Copy the original contents of system/libraries/Controller.php and add your own extensions as needed.

Every controller in your application will inherit your additions.
#4

[eluser]reaktivo[/eluser]
[quote author="xwero" date="1228325282"]Controllers are not extended like other classes. The MY_Controller file only provides you a way to 'autoload' extended controllers.
The extended controllers are only known to the controller that is called by the url when you extend it with a extended controller class.[/quote]

Thanks xwero.
#5

[eluser]Colin Williams[/eluser]
wired's solution is usually the best place to start, in my opinion. Nothing wrong with "grandfathering" the Controller class like xwero points out, either. Choose your medicine..
#6

[eluser]Rey Philip Regis[/eluser]
[quote author="xwero" date="1228325282"]Controllers are not extended like other classes. The MY_Controller file only provides you a way to 'autoload' extended controllers.
The extended controllers are only known to the controller that is called by the url when you extend it with a extended controller class.

Code:
// MY_Controller.php
class Extend1 extends Controller {}

class Extend2 extends Controller {}
// user.php
class User extends Controller {} // child of the controller class
// members.php
class Members extends Extend1 {} // grandchild of the controller class
[/quote]

Didn't know this yet..But at least I know now..What's the use of creating a new controller when you can extend it in you application? What do you thing reactivo trying to do? Just curious....
#7

[eluser]Colin Williams[/eluser]
Anything "global" Rey. Like, load up menus from your menu system. Load up sidebar content, header content, footer content, etc. This leaves your individual controllers free to worry only about their specific task, and not each final piece of the layout.
#8

[eluser]Rey Philip Regis[/eluser]
[quote author="Colin Williams" date="1228362433"]Anything "global" Rey. Like, load up menus from your menu system. Load up sidebar content, header content, footer content, etc. This leaves your individual controllers free to worry only about their specific task, and not each final piece of the layout.[/quote]

Hi Colin thnks for the reply but im having trouble grasping what youve posted. Can you explain more on this. Actually I dont have a use for this as of now, but maybe I maybe using this technique in the future.

Good day.
#9

[eluser]reaktivo[/eluser]
[quote author="Rey Philip Regis" date="1228362098"][quote author="xwero" date="1228325282"]Controllers are not extended like other classes. The MY_Controller file only provides you a way to 'autoload' extended controllers.
The extended controllers are only known to the controller that is called by the url when you extend it with a extended controller class.

Code:
// MY_Controller.php
class Extend1 extends Controller {}

class Extend2 extends Controller {}
// user.php
class User extends Controller {} // child of the controller class
// members.php
class Members extends Extend1 {} // grandchild of the controller class
[/quote]

Didn't know this yet..But at least I know now..What's the use of creating a new controller when you can extend it in you application? What do you thing reactivo trying to do? Just curious....[/quote]

I really don't know if this is the best way of doing this task but...

I was looking for a way to remove form submission validation on Controllers, and allow all Controllers to validate different forms, for example a user auth form submitted and displayed on different view's loaded on different Controllers. You would not want to check for user authentication on each controller. So I added some code to the Controller constructor to load and call a model method, here it is:
Code:
if($service = $this->input->post('service')) {
            $this->load->library('Request');
            
            list($model, $method) = explode(".", $service);
            
            $this->load->model($model);
            $this->response = $this->$model->$method($_POST);
            
            if($this->request->is_ajax()) {
                switch($this->input->post('format')) {
                    case 'php':
                        serialize($this->response);
                        break;
                    
                    case 'xml':
                        //convert to xml
                        break;
                        
                    case 'json':
                    default:
                        json_encode($this->response);
                        break;
                }
                
            }
            
        }

This removes the need for my Form to be tied to a Controller, also, it allows to make ajax request without the complete page being returned. And since in CI 1.7 I can have form_validation on the model itself, I think is nice.

I you feel you have a better way of doing this, please let me know! Thanks.




Theme © iAndrew 2016 - Forum software by © MyBB