Welcome Guest, Not a member yet? Register   Sign In
Writting support classes?
#1

[eluser]Knitter[/eluser]
Hi,
After a long hiatus on using CI I'm trying to get this all concept into my head. So I've read the tutorials I could find, the official documentation, which is easy to read, and feels a bit barren, maybe CI is just that easy, don't know.

I'm now trying to get all this into code, so today's question is: Where do I put my application classes and how does CI know about them, or manages them?

Putting it in another way, I usually develop applications using MVC, a type that conflicts a bit with the way CI sees MVC. The pattern does allow direct contact from the view to the model, just doesn't allow the view to modify the model directly, but does allow it to read the model. You can say that I'm more used to Java MVC Smile

But CI puts me in the position where I don't really know how to create my application, it tells me that the model is just a collection of classes that only have data, and data access if you considered accessing the DB as part of the model, and that a controller is an all mighty class/entity that controls everything. So:
- How do I access one controller from the other, or, send events to listening controllers?
- Where do I put my support classes and interfaces and how does CI load them? Must I do all the work myself and go around what CI does?
- If, for example, I was building a contacts application I would create the ContactManager class, that would hide all the functionality of my application, I would then create the necessary views to it, can I do something like this in CI, or am I stuck with the "one page-one controller" thing?
- Can I have a middle controller from where all my controllers extend without having to modify CI? I have a piece of code that all controllers should have, can I create an abstract controller that is going to be the parent for all my controllers? Or must I use things like URI mapping and extending base classes from CI?

Any thoughts are appreciated Wink

Best regards,

Knitter
#2

[eluser]Damien K.[/eluser]
If you're looking for the short answer version, you can skip to the last line.

[quote author="Knitter" date="1254274927"]Hi,
After a long hiatus on using CI I'm trying to get this all concept into my head. So I've read the tutorials I could find, the official documentation, which is easy to read, and feels a bit barren, maybe CI is just that easy, don't know.[/quote]

I think you're not getting a response because it is clear that you either did not RTFM or you've read it all and forgotten everything.

[quote author="Knitter" date="1254274927"]I'm now trying to get all this into code, so today's question is: Where do I put my application classes and how does CI know about them, or manages them?[/quote]

Are you sure you read the manual?

[quote author="Knitter" date="1254274927"]Putting it in another way, I usually develop applications using MVC, a type that conflicts a bit with the way CI sees MVC. The pattern does allow direct contact from the view to the model, just doesn't allow the view to modify the model directly, but does allow it to read the model. You can say that I'm more used to Java MVC Smile[/quote]

Java implements MVC Model 2. CI is similar. If you ask 10 developers to define MVC you get 11 answers, because by the time you asked the 10th person the first person changed his/her mind.

There appears to be more agreement to not access the model via the view in an MVC architecture, regardless of which world (ie, JAVA/.NET/PHP/etc.) you're in. In software theory maybe you can, but should you? I'll leave the answer as an exercise for the reader.

[quote author="Knitter" date="1254274927"]But CI puts me in the position where I don't really know how to create my application, it tells me that the model is just a collection of classes that only have data, and data access if you considered accessing the DB as part of the model, and that a controller is an all mighty class/entity that controls everything. So:[/quote]

Which MVC definition does not consider accessing the DB as part of the model? I have yet to come across one that skew from this definition.

CI controller is no different than your Java controller. Java controllers are mighty too, maybe not superman mighty, but still it is mighty.

[quote author="Knitter" date="1254274927"]
- How do I access one controller from the other, or, send events to listening controllers?
[/quote]

I don't think controller chaining is part of the MVC definition. Some frameworks has it built-in while others don't support it. I don't think CI has inherit support for it although it can likely be achieved by other means. How you want to achieve this is up to you, especially if you're extending the framework to support this feature.

[quote author="Knitter" date="1254274927"]
- Where do I put my support classes and interfaces and how does CI load them? Must I do all the work myself and go around what CI does?
[/quote]

Remember the manual thingy I mentioned earlier?

[quote author="Knitter" date="1254274927"]
- If, for example, I was building a contacts application I would create the ContactManager class, that would hide all the functionality of my application, I would then create the necessary views to it, can I do something like this in CI, or am I stuck with the "one page-one controller" thing?
[/quote]

