tags:

views:

44

answers:

1

ok yesterday i open a thread about when to use mvc,

today im about to learn how does the MVC frameworks works, ive open some framework like CI, CAKE, etc

1.on the .htacsess i found this

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteRule ^(.*)$ index.php/?url=$1 [QSA,L]
</IfModule> 

ok so when we type http://localhost/mymvc/something1/something2/something3/somethingetc

we got an $_GET['url'] = string 'something1/something2/something3/somethingetc' (length=45)

2.so i sugest the something1 will be the class, something2 must be the function and something3 im not quite sure, how does exectly framework loads the class ?, functions ?

<?php
class Blog extends Controller {

    function index()
    {
        echo 'Hello World!';
    }
    function stack()
    {
        echo 'Hello Stack!';
    }      
}
?>

3.ok so , i found that every framework first loads the config files, then loads a front-controller this is a front-controller ( on ci ) looks, i assume that they do like this ?

  • extends the class ?
  • they get the name of the class ? then require_once controller.nameclass.php
  • then they somehow search for functions ? ( how do they do that ? )
  • next they look for the default function ( function index ) then loads it ?
  • if there is a client call the url /Blog/stack it loads just the Stack function, i dont >know how that work either .
  • if we put $this->loadview('something') so i assume that they call the function loadview ( that is inside the Controller class and require them by the name, like require_once something.php

maybe there is a part two of this :|,

Thanks a lot.

Adam Ramadhan

A: 
  1. The framework that built this .htaccess file, uses PHP to decompose its elements (possibly slower than Apache would do it with more complex declarations in .htaccess)
  2. Yes, it determines the class (Controller) and the method to call (Action), and passes the rest of the parameters to that method. It uses call_user_func or something similar to call the functionality after the framework's core functionality has executed.
  3. A function presence can be checked also. PHP has some basic Reflection implementation, which allows you to see what the class/object is built of.

Controller classes in many cases inherit a base class, for this functionality.

This doesn't really give you much. You'd have better luck with the frameworks if you learn how to work with it, not how it works.

It might help if you take a course of pure MVC (not linked to any framework), or if you're in a hurry, try this.

Alexander
for number one, well there are some framework ( vanilla2 ) who does not decompose its elemets by doing that in .htacses, but how do they parse a $_GET ?
Adam Ramadhan
I don't know if I understand the question. If .htaccess passes on the requested URL after the domain name as a string in $_GET, it can be easily decomposed using the `explode` method in PHP.If "beautiful" URLs are present, on Apache, there must be a rewrite condition in .htaccess to do that.
Alexander
you got that right. oh i see, so they explode it to array ? then they search the first array ( class ) second array ( function ) ? is that faster then above ? thanks
Adam Ramadhan
In general, there are more ways of handling this: 1. Write regular expressions in .htaccess that would separate the controller, action and parameters from the URL, so PHP would only work with many $_GET parameters. 2. Use .htaccess only to pass the URL as a $_GET parameter to PHP, and let PHP with explode function do the rest - take the first parameter as controller name, second as action name, etc. 3. Same as above, but use regular expressions instead of explode; this is slower, but can handle more complex URL composition techniques.
Alexander
great, anyway the call_user_func is a very very useful to me thanks, maybe i should wait for others :)
Adam Ramadhan
There are much more wonderful features in PHP that you should know about.http://www.php.net/manual/en/ref.funchand.phphttp://www.php.net/manual/en/book.classobj.phplook through them. You will find many interesting and useful stuff. Oh, and also check out PHP Magic Methods - http://php.net/manual/en/language.oop5.magic.php .
Alexander