Welcome Guest, Not a member yet? Register   Sign In
Need a bit of help using CI to build a Website.
#1

[eluser]Lord Ve[/eluser]
First off let me say that CI is a great framework.

Now to begin. I have built website barebones in both php and html for years. I define a barebone as a website that is unskined in anyway. Just the functionality.

I started CI a few days ago because I was told that it would make it simpler by handling the redundant tasks. It does so wonderfully. The problem arises when trying to use my current knowledge to help me with building a relatively simple website.

The site I am currently working on only has a few requirements. It must allow logins, registration, a 'members area', and a simple news type main component. I have no trouble with all of these individual projects. My problem shows it's head when trying to use them all together.

I am currently using a 'template' type view builder. I call the template and pass the relevant view to show in the content area as a variable. So far so good. Now comes adding a simple login form under the navigation div. Here is where I get lost.

I either have to let the overall controller handle everything, which would make it rather clunky, for find a way to use the Controller I made for the Login section along with the overall controller. The first I can do, I would rather not, but I can do it. The second is what I am trying to aim for.

My question is, in a nutshell, what is the best way to bring everything together? Use the Overall controller or find a way to use more than one on one page or another way?
#2

[eluser]bobbytb[/eluser]
Welcome!

Option 2 can be done as follows: You can create a base controller that would do your login stuff and then have all other controllers inherit this. Checkout this excellent article on Base Classes by Phil Sturgeon: http://philsturgeon.co.uk/news/2010/02/C...ing-it-DRY
#3

[eluser]Lord Ve[/eluser]
I thank you for the article and the welcome.

I have read the article you posted and find it quite interesting. Would you happen to know of an example that explains how to use it in a meaningful way? I am one of those people who learn by example. At the moment I can not visualize how to incorporate using a Base Controller to handle everything. I can however see using it to check sessions and the like in 1 location.

To be a bit more detailed, right now I have made up the 'modules' for logging in, registering, and the members area. By modules I mean that I have completed working examples of each system. The main thing I am having trouble with is adding the completed login box directly into the layout. I have no idea how to take the Login Controller with the view and model and use it within the Overall Controller. Now, as I have said, I know I can bog down the Overall Controller with all the functions it needs to handle everything, but was hoping that there was a better way.
#4

[eluser]Buso[/eluser]
A simple way is to set some attributes in your base controller, where you load the data shared between all your pages (from which you will build your layout), eg.:
Code:
$this->data['header'] = $this->load->view('logo','',TRUE);
$this->data['footer'] = $this->load->view('sitemap','',TRUE);
$this->layout = 'my_global_layout';

Then in your current controller 'Login' (which should inherit the above attributes), you can do:

Code:
$this->data['content'] = $this->load->view('login_box','',TRUE);
$this->load->view($this->layout,$this->data);

Not sure if this is what you asked
#5

[eluser]Lord Ve[/eluser]
I'm not sure either. Smile

I am thinking in modular design. I want to be able to use the login module in any site I make without having to start from scratch. I want to think of everything as an object and that I can place that object where it needs to be at the time. But, that is from my background in C#. Not sure php can do that.

Maybe a better question would be, is there currently a way to, for example, call my Login Controller from within a view and have it process within that view? So, when I call the login_mod view, it calls the login controller and does whatever it needs to, with in that login_mod view.

Something like:
Code:
//login_mod
$this->load->controller('login');
and have it display everything that it needs, within that area.
#6

[eluser]juanvillegas[/eluser]
You can't call a controller from a view, i don't know technically, but morally you can't. The MVC principles states the opposite.

