Jump to content

Recommended Posts

I know some people make a single point of entry to their website which is usually index.php - the first site you visit and login from.

 

But what is this exactly? Is it a controller that decides which webpages to show like in this tutorial http://www.technotaste.com/blog/simple-php-front-controller/ ?

 

Why would you want a controller for a whole page?

 

How does it work? Does it just redirect every link you click to the index.php and every header in php leads to index.php?

 

In the provided tutorial, author goes on how we type the url and it redirects us to index.php. But what does this mean in reality, when you must click links and forms to navigate through a website?

 

Does this mean that every submitted form and every new php page redirects to controller page and then the controller page redirects everything to index.php?

Edited by Matic
Link to comment
https://forums.phpfreaks.com/topic/281091-single-point-of-entry-for-php-app/
Share on other sites

Yes, it's called a front controller. No, it doesn't work through redirects (generally speaking). What happens is that the front controller catches the request made to the server, looks at the URL passed in, and then includes what the request/URL was looking for.

 

It makes a lot more sense when you start learning the MVC pattern. The front controller merely parses the URL, and then creates and delegates to the proper controller to actually handle the request.

Yes, it's called a front controller. No, it doesn't work through redirects (generally speaking). What happens is that the front controller catches the request made to the server, looks at the URL passed in, and then includes what the request/URL was looking for.

 

It makes a lot more sense when you start learning the MVC pattern. The front controller merely parses the URL, and then creates and delegates to the proper controller to actually handle the request.

 

So every request we make on a specific controller is redirected via .htaccess modrewrite to the front controller, which then displays the output of that controller?

Yes, normally people would use index.php, especially since many webservers load this by default.

 

.htaccess would rewrite www.example.com/products/samsung-tv-45inch to index.php?url=products/samsung-tv-45inch.

 

Then index.php would read the $_GET['url'] var, split it up and decide to load the products controller and pass it the value of samsung-tv-45inch

 

The products controller would then look up the database details (usually via the model) for samsung-tv-45inch and pass the results to a view that would display the results

So if I set up my front contoller right, then everything will be displayed on index.php, every template I have? But why would I want this

Because it makes things act more like a coherent system and less like a loose collection of standalone pages. Index.php is merely the entry point of an application. The address bar is a user's terminal into that application.

 

Put another way, it generally looks like:

 

                                      - controller (which may or may not access a db, process data, etc.) -> template
                                     /
request to server -> front controller - controller -> template
                                     \
                                      - controller -> template

Your index.php would be in charge of deciding which other files to includes/run. It would not handle any of the requests directly (except maybe an error request, ie 404).

 

Also none of your links actually include the index.php filename in them. The request gets re-routed to index.php via a behind-the-scenes mechanism such as mod_rewrite.

 

So what happens is the user enters the url (or click a link going to): http://example.com/blog/2013/12/1/192-a-simple-blog. mod_rewrite would then take this request, and rewrite it so that rather than the server trying to serve the file blog/2013/12/1/192-a-simple-blog it will instead serve up index.php.

 

Inside index.php you would have the code examine the $_SERVER['REQUEST_URI'] variable which would contain the url the user tried to access (ie /blog/2013/12/1/192-a-simple-blog) and based on that URL determine how to handle the request. For example, the above might be matched by a regex such as:

$blog_request = '^/blog/\d+/\d+/\d+/(\d+)-.*$';
if (preg_match($blog_request, $_SERVER['REQUEST_URI'], $urlparams)){
   //This is a blog request, include the blog handler
   include './controllers/blog.php';
}
Inside ./controllers/blog.php, we'd handle the retrival and display of the blog entry:

$blogid = $urlparams[1];
$res = $db->query('select title, content from blogs where blogid='.intval($blogid));
//...
$template->render('./views/blog.tpl');
As for why you'd want to do this rather than just have different files, there are a few reasons:

1) It generally results in cleaner URLs since the URLs do not have to map to a real file. Instead they map to a concept/data object

2) index.php makes a convinent place to setup any necessary resources such as the database connection, session details, etc

3) It helps keep the backend files organized without having to worry about how their layout will affect URL structure

Edited by kicken

Wow great answers guys thanks. However now that I understand this better I must ask you why would I use this if my user has to log in to access the page anyway? I could simply use $_SESSION variables to display correct data on my index page... like so (I am building a simple browser game):

if ($_SESSION['combat'] = true)
{
include 'certain_page.php';
}
else if (...)
{
include 'another_page';
}
else if ..... etc...

Edited by Matic

 

Wow great answers guys thanks. However now that I understand this better I must ask you why would I use this if my user has to log in to access the page anyway? I could simply use $_SESSION variables to display correct data on my index page... like so (I am building a simple browser game):

if ($_SESSION['combat'] = true)
{
include 'certain_page.php';
}
else if (...)
{
include 'another_page';
}
else if ..... etc...

 

That's almost a totally different question.  In the front controller and/or MVC method you would still load the correct controller and it would check if the user is authorized and take action accordingly.  Maybe it would do all of the same things but just render a different view, or it may do something altogether different.

Edited by AbraCadaver

That's almost a totally different question.  In the front controller and/or MVC method you would still load the correct controller and it would check if the user is authorized and take action accordingly.

 

Exactly so it is safe to say that I can use session variables to achieve the same result in my front controller?

You have to check the url to determine which page they want to go to. Unless your site consists of only two pages, one for those logged in, and one for those not.

 

If my users have an active session I can also determine which page to redirect them to based on their choices. Just store their actions in a variable and call that variable in index.php

 

And the index.php will display apropriate include file! The url will then always be index.php/

Edited by Matic
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.