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

[eluser]Unknown[/eluser]
I have just started on CI and have finished going through the hello world and blog tutorial. I have been trying it out further and have been wondering how does one extend a controller class or should one even need to extend a controller class?

I guess one example is a controller for a generic shop front. So it would be
shop.php
Code:
class Shop extends Controller
{

}

Then I would like to use that same controller for a shop thats specific for downloads only
shopdownloads.php
Code:
class ShopDownloads extends Shop
{

}

In normal PHP I would usually just add
Code:
include (path/to/shop.php controller);
on top of shopdownloads.php and continue on.

Is there a proper way to do this in CI or have I got my basic concepts wrong?
#2

[eluser]Sarfaraz Momin[/eluser]
In this case you can make a library which you can extend. You can refer the userguide for CI libraries where you can have all generic functions and then just load the library and call the functions in it appropriately from your controllers.

Hope it makes sense.

Have a good day !!!
#3

[eluser]Unknown[/eluser]
your ShopDownloads will inherit (methods,properties etc etc) from the Shop controller. and as said in the video tutorial, u must inherit your class from the controller class so that it can inherit all the properties and methods codeigniter provides for u.

Sohaib,
#4

[eluser]Colin Williams[/eluser]
Quote:In normal PHP I would usually just add include (path/to/shop.php controller); on top of shopdownloads.php and continue on.

Luckily for you, CodeIgniter was written in normal PHP. So, what are your concerns with doing it that way? Seems perfectly valid to me.

Why do so many CodeIgniters think include() is the devil?
#5

[eluser]Xelgen[/eluser]
Sorry for kind of offtopic, but I also thought that include() is somewhat unrecommended/not-CI-way. And used helpers/libraries instead.
#6

[eluser]xwero[/eluser]
[quote author="Colin Williams" date="1232378317"]
Quote:Why do so many CodeIgniters think include() is the devil?
They have never used it since they started using CI Smile

It's the same as using input->post when they don't use the xss cleaning. For the isset check you don't have to do it
Code:
if( ! $this->input->post('key'))
Isn't longer than
Code:
if( ! isset($_POST['key']))
#7

[eluser]Nick Husher[/eluser]
Note that you can put your Shop class in MY_Controller.php in your application/libraries folder and it will be automatically loaded by the CI engine. Note that you can define multiple subclasses of Controller in MY_Controller.php, which makes it handy for developing a variety of different controller types.
#8

[eluser]Phil Sturgeon[/eluser]
[quote author="Sohaib" date="1232372336"]your ShopDownloads will inherit (methods,properties etc etc) from the Shop controller. and as said in the video tutorial, u must inherit your class from the controller class so that it can inherit all the properties and methods codeigniter provides for u.

Sohaib,[/quote]

That is exactly what he is trying to do. When you extend a class you inherit all of its methods and properties, AND that of any class above it. So you get everything "CI provides for you" and everything in the shop class.
#9

[eluser]Colin Williams[/eluser]
Quote:Sorry for kind of offtopic, but I also thought that include() is somewhat unrecommended/not-CI-way. And used helpers/libraries instead.

It's an odd perception that a lot of people have. It would be bad practice to use it in place of an existing CI method that is better equipped to perform the given action.

Quote:Note that you can put your Shop class in MY_Controller.php in your application/libraries folder and it will be automatically loaded by the CI engine.

True. But he obviously has an array of methods in his Shop controller. It wouldn't be practical to move this into MY_Controller.php. I don't get how it makes sense to bend over backwards just to avoid using the include() statement. Essentially, you end up hunting for files that CI include()s itself.

Including the shop.php controller is the absolute, most practical way to go about it.




Theme © iAndrew 2016 - Forum software by © MyBB