If you need to chain controllers, see my response above. Like most controllers in any worlds, one controller can handle multiple views (eg, pages). You can have '6 pages-one controller', which in turn can call 6 different functions from ContactManager. You may want to re-read the user guide/manual.

[quote author="Knitter" date="1254274927"]
- Can I have a middle controller from where all my controllers extend without having to modify CI? I have a piece of code that all controllers should have, can I create an abstract controller that is going to be the parent for all my controllers? Or must I use things like URI mapping and extending base classes from CI?
[/quote]

You don't extend base classes in the Java world for similar purposes? I thought this was a part of OOP/D all along... silly me. Anyway, I think your question here is contradicting. You want "an abstract controller that is going to be the parent for all your controllers". Don't you need to extend from something if it is going to be the parent for all your controllers? Since you're extending (yes, I answered the question preceding this), is there a reason why you don't want to extend from the base class?

Short answer: RTFM will address most if not all of your concerns.
#3

[eluser]Knitter[/eluser]
I think you misunderstood me in... well everything, I do get a bit confusing when writing what comes to mind, let me try to explain.

As support classes I'm not talking about other controllers, I'm talking about... support classes, for instance, Menu class that represents a menu item in the Toolbar class, a button class that represents a button, with all it's stats and action, not a controller and not accessed by a URL.

Putting those classes in the libraries folder is strange, that's what got me confused, I don't want a library, nor a helper, I want something that is part of my base application.

I do extend Java core/base classes but to get my "middle controller" I had to extend the Controller class, give my class the name MY_Controller and put it into the libraries folder, if I didn't do that, the my controller was never found by CI, it was an invisible class.

Accessing a controller from another one was just a quick example, I don't want to have to include all the files that define my classes all over one another, I wanted on simple autoload special function to handle that, I tried to get one into CI, but I failed to achieve that, maybe my fault, but I did manage to break CI...

The existing autoload method will look only at predestined folders and will instantiate the object and it will fail because most, if not all my objects, have constructors with parameters, as the should.

Should my "middle constructor" be named just "General", extend Controller and then all my other constructors be able to extend "General" without going to the trouble of creating a library?

I have a bunch of classes, they are in the model folder, why must they extend Model? It messes up with my inheritance, as I thing PHP still lacks multiple inheritance, and forces me to join things that are not related. And if they are nor extending Model, or Controller, or any other base class CI just ignores them.

I may have failed to see, in the manual, where this issue was addressed, and if so, I'll be more than glad to read another RTFM reply, but TFM is becoming to lack in it's explanations. I can, of course, dig into the code, but isn't the point, or at least one of them, not to do that? If I start needing to read the code to learn the basics on CI, then something is not right with TFM.

Regards,

Knitter
#4

[eluser]steelaz[/eluser]
Is there a reason you don't want to include your classes via simple include()? You could do that by extending CI Controller class. Here is my simplified MY_Controller.php file, all my controllers extend either Admin_Controller or Public_Controller:


Code:
class MY_Controller extends Controller
{
    function __construct()
    {
        parent::__construct();

        // Load database settings
        
        // Enable profiler?
        
        // Common template settings
    }
}


class Admin_Controller extends MY_Controller
{
    function __construct()
    {
        parent::__construct();

        // Security check
        
        // Load admin model

        // Load admin template
    }
}


class Public_Controller extends MY_Controller
{
    function __construct()
    {
        parent::__construct();
        
        // Enable website maintenance?
        
        // Load public template
    }
}
#5

[eluser]Damien K.[/eluser]
I don't mean to refer you to TFM but it explains it better than I can ever do.

Quote:As support classes I’m not talking about other controllers, I’m talking about… support classes, for instance, Menu class that represents a menu item in the Toolbar class, a button class that represents a button, with all it’s stats and action, not a controller and not accessed by a URL.

Putting those classes in the libraries folder is strange, that’s what got me confused, I don’t want a library, nor a helper, I want something that is part of my base application.

I have no problem with putting these in the 'libraries' folder. By definition, they fit the bill. How about 'libraries/widgets'? Smile Or 'libraries/views/widgets'? Great sounding locations for me, but that's just me, of course.

