views:

113

answers:

2

Hi,

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?

A: 

Note that you are not specifying the correct method name for your main controller:

class Mainpage extends Controller
{
    function Welcome() // < problem should be Mainpage
    {
        parent::Controller();
    }

    function index()
    {

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

Suggestion:

Instead of creating html page, create a php page and use the include command to include the login.php file.

Mainpage.php:

<HTML>

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

</HEAD>

<BODY>

     <?php include './login.php'; ?>

</BODY>
</HTML>

And modify your main controller with this file name eg:

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

    function index()
    {

        $this->load->view('mainpage.php');
    }
}
Sarfraz
Still no luck. I'm getting this error: A PHP Error was encounteredSeverity: WarningMessage: include(./login.php) [function.include]: failed to open stream: No such file or directoryFilename: views/mainpage.phpLine Number: 14I triple checked everything.
Yustme
use absolute path like this `dir/dir2/login.php` Make sure that you specify the correct path. Also let me know if you could the path of hte login.php file.
Sarfraz
Absolute path didn't work either which was: <?php include 'D:/xampp/htdocs/CISite/application/controllers/login.php'; ?>. I didn't understand your last comment from 'Also .... '.
Yustme
Ok try: `include ("../controllers/login.php");`
Sarfraz
Didn't work either. Tried with 1 dot and also without '../'. Both failed.
Yustme
Tried with both dots as i showed? Otherwise try with what you were doing with html `<a class="2" href="login.php">login</a>` but iam not sure it will work it might just as well create the path issue.
Sarfraz
Ya tried both the dots as u showed too. Its like login.php doens't excist. Triple checked everything again, no luck.
Yustme
Otherwise try with what you were doing with html `<a class="2" href="login.php">login</a>` but iam not sure it will work it might just as well create the path issue.
Sarfraz
Im sorry if i wasn't clear, but i tried that option too.
Yustme
+2  A: 

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"&gt; 
<html xmlns="http://www.w3.org/1999/xhtml"&gt; 
<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.

treeface
I removed the index.php file from CI i followed this tutorial: http://www.anmsaiful.net/blog/php/remove-codeigniter-index-php.html and because i changed mainpage.html to .php i got 2 mainpage.php now. i changed the one which was .html to index.php. the body looks like this: <BODY> <?php $loginViewString = $this->load->view('login.php', '', true); ?> <a class="2" href="login">login</a> </BODY> now i can't get to mainpage.php which is the default_controller. 404 page not found.
Yustme
Just to be clear, the login.php is located in the controller folder. Which loads the view login_form.php from views folder. Is there anyway to call another controller from a controller? the mainpage is a controller and so is login.php
Yustme
Hey Yustme. I can't fully tell anymore exactly what you're trying to do, because I think there's been a misunderstanding. I'll edit my post to try to walk you through exactly how I would put together a "landing" page and a "login" page.
treeface
Thanks! That did the trick!
Yustme