Welcome Guest, Not a member yet? Register   Sign In
Call model-class from view
#21

[eluser]Dam1an[/eluser]
The function name is just get_instance, the '&' just means reference instead of creating a new instance. Its valid to have =& get_instance as well as = &get;_instance

As for learning about how CI is built, have you read the introduction and general sections in the user guide?

Also, you can browse through the source code, starting with index.php, and then following off various calls to see what happens
If you want to find out how a specific function works, what it calls etc, just browse through the library code for it
#22

[eluser]TheFuzzy0ne[/eluser]
We learn what we know from two sources:

a) The [url="http://www.ellislab.com/codeigniter/user-guide"]user guide[/url]
b) By reading the source code for CodeIgniter. You'd be surprised how little there is to learn, and how simple it is.

EDIT: Oh, and I put this thread together to help - http://ellislab.com/forums/viewthread/107773/
#23

[eluser]juan1904[/eluser]
I've already read the user guide a few times.
Then I just need to study the source code, thank you!
#24

[eluser]juan1904[/eluser]
About saving the URI in a session-variable. Isn't that a little unnessecary when I can use $_SERVER['HTTP_REFERER'] or is it any reason not to use $_SERVER['HTTP_REFERER']?
#25

[eluser]juan1904[/eluser]
If anyone else know a good way to implement a login-formula on every page on your website, and get a welcome-text (on the same spot where the login formula was) and get error messages if they don't successfully logged in, it would be nice to hear!

I really wonder how they did it here on the forum. On the top of every page when I'm logged in there is a text "Hello juan1904" and some links to edit information and stuff.
#26

[eluser]TheFuzzy0ne[/eluser]
That's done by calling views from within a view. You'd have a template which includes the necessary views - header, navigation, body and maybe footer.
#27

[eluser]juan1904[/eluser]
[quote author="TheFuzzy0ne" date="1242355718"]That's done by calling views from within a view. You'd have a template which includes the necessary views - header, navigation, body and maybe footer.[/quote]

That is excatly how I did. But since this template is a view file it's no good to call session-variables to that template (if I understood MVC correct).

So if I wanted to check if my user is logged in i need to do it like this in every singel Controller:

Code:
if(session-variable is set) {
$data['login'] = welcome.php;
} else {
$data['login'] = login.php;
}

$data['content'] = mainpage.php;

$this->load->view(template, $data);

That seem like some extra work in every single controller.

And also when I redirecting my users back to the page he was browsing if he get an error message, I need to use a session-variable or something to write some text.

form_validation error message only seem to work if you do $this->load->view('template'); again, which I cannot do because I don't know which page he was browsing.
#28

[eluser]TheFuzzy0ne[/eluser]
That's where exporting the functionality to a library or another model can help.

Code:
if ($this->user->is_logged_in())
{
    # ...
#29

[eluser]juan1904[/eluser]
Hm so all I need to do is to call a model which choose the view to load (login.php or welcome.php). Seem to make sense!

Just one more problem then. When I use form_validation in my login-controller the code is something like this:

Code:
function login()
{
$login = $this->input->post('login');
$password = $this->input->post('password');

$this->form_validation->set_rules('login', 'användarnamn', 'callback_user_exists');
$this->form_validation->set_rules('password', 'lösenord', 'callback_user_exists');

$prev_page = $this->input->server('HTTP_REFERER');

if ($this->form_validation->run() == TRUE)
{
// Set some session-variables.
redirect($prev_page);
}
else
{
redirect($prev_page);
}
}

function user_exists($login, $password)
{
if(user exists i database) {
return TRUE;
} else {
$this->form_validation->set_message('user_exists', 'Fel %s.');
return FALSE;
}
}

I don't know how to use form_validation to outprint error messages in my login.php here. If I load the template view file I don't know which content the user were browsing.

Another question: Should I put all these validation code in a model instead? I'm not really sure in which situations to use models and which to use controllers.
#30

[eluser]juan1904[/eluser]
I've read alot of posts here on the forum and I can't find a really good answer to my problem. CodeIgniter doesn't seem so good at handling these kind of problems.

Nobody got a good solution for me? It might not be the best forum to ask on but here we go anyway, does anyone know about another framework which maybe suit my needs a little better than CodeIgniter?




Theme © iAndrew 2016 - Forum software by © MyBB