Welcome Guest, Not a member yet? Register   Sign In
What is best structure for a big application?
#11

[eluser]Edemilson Lima[/eluser]
The problem is not to nest views inside of views. It is nest controllers inside views and controllers inside controllers. Because, if you nest a view inside another view, which controller will generate the data to that sub view? How to keep views and controllers together in the flow of the application? I want to have a separate controller for each view, not one huge controller for all my views and sub views...

I would like to do it this way:

1) In the home page, the welcome controller loads the main view, passing by $data what views and controllers it need to load. In this case, are the categories controller and the home controller.
2) The main view loads the header view, the sibebar controller, the content controller and the footer view.
3) The sidebar controller loads the categories controller, which loads the categories view.
4) The content controller loads the home controller.
5) The home controller loads the news controller, which loads the news view, and the offers controller, which loads the offers view.

6) In the products page, the products controller loads the main view, passing by $data the views and controllers to load.
7) The main view loads the header view, the sidebar controller, the content controller and the footer view.
8) The sidebar controller loads the product menu controller, which loads the product menu view.
9) The content controller loads the product list controller, which loads the product list view.
10) The product list controller have the add, update and remove functions.

As can you see, this is something like the old fashion way to do the thing, but this is the more logic way, in my opinion. You can keep the controllers and their associated views together in the application flow, without need to create isolated functions in libraries, helpers or plug-ins. It is not even necessary to load controllers from views, but only from controllers, if you want to keep the presentation logic and business logic completely separate.
#12

[eluser]Rick Jolly[/eluser]
[quote author="Edemilson Lima" date="1200614138"]...but what I really want (I guess) was a way to load controllers within views or load constructors instead. We could have something like:

Code:
$this->load->constructor('[folder/]file','function'[,$data][,true/false]);

Such method could load the file and execute the function, passing the optional $data array and print or return the output to the caller constructor or view. Loaded constructors could also load another views or constructors.

Well, I don't know the impact of this or even if it is possible, but I think it could solve this issue.[/quote]

CI can only run one controller per request, but you can accomplish something similar in the view. You can call a helper function in the view that loads a specified library and calls the correct method of that library. The library would return a view with it's data as a string and the helper would echo that. Search for HMVC in these forums.

Also, I don't think anyone has mentioned extending the CI controller. You could put all your common code in the constructor of a parent controller that extends the CI controller. Then instead of extending the CI controller, you'd extend your custom parent controller. There are lots of examples in these forums.
#13

[eluser]Edemilson Lima[/eluser]
I did search for HMVC and I read a lot of threads about this same issue. It is an ancient problem, isn't it? Controllers can not be nested. At least without loose the compatibility with PHP4 and may be change a lot of things in the core. The worst thing with such changes in the core is that it cannot break the compatibility of code already running with the actual version of CI.

The problem with put everything in helpers or libraries and load them in the parent controller is to load a lot of code into memory every time and use only part of it in each request. We could load them in the views only when necessary.

In my examples, categories, news and offers are not really controllers, as they don't need to be called from the URI directly. In fact they are more like helpers or libraries.
#14

[eluser]Rick Jolly[/eluser]
[quote author="Edemilson Lima" date="1200639306"]
The problem with put everything in helpers or libraries and load them in the parent controller is to load a lot of code into memory every time and use only part of it in each request. We could load them in the views only when necessary.
[/quote]
I wasn't suggesting loading all the libraries in the parent controller. A library can be loaded by the single helper function when/if it needs to be loaded. Using your example but instead calling a helper function:
Code:
<?= load_partial('library_name','function'[,$data]); ?>

The comment I made about the parent controller wasn't really related to this - it was just demonstrating another way of not repeating code.

[quote author="Edemilson Lima" date="1200639306"]
In my examples, categories, news and offers are not really controllers, as they don't need to be called from the URI directly. In fact they are more like helpers or libraries.[/quote]
Exactly. Make them libraries as opposed to controllers and you don't have to worry about CI not being able to nest controllers.
#15

[eluser]Edemilson Lima[/eluser]
I was thinking better about this and I think the models are a better option to do this instead of helpers or libraries. Models in some way are like controllers, but they cannot be accessed by the URI, what is not necessary or even desired. I don't think models are just for database operations. Where a domain logic comes in, models can be used for this purpose, with the advantage of models are classes that are joined to CI super object and work better with the controllers. I say this because things like categories, news and offers in my examples are not helpers, libraries or plug-ins. They are modules of my application. As I can't nest them within their parent controllers, the best way to fit them there is create them as models.
#16

