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> </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> </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"> </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.