To reach modular design in this case i would make a view file for the login piece (the forms, and make it submit to the current page). Then your login controller should rely the auth process to the login model, for example. Until now, everything is reusable i think.
In your model you can apply some pattern to make it extensible and customizable for reuse. I mean, you'll have different auth on each applications, so you'll have to customize the model every time. A good idea may be applying some sort of template pattern (http://en.wikipedia.org/wiki/Template_method_pattern) with basifc skeleton methods.

So briefly to reuse the module just copy the view, copy the controller and extends the template model.
#7

[eluser]WanWizard[/eluser]
@Lord Ve,

In a standard CodeIgniter installation, there is a one-to-one mapping between the segments in the URI and a controller (even if you use routing, a specific URI always maps to one controller), and that controller is expected to generate the entire page.
While this works for a lot of applications, reuse of code is a lot easier if you can work in a more modular fashion.

There are several solutions available that add modularity to CodeIgniter. Most however, only extend the static way of one-to-one controller mapping to modules, but still have the restiction of one controller producing an entire page.

If you need more modularity, like being able to call individual components of a module and use their results to assemble a page, you need an HMVC solution.
There are two available, Modular Extensions, en Modular CI. Both are documented in the Wiki.
#8

[eluser]Oliver Ponder[/eluser]
I'm not sure of its 'morally' wrong, but what I sometimes do is call a model from a sub view.

So for example, in your main view you call a sub-view "news" (its ok to call views from views right?)

Code:
<? $this->load->view('sub/news');  ?>

Which then iterates through results it obtains from a model your wrote for it

Code:
//views/sub/news.php

<div id="news">
<h2>News of the day:</h2>
&lt;?
    $news = $this->News_model->get_news();
    
    foreach ($news as $news_item){
        echo '<p>'.$news_item->content.'</p>';
    }
?&gt;
</div>

I'd appreciate a more experienced coders comment on this approach.
#9

[eluser]Buso[/eluser]
[quote author="Oliver Ponder" date="1283054862"]I'm not sure of its 'morally' wrong, but what I sometimes do is call a model from a sub view.

So for example, in your main view you call a sub-view "news" (its ok to call views from views right?)

Code:
&lt;? $this->load->view('sub/news');  ?&gt;

Which then iterates through results it obtains from a model your wrote for it

Code:
//views/sub/news.php

<div id="news">
<h2>News of the day:</h2>
&lt;?
    $news = $this->News_model->get_news();
    
    foreach ($news as $news_item){
        echo '<p>'.$news_item->content.'</p>';
    }
?&gt;
</div>

I'd appreciate a more experienced coders comment on this approach.[/quote]
This is not the most common style for CI, but I have seen other programmers using it, and I have been told that views can call models, because the controller is not suposed to be the connection between them, but the one who authorizes and picks what to show. This way the controller calls libraries/models just to authorize and make a decision, and then the view calls other libraries/models/helpers/views to build the output.
I have to try this approach.
#10

[eluser]juanvillegas[/eluser]
[quote author="Oliver Ponder" date="1283054862"]I'm not sure of its 'morally' wrong, but what I sometimes do is call a model from a sub view.

So for example, in your main view you call a sub-view "news" (its ok to call views from views right?)

Code:
&lt;? $this->load->view('sub/news');  ?&gt;

Which then iterates through results it obtains from a model your wrote for it

Code:
//views/sub/news.php

<div id="news">
<h2>News of the day:</h2>
&lt;?
    $news = $this->News_model->get_news();
    
    foreach ($news as $news_item){
        echo '<p>'.$news_item->content.'</p>';
    }
?&gt;
</div>

I'd appreciate a more experienced coders comment on this approach.[/quote]

I don't believe that's the way. At least if you want to stick strictly to MVC modelling. If you google images for "MVC" you'll find no connectin between models and views, just view<->controller<->model .
Justification for this is that models are usually closely related to a database table, and we don't want the table structure to spread all over the application. As more we can hide the details of the table, the better. For example, in the code you posted that view is only going to work with a table having content as its main field. A better approach would be using Model::FIELD_CONTENT to ask the model for the content column name, and a better one to delegate this to controller.
Also, the controller is there to control what information is forwarded to views and how. If we accept views having access to models then we don't need controllers anymore than for rendering.

My approach for this would be setting up some filters system like we do in Rails. For example, all methods requiring a "news" block have a before_filter procedure "instantiate_news", which somehow (setting session, flashdata or just retrieving news). You can write libraries to simulate filters (coding in the constructor) and using $this->load->library in the controllers construct.




Theme © iAndrew 2016 - Forum software by © MyBB