June 25th, 2013, 05:04 AM
How web frameworks work?
Not really sure under which topic does this question lie. Please move it under an appropriate topic if this is not correct.
I recently started using a web framework (web2py) to create websites and I am confused about how it works.
Here's my current knowledge (please correct me if I'm wrong):-
2) To create a dynamic web page you need some sort of a programming language and php is one. We have servers that convert php into html for the browser to understand.
Using a framework it segregates my website into three parts - model, views and controller. Controller is written in python in my case. I don't understand how the client is interacting with the server.
Servers only understand php, I suppose. But here frameworks can be in so many languages - python, C, java etc. How does the server know what to do with the python/C/java file?
Basically how frameworks are working?
June 25th, 2013, 02:31 PM
A web framework is just a library to take a lot of the basic effort out of building a web application. Using a framework is not required.
An MVC framework separates the application into three parts, models, views and controllers. The MVC design is just possible design for a framework, there are others.
Frameworks are not normally implemented in more than one language as that is pretty inefficient.
June 26th, 2013, 09:53 AM
So a server say Apache understands more than one language. How does it know whether it is python, java or c?
Also frameworks have their own method of organizing files into folders. How does the server know where to go? Is there a default directory or something?
June 26th, 2013, 01:12 PM
Apache itself doesn't understand any languages, however it can be configured to execute programs that do understand languages in response to a particular HTTP request. In most cases, Apache decides how to handle a particular request based on the file extension of the requested file.
Support for particular languages is added to Apache in one of two ways: either by adding a module to Apache, or by configuring Apache to use a CGI or fast-CGI API to communicate with another program that understands how to execute the file.
In the case of PHP, either option is possible. mod_php is an Apache modules that handles requests for PHP files by invoking the PHP interpreter on the requested file. PHP also has a CGI/fCGI program that Apache can be configured to use to execute PHP files. The CGI program also invokes the PHP interpreter to process the requested file. In both cases, the normal PHP interpreter is used to execute the PHP file, and only the input and output is passed between Apache and PHP, not the code itself.
Similar is true of other languages.
The web server executes the file that is requested in the HTTP request, relative to its root directory. So if the server root directory is /var/www/, and the HTTP request asks for /list.php, then Apache executes /var/www/list.php. Apache is not aware of the way in which frameworks structure files, that is handled entirely by the application code.
The exception to this is a case where you are using a URL rewriting module like mod_rewrite. In that situation, the file that gets executed may not be the one in the HTTP request if the rewrite rules change it. The rules for rewriting URLs would be specified in a configuration file.