• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Call model-class from view

#1
[eluser]juan1904[/eluser]
Hi,

I got a template view which looks like this:
Code:
$this->load->view('header');
$this->load->view('top');
$this->load->view('menu');
$this->load->view('login');
$this->load->view($content);
$this->load->view('footer');

This template view is called from different controllers with a parameter $data['content'] which is the name of the content-file for that specific page.

In my login.php view file I need to call a model named login_model.php, and now I wonder if it's bad coding to call the model directly through the view file instead of calling it from the Controller.

The problem with calling it from the controller is that I need to call it in every controller-file since the login.php is visible on every page of my website.

If you do know any smarter solution than those two I got on my mind, please explain it for me. I really want my code to be as good as possible!

Thank you!

#2
[eluser]TheFuzzy0ne[/eluser]
Welcome to the CodeIgniter forums.

It is considered bad practice to call a model directly from the view, although to my understanding, it's OK to call upon model properties as opposed to methods.

IMHO, the view should only be aware of the controller. The controller is the glue between the pieces of your application.

If it's something you need to use often, you can create a helper for it, or create a custom controller and extend it.

#3
[eluser]juan1904[/eluser]
I don't think to create a helper is the best solution here because I will only use that helper for my specific project.

I am not very familiar with creating custom controllers. Do you know any webpage where I can learn about it?

#4
[eluser]TheFuzzy0ne[/eluser]
[url="http://ellislab.com/codeigniter/user-guide/general/core_classes.html"]RTFUG[/url]. Smile

The user guide shall guide you!

#5
[eluser]juan1904[/eluser]
I've created a custom controller named MY_Controller (my Controller extends the regular Controller) now.
All my other controllers now extends MY_Controller.

In MY_Controller I got this code:

Code:
class MY_Controller extends Controller {

    function __construct()
    {
        parent::Controller();
    }

    function login()
    {
        $data['content'] = 'content';
        $this->load->view('template_page', $data);
    }
}

I got a formula for login on all pages on my website, and now all my controllers got the function login.
But when I use the form-helper like this in my login.php:

Code:
<?php echo form_open('login'); ?>
<input type='text' name='login' />
<input type='password' name="password" />
</form>

I do get this source code:
Code:
<form action="http://localhost/newnhl/index.php/login" method="post">

And when I click it, it does not exists because login is not a controller itself it is just a function.

Does anyone got a good solution for this?

#6
[eluser]Dam1an[/eluser]
The parameter for form_open should be made up of 2 parts, the controller and the function
If just one is supplied, it looks for that controller and executes the index function

Calling it on any of your controllers / login should work

(tbh, I'm not really sure why you want the login function to be part of all your controllers)

#7
[eluser]juan1904[/eluser]
Well then I need to write different form_open's for every single page which I don't want to do.

The reason I want the login-function to be a part of every singel controller is that when someone logs in I want him to stay on that page he currently browsing.

If there is a better way to do this, please explain it for me Smile

#8
[eluser]Dam1an[/eluser]
The way I do it is
1. On every page (that you need to be logged in to see) check if the user is logged in
2. If they're not logged in, get the current URI string, and save it to session
3. Redirect to login (and display a "You need to be logged in to see that page" message)
4. Process the login attempt, and on successful login, redirect to the URI string you saved to session (and unset it)

#9
[eluser]juan1904[/eluser]
[quote author="Dam1an" date="1242181426"]The way I do it is
1. On every page (that you need to be logged in to see) check if the user is logged in
2. If they're not logged in, get the current URI string, and save it to session
3. Redirect to login (and display a "You need to be logged in to see that page" message)
4. Process the login attempt, and on successful login, redirect to the URI string you saved to session (and unset it)[/quote]

You know my login-form is to the left on every singel page of my website. And when you login I just want that login-part of the page to refresh and there will appear a welcome message. The other parts of my homepage you are browsing should consist.

So my controller-function login should call for following view-files:
Code:
$this->load->view('header');
$this->load->view('top');
$this->load->view('menu');
$this->load->view('welcome_msg');
$this->load->view($content);
$this->load->view('footer');

My problem here is that I do not know what they were currently browsing, so I do not know what I should set my $content variable to.

#10
[eluser]juan1904[/eluser]
I am still stuck! Sad

If you need a better explanation of my problem, here we go:

Controller, users.php:
Code:
<?php

class Users extends Controller {

    function Controller()
    {
        parent::Controller();
    }

    function login() {
        // Here I should call the model to check if the user exists or not.
        if(user exists and the username and password was correct)
        {
            start session and set session-variables.
            redirect('the page the user was browsing but with session var set.');
            // How do I know which page he was browsing?
        }
        else
        {
            give error message.
        }
    }
}

View, login.php:
Code:
<?php echo validation_errors(); ?>
<?php echo form_open('Users/login'); ?>
    <input type='text' name='login' class='login' value='Användarnamn'>

    <input type='password' name='password' class='login' value='Lösenord'>

    <input type='submit' value='Logga in' class='loginsub' />
</form>

To clarify things, the login.php is just one of the views I load at each page. In my first post you can see my template view!

In my view file login.php I would like to add if he is logged in, show a welcome text, if not show the login formula. Is it good coding or shouldn't the view know anything about sessions aswell?

Sorry for all those questions, but I'm totally new trying to redo my webpage in code igniter. I've read the manual a few times but everything isn't clear.

// Johan


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.