Ajzz
Ajzz

Reputation: 360

defining custom urls in codeigniter

In codeigniter we can define the url that should be displayed in the browser. I have a login page which goes well when the credentials are given right. I need to show an error message in the same page itself if the credentials are wrong. For that i have did the followings.

<div style="<?php echo ($status==='err')?'display:block':''?>" class="form-login-error">
            <h3>Invalid login</h3>
            <p>Please enter correct email and password!</p>
        </div>

and

$data['status'] = (isset($_GET['status']))?$_GET['status']:true;

in controllers's index function

if($userdata['status'] == 'success')
    {
        if(!empty($userdata['userdet']))
        {
            $this->session->set_userdata($userdata['userdet']);
            redirect('dashboard');
        }
    }else{

        redirect('login?status=err');
    }

The code works well. but my doubt is can i change the url pattern? It is now showing loclhost/myproject/login?status=err.Can i change this to localhost/myproject/loginerror? I tried to define in routes, but it is showing page not found error. Thanks in advance.

Upvotes: 0

Views: 46

Answers (2)

Mukesh Kumar Sahu
Mukesh Kumar Sahu

Reputation: 11

use the codeigniter application/config/routes.php

$route['myproject/login/(:any)']="myproject/login/$1";

youcan hold the value of parameter in action of controller.

Upvotes: 0

Mohan
Mohan

Reputation: 4829

You are making a simple thing much complex by using URL's param's here. I would suggest using CI session Flashdata. The benefit of using Flashdata is that the session variable is maintained only for the next page and then the session variable is cleared automatically so it is very useful in cases where we need to display some messages to users.

Change your code to something like this :

<div style="<?php echo ($this->session->flashdata('error') )?'display:block':''?>" class="form-login-error">
            <h3>Invalid login</h3>
            <p>Please enter correct email and password!</p>
        </div>

and in your controller :

if($userdata['status'] == 'success')
    {
        if(!empty($userdata['userdet']))
        {
            $this->session->set_userdata($userdata['userdet']);
            redirect('dashboard');
        }
    }else{
        $this->session->set_flashdata('error', 1);
        redirect('login');
    }

Upvotes: 1

Related Questions