Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create main page with codeigniter

How can i create a main page with codeigniter?

That page should contain a few links like login, register, etc.

I followed a tut to create a login screen. But it made codeigniter only for that purpose. This is the site i'm talking about:

http://tutsmore.com/programming/php/10-minutes-with-codeigniter-creating-login-form/

So basically what im trying to is, use codeigniter for more things than just a login form.

My try routes.php i set these settings:

$route['default_controller'] = "mainpage";
$route['login'] = "login";

My mainpage.php file:

class Mainpage extends Controller
{
    function Welcome()
    {
        parent::Controller();
    }

    function index()
    {

        $this->load->view('mainpage.html');
    }
}

Mainpage.html:

<HTML>

<HEAD>
<TITLE></TITLE>
<style>
      a.1{text-decoration:none}
      a.2{text-decoration:underline}
</style>

</HEAD>

<BODY>

     <a class="2" href="login.php">login</a>

</BODY>
</HTML>

Login.php looks exactly like the one in that website which i provided the link for in this post:

Class Login extends Controller
{
    function Login()
    {
        parent::Controller();
    }

    function Index()
    {
        $this->load->view('login_form');
    }

    function verify()
    {
        if($this->input->post('username'))
        { //checks whether the form has been submited
            $this->load->library('form_validation');//Loads the form_validation library class
            $rules = array(
                array('field'=>'username','label'=>'username','rules'=>'required'),
                array('field'=>'password','label'=>'password','rules'=>'required')
            );//validation rules

            $this->form_validation->set_rules($rules);//Setting the validation rules inside the validation function
            if($this->form_validation->run() == FALSE)
            { //Checks whether the form is properly sent
                $this->load->view('login_form'); //If validation fails load the <b style="color: black; background-color: rgb(153, 255, 153);">login</b> form again
            }
            else
            {
                $result = $this->common->login($this->input->post('username'),$this->input->post('password')); //If validation success then call the <b style="color: black; background-color: rgb(153, 255, 153);">login</b> function inside the common model and pass the arguments
                if($result)
                { //if <b style="color: black; background-color: rgb(153, 255, 153);">login</b> success
                    foreach($result as $row)
                    {
                        $this->session->set_userdata(array('logged_in'=>true,'id'=>$row->id,'username'=>$row->username)); //set the data into the session
                    }

                    $this->load->view('success'); //Load the success page
                }
            else
            { // If validation fails.
                    $data = array();
                    $data['error'] = 'Incorrect Username/Password'; //<b style="color: black; background-color: rgb(160, 255, 255);">create</b> the error string
                    $this->load->view('login_form', $data); //Load the <b style="color: black; background-color: rgb(153, 255, 153);">login</b> page and pass the error message
                }
            }
        }
        else
        {
            $this->load->view('login_form');
        }
    }
}

What am i missing guys?

like image 236
Yustme Avatar asked Dec 22 '22 00:12

Yustme


1 Answers

You're using CodeIgniter, right? Do you have some stipulation in your config that you must use .php as an extension on all URLs? If not, you should be sending your hrefs to "/login" not "/login.php". Furthermore, if you haven't removed the "index.php" from your URL in your htaccess file and CI config, you'll probably need to include that in your links.

Furthermore, if I were you, I'd not follow what Sarfraz does in his Mainpage.php file. You shouldn't be using standard PHP includes in CodeIgniter. Anything that is done with an include can easily be done by loading a view. For example, if you want to load the view as a string, you can say:

$loginViewString = $this->load->view('login.php', '', true);

Where the second parameter is whatever information you want passed to your view in an associative array where the key is the name of the variable you'll be passing and the value is the value. That is...

$dataToPassToView = array('test'=>'value');
$loginViewString = $this->load->view('login.php', $dataToPassToView, true);

And then in your login.php view you can just reference the variable $test which will have a value of "value".

Also, you don't really need to have that "login" route declared since you're just redirecting it to the "login" controller. What you could do is have a "user" controller with a "login" method and declare your route like this:

$routes['login'] = 'user/login';

EDIT...

OK, I think this has perhaps strayed one or two steps too far in the wrong direction. Let's start over, shall we?

First let's start with a list of the files that are relevant to this discussion:

  1. application/controllers/main.php (this will be your "default" controller)
  2. application/controllers/user.php (this will be the controller that handles user-related requests)
  3. application/views/header.php (I usually like to keep my headers and footers as separate views, but this is not necessary...you could simply echo the content as a string into a "mainpage" view as you're doing....though I should mention that in your example it appears you're forgetting to echo it into the body)
  4. application/views/footer.php
  5. application/views/splashpage.php (this is the content of the page which will contain the link to your login page)
  6. application/views/login.php (this is the content of the login page)
  7. application/config/routes.php (this will be used to reroute /login to /user/login)

So, now let's look at the code in each file that will achieve what you're trying to do. First the main.php controller (which, again, will be your default controller). This will be called when you go to your website's root address... www.example.com

application/controllers/main.php

class Main extends Controller
{
    function __construct() //this could also be called function Main(). the way I do it here is a PHP5 constructor
    {
        parent::Controller();
    }

    function index()
    {
        $this->load->view('header.php');
        $this->load->view('splashpage.php');
        $this->load->view('footer.php');
    }
}

Now let's take a look at the header, footer, and splashpage views:

application/views/header.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<meta name="description" content="This is the text people will read about my website when they see it listed in search engine results" /> 
<title>Yustme's Site</title> 

<!-- put your CSS includes here -->

</head> 

<body>

application/views/splashpage.php - note: there's no reason why you need a wrapper div here...it's only put there as an example

<div id="splashpagewrapper">
    <a href="/login">Click here to log in</a>
</div>

application/views/footer.php

</body>
<!-- put your javascript includes here -->
</html>

And now let's look at the User controller and the login.php view:

application/controllers/user.php

class User extends Controller
{
    function __construct() //this could also be called function User(). the way I do it here is a PHP5 constructor
    {
        parent::Controller();
    }

    function login()
    {
        $this->load->view('header.php');
        $this->load->view('login.php');
        $this->load->view('footer.php');
    }
}

application/views/login.php

<div id="login_box">
<!-- Put your login form here -->
</div>

And then finally the route to make /login look for /user/login:

application/config/routes.php

//add this line to the end of your routes list
$routes['login'] = '/user/login';

And that's it. No magic or anything. The reason I brought up the fact that you can load views as strings is because you may not want to have separate "header" and "footer" views. In this case, you could simply "echo" out a view as a string INTO another view. Another example is if you have a shopping cart full of items and you want the shopping cart and the items to be separate views. You could iterate through your items, loading the "shoppingcartitem" view as a string for each item, concatenate them together, and echo that string into the "shoppingcart" view.

So that should be it. If you still have questions, please let me know.

like image 83
treeface Avatar answered Dec 24 '22 13:12

treeface