[eluser]madeks[/eluser]
Hi guys,

I'm not sure my problem is similar to you or not.
Here is my problems:
1. In my project, I want to have many controllers(modules) in one page.
For example, at home page I want user authentication, news, jobs, article, etc.
2. I don't want to duplicate the view as most of my site will have the same template. For example, the page will contain header, top menu, left menu, body and footer.

I try to read and search this forum for a couple of hours.

These are my conclusion:
1. With Codeigniter, we cannot call a Controller in a Controller.
2. Only 1 controller will be called for each request.
3. We cannot extends Model class.


For the first problem, does it work for you when you make your modules as a model instead of controller. I'm not sure because it seem CodeIgniter doesn't seem to like you to do much in the model. I guess it from they don't allow you to extended it.

Anyway, I going to try it myself too.
This is my plan,
1. Make a 2 controllers for frontend and backend called.
2. at frontend controller, let call it "Home", it will contain a method that have the same name of the module it trying to work with(from request). For example, this url "home/job/add" will go to home controller and job method and in the job method it will process to add a new job by call to a function of Job Model.

Code:
class Home extends Controller {

function Home () {
// call parent constructor
// do some instantiate
}

function job () {
//instance job model
if (action  eq 'add') {
// call to model add function
}
else if (action  eq 'show') {
// call job model to query data
}
}

function news () {

}

function Authentication () {

}

}

For the second problem ,I plan to use this.
http://www.maestric.com/wiki/doku.php?id...r_template

If you have any suggestions or ideas please feel free to share.
I'm stuck with this for many hours now.

Thanks
#17

[eluser]marlar[/eluser]
[quote author="Edemilson Lima" date="1200594854"]As you can see, I must to reuse the same code to rebuild the entire web site in every class and every function... This is a lot of redundant code.[/quote]

This is untested but I believe it will work. The code below makes use of the fact that you can store a function name in a variable and call the function using that variable.

So your redundant code could be replaced with:
Code:
function remove($id) {
    _main('remove', $id);
}
    
function update($id) {
    _main('update', $id);
}
    
function _main($type, $id) {
        $header['menu']=$this->load->view('menu_view','',true);

        $categories=$this->categories->list();
        $lateral['lateral']=$this->load->view('categories_view',$categories,true);

        // Dynamically assign the correct function depending on $type
        $function = "product_$type_function";
        $product_data=$function($id);

        $data['section']='Products - $type';
        $data['header']=$this->load->view('header_view',$header,true);
        $data['lateral']=$this->load->view('lateral_view',$lateral,true);
        $data['contents']=$this->load->view("product_{$type}_view",$product_data,true);
        $data['footer']=$this->load->view('footer_view','',true);
        $this->load->view('main_view',$data);
    }
#18

[eluser]Colin Williams[/eluser]
"Controllers within controllers" makes no sense. Let's examine what CI defines a controller as: "Controllers are the heart of your application, as they determine how HTTP requests should be handled... A Controller is simply a class file that is named in a way that can be associated with a URI."

Controllers handle URI requests. They are not modular sections of a page display; they build the page from modules. When you think "module" in CI, you're probably looking at a Library. Models should be reserved for data retrieval and data formatting.
#19

[eluser]BizComputing[/eluser]
There is a place for controllers within controllers, it's called HMVC. It's still MVC but it's breaking up the function of the controller into smaller modules.

When working with a very large application ( which I do day in and day out ) having the ability to break bigger pieces of code into smaller units is key to managing and maintaining my applications. And yes, libaries assist controllers in what they do, and controllers themselves are nothing more than libraries.

To point blank say that "Controllers within controllers" makes no sense is to show your lack of experience working in the large application environment. Just do a search on this forum for HMVC or nested controllers or any other similar search and you will find that this is not the first time this feature has been requested.

I agree, models are for data.
#20

[eluser]Colin Williams[/eluser]
Quote:To point blank say that “Controllers within controllers” makes no sense is to show your lack of experience working in the large application environment.

My comments are with respect to the CodeIgniter framework (figured I had defined that by quoting from the CI user guide). CodeIgniter is not HMVC. "Controllers within controllers" is often the wrong approach to take within this framework, and I think a lot of CI beginners go that route too soon (regardless of whether or not it's a "very large application").

I simply wanted to reinforce CI's approach to MVC architecture.




Theme © iAndrew 2016 - Forum software by © MyBB