Quote:I do extend Java core/base classes but to get my “middle controller” I had to extend the Controller class, give my class the name MY_Controller and put it into the libraries folder, if I didn’t do that, the my controller was never found by CI, it was an invisible class.

It doesn't get 'merged/extended' with the default CI controller class? I assume you're doing it correctly. However, I do see many people extending their own MY_Controller instead of CI's Controller when they implement a controller, so that kind of suggest you're right on this. steelaz has a good response (above) regarding this though.

Quote:Accessing a controller from another one was just a quick example, I don’t want to have to include all the files that define my classes all over one another, I wanted on simple autoload special function to handle that, I tried to get one into CI, but I failed to achieve that, maybe my fault, but I did manage to break CI…

If you're okay with the default 'libraries' folder, then this is no problem. If you're not, then it is not too hard to extend the framework to support other folders.

Quote:The existing autoload method will look only at predestined folders and will instantiate the object and it will fail because most, if not all my objects, have constructors with parameters, as the should.

Ahhh, it just struck me why CI instantiate libraries. You can extend the autoload function to do the way you like it to do. You might wonder why you have to always muck with the framework. I personally don't like to have to always fight the framework to do what I want, hence why I'm enjoying CI because it serves my purpose most of the time. It might not serve your purposes though.

Quote:Should my “middle constructor” be named just “General”, extend Controller and then all my other constructors be able to extend “General” without going to the trouble of creating a library?

steelaz has responded to this end.

Quote:I have a bunch of classes, they are in the model folder, why must they extend Model? It messes up with my inheritance, as I thing PHP still lacks multiple inheritance, and forces me to join things that are not related. And if they are nor extending Model, or Controller, or any other base class CI just ignores them.

Your Java MVC framework doesn't require you to extend a framework base class? That must be one of the newer frameworks using something like reflection or IoC to identify/make the 'type'... cool stuff! No languages support multiple inheritance. It sounds like you're a "hard-core" OOP person. You're in the wrong world. The sad news is that there is a trend towards scriptish programming and Java/.NET is heading in this direction too. The good news is that PHP is heading in the opposite direction, or is this bad news? Smile

Everything is in TFM + common sense/intuition + experience. If you don't see it addressed in the manual, yes, you dig into the code and if that doesn't work you make it work. Isn't it great to be a computer science wiz? Wink
#6

[eluser]Knitter[/eluser]
Quote:Is there a reason you don’t want to include your classes via simple include()?
Maybe I'm just lazy, but it becomes cumbersome to manually manage all includes if they are scattered around the code, on single place to manage them, preferably if done automatically, would be better, it's what I've gotten used to and it feels I'm regressing instead of progressing, why should I do something manually, and that can be error prone, if I can create a method that will do that for me?

Quote:Here is my simplified MY_Controller.php
It's basically what I have and it kind of illustrates what I wanted to say, if I'm not mistaken, the MY_Controller is in the libraries folder, yet it is a controller and should be where all controllers are, if you name it otherwise, it will not be loaded by CI or at least will not work correctly, you can't have
Code:
XPTO extends Controller
and
Code:
XPTO2 extends XPTO
, you must always have
Code:
MY_Controller extends Controller
and
Code:
XPTO2 extends MY_Controller
, or am I wrong?

Quote:I have no problem with putting these in the ‘libraries’ folder. By definition, they fit the bill.
I have, has they don't fit the bill. The ContactManager class is model, it contains a linked list of Person objects that are model, each Person contains a Contact that is model but I cannot let CI manage them or it will try to load what it can possibly know how. Toolbar is model, but must not extend from Model, Menu is model but most no extend from Model, they all need to be manually managed by me. Files manually included, some are in the libraries folder, some are in the models folder, yet they all should be considered model, they are the application. They have no view to show their contents, nor a controller as they need none, yet the provide all the functionality of my application, nevertheless CI forces me to separate them in a way that is not natural, considering the problem's domain.

Quote:Your Java MVC framework doesn’t require you to extend a framework base class?
Did I say Java doesn't require me to extend the framework? Of course you can and sometimes need to extend it, but not for the simplest of things, and not in the same way I'm being forced, and the keyword here is 'forced', to do. I can create a all application without extending a base class from Java, I do, however, use the base classes, a big difference there.

