Create a homepage with codeigniter

How to create a main page using codeigniter?

This page should contain several links, such as login, register, etc.

I followed tut to create a login screen. But this made codeigniter just 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 I'm trying to do is use codeigniter for more things than just a login form.

My attempt at routes.php sets the following 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 the same as on this site, to which I provided a link 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?

+4
source share
3 answers

You use CodeIgniter, right? Do you have any condition in your config that you should use .php as an extension for all URLs? If not, you should send your hrefs to "/ login", not "/login.php". Also, if you have not removed "index.php" from your URL in the htaccess file and in the CI configuration, you probably need to include this in your links.

In addition, if I were you, I would not follow what Sarfraz is doing in my Mainpage.php file. You should not use the standard PHP included with CodeIgniter. Everything that is done with include can be easily done by loading the view. For example, if you want to load a view as a string, you can say:

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

If the second parameter is any information that you want to pass to your view in the associative array, where the key is the name of the variable you will pass and the value is the value. It...

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

And then in your login.php view, you can simply refer to the $ test variable, which will have a value of "value".

In addition, you really do not need this “login” to be declared, since you are simply redirecting it to the “login” controller. What you can do is have a "user" controller with the "login" method and declare your route as follows:

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

EDIT ...

Well, I think it may have gotten lost one or two steps too far in the wrong direction. Let it begin, will we?

Start by listing the files related to this discussion:

  • application / controllers / main.php (this will be your "standard" controller)
  • application / controllers / user.php (this will be the controller that processes requests related to the user).
  • application / views / header.php (I usually like to keep the headers and footers as separate views, but this is not necessary ... you can just echo the contents as a string as a “main page”, but I have to mention in your example it seems that you forget the echo in the body)
  • App / views / footer.php
  • application / views / splashpage.php (this is the content of the page that will contain the link to your login page)
  • application / views / login.php (this is the contents of the login page)
  • application / config / routes.php (this will be used to redirect / login / user / login)

So, now let's look at the code in each file that will achieve what you are trying to do. First, the main.php controller (which, again, will be your default controller). This will be called when you go to the root address of your website ... 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 splash screen views:

app / 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 Site</title> <!-- put your CSS includes here --> </head> <body> 

application / views / splashpage.php - note: there is no reason why you need a div shell here ... it fits only as an example

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

app / views / footer.php

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

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'); } } 

app / views / login.php

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

And then finally a 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'; 

What is it. No magic or anything else. The reason I explained that you can load views as strings is because you cannot have separate header and footer views. In this case, you can simply “track” the view as an INTO string in another view. Another example: if you have a basket with many items, and you want the shopping basket and items to be separate views. You can iterate through your objects by loading the "shoppingcartitem" view as a string for each item, combining them together and repeating that line in the "shoppingcart" view.

It should be so. If you still have questions, please let me know.

+8
source

Please note that you did not specify 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'); } } 

Sentence:

Instead of creating an 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 change your main controller with this file name, for example:

 class Mainpage extends Controller { function Mainpage() { parent::Controller(); } function index() { $this->load->view('mainpage.php'); } } 
+2
source

I think you should first add a file named .htaccess in your home folder for example

 <IfModule mod_rewrite.c> RewriteEngine On # !IMPORTANT! Set your RewriteBase here and don't forget trailing and leading # slashes. # If your page resides at # http://www.example.com/mypage/test1 # then use # RewriteBase /mypage/test1/ RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /test/index.php?/$1 [L] </IfModule> <IfModule !mod_rewrite.c> # If we don't have mod_rewrite installed, all 404's # can be sent to index.php, and everything works as normal. # Submitted by: ElliotHaughin ErrorDocument 404 /index.php </IfModule> 

in the test field you need to add your home folder or baseurl

and you need to install baseurl on the configuration page as

 $config['base_url'] = 'http://localhost/test'; 

And the link inside the html page should be like <a class="2" href="http://localhost/test/Login">login</a>

On the login_form page, you need to make the action url as

 http://localhost/test/Login/verify 
+1
source

All Articles