Welcome Guest, Not a member yet? Register   Sign In
How to keep url clean on calling $this->index()
#1

[eluser]vercoutere[/eluser]
Hi,

I'm completely new to CI, but loving it already!
I've stumbled across a small issue here, I'm creating a login form for my website and when the user credentials are incorrect I want the page to reload and to keep the formfields filled in as the user did. Now this all works easily by using the $this->index() function.

The only issue I've got with this is that my url is not clean anymore when doing so.

My login form is located at domain.com, but when I press submit and the user credentials were wrong my url changes into domain.com/index.php/main/validate_user wich is clearly not how I want my url to look after only 1 click!

I've searched the forums but did not come up with any helpful results (I guess "index" is quite a common term on these forums :-) )

My code at this point is very simple and straight forward.

Code:
class Main extends CI_Controller {

    function index()
    {    
        $this->load->helper('form');
        $this->load->view('main_view');
    }
    
    function validate_user(){
        
        $this->load->model('user_model');
        $query = $this->user_model->validate();
        
        if($query) {
            
            // De login gegevens waren correct ...
            
            $voornaam = $query->row()->voornaam;
            $achternaam = $query->row()->achternaam;

            $session_data = array(
            
                'gebruiker' => $voornaam . " " . $achternaam,
                'is_ingelogd' => TRUE
            );
            
            $this->session->set_userdata($session_data);
            
        } else { $this->index(); }
    }
}

Thanks a bunch for any help!
#2

[eluser]InsiteFX[/eluser]
Index is a controller default try using the redirect below.
Code:
redirect('main');

InsiteFX
#3

[eluser]vercoutere[/eluser]
Hey, thanks for the quick reply!

Using that redirect still has the /index.php/main popping up in the URL bar. Though I could use a redirect to the base_url in this case, but the real issue when using redirect is that the set_value() function for the form fields doesn't get anything from the post anymore, so the values that were filled in by the user don't get remembered.

(I hope I'm making myself clear here, English isn't my first language :-))
#4

[eluser]CodeIgniteMe[/eluser]
You can use ajax if you want to submit and receive data to webserver without redirecting the page if not necessary.
I prefer you read http://jquery.com, and its powerful ajax feature.
#5

[eluser]toopay[/eluser]
If you still want it to use "php way" rather than "ajax way", you need to configure this stuff:

1. htaccess, so you can provide uri like http://yourdomain.com/somecontroller/somefunction (without index.php). Search over the forums related with this.
2. You either set up a hook, _remap or the simple one : route!. You can set up a route like these
Code:
$route['signin'] = "main/validate_user";
Then now, since you already set an exception on your route file, you can have a simple uri at your form (at view file)
Code:
<form method="POST" action="/signin">
<!--- rest of your form ---->
</form>
#6

[eluser]vercoutere[/eluser]
[quote author="OOP-MVC addict" date="1309527900"]You can use ajax if you want to submit and receive data to webserver without redirecting the page if not necessary.
I prefer you read http://jquery.com, and its powerful ajax feature.[/quote]

Ajax isn't really what I was going for though it had allready crossed my mind, thanks for the reply anyway Smile

[quote author="toopay" date="1309528600"]If you still want it to use "php way" rather than "ajax way", you need to configure this stuff:

1. htaccess, so you can provide uri like http://yourdomain.com/somecontroller/somefunction (without index.php). Search over the forums related with this.
2. You either set up a hook, _remap or the simple one : route!. You can set up a route like these
Code:
$route['signin'] = "main/validate_user";
Then now, since you already set an exception on your route file, you can have a simple uri at your form (at view file)
Code:
<form method="POST" action="/signin">
<!--- rest of your form ---->
</form>
[/quote]

I'll look into the rerouting future, thanks for the tip!
#7

[eluser]CodeIgniteMe[/eluser]
I recommend using the form helper instead of the actual form tag.
Code:
<?php echo form_open('signin'); ?>
This is to prevent this
Code:
<form method="POST" action="/signin">
<!--- rest of your form ---->
</form>
from appending to your existing URI.
for example. If your current URI is
Code:
http://example.com/index.php/home/index
then using this actual form action, will only append and not overwrite the existing URI.
Code:
http://example.com/index.php/home/index/signin
which should not be the behavior
#8

[eluser]Amitabh Roy[/eluser]
I guess the problem is when the credentials are wrong you are stuck in a unwanted url domain.com/index.php/main/validate_user .

And next if you can solve this issue by redirecting using bas_url() the form fields that were filled up were empty.

To overcome this you can use flashdata to set the field values when redirected back.
#9

[eluser]CodeIgniteMe[/eluser]
If you really want to get back to your original URIs you can set your validation at the same function.

Code:
<?php
Class Some extends CI_Controller{

function index()
{
if(isset($_POST['btnSubmit'])) // if the form isn't submitted yet, you skip validation
{
$this->validate_user();
}

$this->load->view('validation form');

}

function validate_user()
{
// your validation and/or redirects here
}

}

in your view
Code:
// with short_open_tags open
<?=form_open('some/index');?>
// your form codes
<?=form_close();?>




Theme © iAndrew 2016 - Forum software by © MyBB