Quote:No languages support multiple inheritance.
I would have to disagree there, but that is another topic.

Quote:Everything is in TFM + common sense/intuition + experience.
You added experience there, as I mentioned I have none with CI, and common sense and intuition are really based on experience, the one I have contradicts what I'm seeing here, so that may be the source of all my confusions Smile.

Quote:Isn’t it great to be a computer science wiz?
It was great when time was free, now, time is money, or at least is not as free and abundant as before. Of course I can change the framework to suite my needs, but can is that really the correct path?
#7

[eluser]bretticus[/eluser]
[quote author="Knitter" date="1254432532"]
Quote:Is there a reason you don’t want to include your classes via simple include()?
Maybe I'm just lazy, but it becomes cumbersome to manually manage all includes if they are scattered around the code, on single place to manage them, preferably if done automatically, would be better, it's what I've gotten used to and it feels I'm regressing instead of progressing, why should I do something manually, and that can be error prone, if I can create a method that will do that for me?

Quote:Here is my simplified MY_Controller.php
It's basically what I have and it kind of illustrates what I wanted to say, if I'm not mistaken, the MY_Controller is in the libraries folder, yet it is a controller[/quote]

Ahhh... wrong. It's the class definition for controller. It most certainly is not a controller.

I didn't even finish reading your post. It seems that each your posts is an attempt to promote some java mvc framework and point out the follies of CI and PHP.

The manual pretty much explains everything. It's the CI framework built on PHP. It can't be anything more unless you make it so.

So that brings us to the question of the hour...Why are you even using CI?
#8

[eluser]steelaz[/eluser]
Quote:Maybe I'm just lazy, but it becomes cumbersome to manually manage all includes if they are scattered around the code

What do you mean by "scattered around the code"? You put all you custom classes in one folder, i.e. /application/my_classes and include them as necessary from extended controller class. You can limit number of includes by adding some logic based on controller being called, or session/cookie values.

Quote:why should I do something manually, and that can be error prone, if I can create a method that will do that for me?

If you have a working solution in PHP that does what you are talking about, I'm sure it can be implemented with CI. Mind sharing the code?
#9

[eluser]BrianDHall[/eluser]
It seems to me that you are trying to do a 'port' of your code which already has quite a bit of MVC concepts, but those concepts don't seem to fit how CI works.

In short, you need to either port your code and accept that CI does things differently, or setup considerable extensions and customizations to the framework to behave differently than it was designed to behave.

For instance with dynamic class loading, CI doesn't really have anything that does that, but there are multiple extensions to CI that do that either as their sole purpose or as a feature. DMZ (Datamapper Overzealous Extension) for instance allows me to do this:

Code:
$user = new User();
$user->usergroup->get();
$user->usergroup->promote('admin');

Here there are three classes involved - User, Usergroup, and Datamapper. I autoload datamapper, and then CI looks for a User class which extends Datamapper which happens to be in my Models folder, and then when I want to access the related user's usergroup CI dynamically includes the usergroup class and loads the appropriate instance of it inside my $user variable.

However, all this was designed to work nicely in CI.

You seem to expect to be able to use your own previously made controllers, models, and views seemlessly or with little effort automagically in CI - all while either wanting CI to use its magic in some instances, but not in others.

I think you are just asking something that CI won't do, and I don't know that any framework anywhere will allow you to do it. So your choices are to a) port the code to use CI's structure and systems, b) extend and alter CI functionality to provide your own magic where you want it and use CI when it seems appropriate, or c) look elsewhere for a framework that 'just works'...but I don't think you'll find one.

For porting, it basically means converting to using libraries and helpers and manually loading them when needed - or designing your own system where if you try to access a library that hasn't been loaded yet then attempt to auto-load it. You then use the constructor of your controller, and/or a MY_Controller to provide some of your common features.

Trying to insist that CI is something that it isn't is only going to frustrate you. CodeIgniter is a lightweight framework that is the lightest and fastest of all major frameworks, makes a few strong opinions on organization, then otherwise gets out of your way.

An extensively robust automagical auto-loading auto-importing framework that attempts to guess what you want so you don't have to think about when to load something is likely to produce difficult to debug applications that can be difficult to pick up and understand, has considerable overhead that may very well produce more demand on a server than the application itself, and when the magic goes wrong you are in for some hurt.

CodeIgniter doesn't pretend to be such a system, because it isn't. Embrace the fundamental philosophy and purpose of the tool and take advantage of its natural strengths, or chose a different tool. Swords are not for banging, and cudgels are not for slicing.
#10

[eluser]Knitter[/eluser]
[quote author="bretticus" date="1254432979"]
Ahhh... wrong. It's the class definition for controller. It most certainly is not a controller.
[/quote]
Hum... so inheriting from a Controller makes my class be.. not a Controller? I know it may not be accessible from an URL, but it is, by definition of inheritance, a Controller, nevertheless, not the point.

[quote author="bretticus" date="1254432979"]
I didn't even finish reading your post. It seems that each your posts is an attempt to promote some java mvc framework and point out the follies of CI and PHP.
[/quote]
Promote Java? I mention Java on one little sentence so that other users could better understand my background. Any other reference is a response a user's questions. I have not, and do not intend to promote anything or point out anything.

[quote author="bretticus" date="1254432979"]
So that brings us to the question of the hour...Why are you even using CI?
[/quote]
Because I want to speed up development of PHP applications I write, ease that development and because CI is the best of all frameworks I tested, because I really like the framework and, if I want to create the usual web application with a few dozen controllers and views and a few tables for some data, I can complete do it in an afternoon. And that IS just great. But then again, that question of the hour of yours, is just not to the point. I appreciate you taking the time not to read the posts, and thank you for being helpful in your reply.

Quote:What do you mean by “scattered around the code
Including them, as necessary, will create several includes throughout the code, the classes have many relations and include many other classes, and nearly every class is a mix of inherintance and composition. There is always the usual already included class, and yes I know I can require_once/include_once, or some file that changes name and I have to search/replace or something alike. Wouldn't having an autoload function that automatically loads the classes I access great? At least it's what I've been doing in PHP almost ever since I write PHP.

[quote author="BrianDHall" date="1254434442"]It seems to me that you are trying to do a 'port' of your code which already has quite a bit of MVC concepts, but those concepts don't seem to fit how CI works.[/quote]
Yes, that is, sort of what I'm doing, I'm porting an existing project, and the some of the concepts that I have always used and that is not going well Sad.

[quote author="BrianDHall" date="1254434442"]
In short, you need to either port your code and accept that CI does things differently, or setup considerable extensions and customizations to the framework to behave differently than it was designed to behave.[/quote]
I guess the all point of my post is to really understand if I have to go all that way, is really CI design in such a way that so many changes are needed or am I missing something that would allow me to have what I want without that much work.

Code:
$user = new User();
$user->usergroup->get();
$user->usergroup->promote('admin');
Now that is great! Thanks for the example and the extension name, I'll really have to have a look at it.

[quote author="BrianDHall" date="1254434442"]
You seem to expect to be able to use your own previously made controllers, models, and views seemlessly or with little effort automagically in CI - all while either wanting CI to use its magic in some instances, but not in others.[/quote]
Of course I don't really expect to just use my code but would like to understand if CI is that flexible, and by flexible I want to say that it doesn't consume 50% of the projects time.

[quote author="BrianDHall" date="1254434442"]
So your choices are to a) port the code to use CI's structure and systems, b) extend and alter CI functionality to provide your own magic where you want it and use CI when it seems appropriate, or c) look elsewhere for a framework that 'just works'...but I don't think you'll find one.[/quote]
I'm trying to go for the first one, I've already seen other frameworks and CI seems better, but I'm a bit lost on how to translate/adapt what I have and the way I've been developing int CI.

[quote author="BrianDHall" date="1254434442"]
An extensively robust automagical auto-loading auto-importing framework that attempts to guess what you want ...[/quote]
I don't want it to guess, but I would like it to allow me to tell it where things are and how they should be handled. CI confuses me on that, it is always giving me errors on things I expect to be simple.

Does this forum have any thing like points or something that I can give you? Thanks for the reply it really help me understand what the options are and how I can work my way into getting my projects running on CI. And I'm going to give that DMZ thing a try as it looks it's what I've been looking for. Nevertheless, I think I'm getting the hang of CI.

Regards,

Knitter




Theme © iAndrew 2016 - Forum software by © MyBB